Unverified Commit efbd726b authored by David Haynes's avatar David Haynes
Browse files

A bunch of comments adhering to pep8

- and some formatting fixes I believe
parent b266fc9e
Pipeline #1143 passed with stage
in 1 minute and 34 seconds
"""
go/admin.py
"""
# Future Imports
from __future__ import unicode_literals, absolute_import, print_function, division
......
"""
go/cas_callbacks.py
"""
# Future Imports
from __future__ import unicode_literals, absolute_import, print_function, division
......
"""
go/forms.py
"""
# Future Imports
from __future__ import unicode_literals, absolute_import, print_function, division
......
"""
go/models.py
"""
# Future Imports
from __future__ import unicode_literals, absolute_import, print_function, division
from __future__ import (absolute_import, division, print_function,
unicode_literals)
# Python stdlib Imports
import string
# Django Imports
from django.db import models
from django.contrib.auth.models import User
from django.utils import timezone
from django.core.cache import cache
from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.utils import timezone
from django.utils.encoding import python_2_unicode_compatible
# Other Imports
from hashids import Hashids # http://hashids.org/python/
# generate the salt and initialize Hashids
hashids = Hashids(salt="srct.gmu.edu", alphabet=(string.ascii_lowercase + string.digits))
HASHIDS = Hashids(
salt="srct.gmu.edu", alphabet=(string.ascii_lowercase + string.digits)
)
@python_2_unicode_compatible
class RegisteredUser(models.Model):
......@@ -58,7 +65,9 @@ class RegisteredUser(models.Model):
str(RegisteredUser)
"""
return '<Registered User: %s - Approval Status: %s>' % (self.user, self.approved)
return '<Registered User: %s - Approval Status: %s>' % (
self.user, self.approved
)
@receiver(post_save, sender=User)
......@@ -105,7 +114,9 @@ class URL(models.Model):
print(URL)
"""
return '<Owner: %s - Target URL: %s>' % (self.owner.user, self.target)
return '<Owner: %s - Target URL: %s>' % (
self.owner.user, self.target
)
class Meta:
"""
......@@ -124,13 +135,14 @@ class URL(models.Model):
if cache.get("hashids_counter") is None:
cache.set("hashids_counter", URL.objects.count())
cache.incr("hashids_counter")
short = hashids.encrypt(cache.get("hashids_counter"))
short = HASHIDS.encrypt(cache.get("hashids_counter"))
tries = 1
while tries < 100:
try:
URL.objects.get(short__iexact = short)
URL.objects.get(short__iexact=short)
tries += 1
cache.incr("hashids_counter")
except URL.DoesNotExist as ex:
print(ex)
return short
return None
"""
go/test_views.py
References:
- http://stackoverflow.com/a/11887308
"""
# Future Imports
from __future__ import unicode_literals, absolute_import, print_function, division
from __future__ import (absolute_import, division, print_function,
unicode_literals)
# Django Imports
from django.test import TestCase
# App Imports
from go.views import index, view, my_links, delete, signup, redirection, useradmin
from go.views import (
index, view, my_links, delete, signup, redirection, useradmin
)
class IndexTest(TestCase):
"""
......
"""
go/views.py
"""
# Future Imports
from __future__ import unicode_literals, absolute_import, print_function, division
......@@ -12,7 +16,6 @@ from django.utils import timezone
from django.core.exceptions import PermissionDenied # ValidationError
from django.core.mail import send_mail, EmailMessage
from django.contrib.auth import REDIRECT_FIELD_NAME
from django.contrib.auth.models import User
from django.contrib.auth.decorators import user_passes_test, login_required
from django.shortcuts import render, get_object_or_404, redirect
......@@ -61,9 +64,7 @@ def index(request):
# If there is a 500 error returned, handle it
if res == 500:
return HttpResponseServerError(
render(request, 'admin/500.html', {})
)
return HttpResponseServerError(render(request, '500.html'))
# Redirect to the shiny new URL
return redirect('view', res.short)
......@@ -86,7 +87,7 @@ def index(request):
@ratelimit(key='user', rate='25/d', method='POST', block=True)
def post(request, url_form):
"""
Function that handles POST requests for the URL creation ProcessLookupError
Helper function that handles POST requests for the URL creation
"""
# We don't commit the url object yet because we need to add its
......@@ -138,8 +139,8 @@ def post(request, url_form):
def view(request, short):
"""
This view allows the user to view details about a URL. Note that they
do not need to be logged in to view info.
This view allows the user to "view details" about a URL. Note that they
do not need to be logged in to view this information.
"""
# Get the current domain info
......@@ -204,7 +205,8 @@ def delete(request, short):
@login_required
def signup(request):
"""
This view presents the user with a registration form. You can register yourself.
This view presents the user with a registration form. You can register
yourself.
"""
# Do not display signup page to registered or approved users
......@@ -317,7 +319,7 @@ def redirection(request, short):
# If the user is trying to make a Go link to itself, we 404 them
if url.target == domain + short:
return redirect('admin/404.html')
return redirect('404.html')
# If the user is coming from a QR request then increment qrclicks
if 'qr' in request.GET:
......@@ -339,8 +341,8 @@ def staff_member_required(view_func, redirect_field_name=REDIRECT_FIELD_NAME, lo
return user_passes_test(
lambda u: u.is_active and u.is_staff,
login_url = login_url,
redirect_field_name = redirect_field_name
login_url=login_url,
redirect_field_name=redirect_field_name
)(view_func)
@staff_member_required
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment