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 # Future Imports
from __future__ import unicode_literals, absolute_import, print_function, division from __future__ import unicode_literals, absolute_import, print_function, division
......
"""
go/cas_callbacks.py
"""
# Future Imports # Future Imports
from __future__ import unicode_literals, absolute_import, print_function, division from __future__ import unicode_literals, absolute_import, print_function, division
......
"""
go/forms.py
"""
# Future Imports # Future Imports
from __future__ import unicode_literals, absolute_import, print_function, division from __future__ import unicode_literals, absolute_import, print_function, division
......
"""
go/models.py
"""
# Future Imports # 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 # Python stdlib Imports
import string import string
# Django Imports # Django Imports
from django.db import models
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.utils import timezone
from django.core.cache import cache from django.core.cache import cache
from django.db import models
from django.db.models.signals import post_save from django.db.models.signals import post_save
from django.dispatch import receiver from django.dispatch import receiver
from django.utils import timezone
from django.utils.encoding import python_2_unicode_compatible from django.utils.encoding import python_2_unicode_compatible
# Other Imports # Other Imports
from hashids import Hashids # http://hashids.org/python/ from hashids import Hashids # http://hashids.org/python/
# generate the salt and initialize Hashids # 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 @python_2_unicode_compatible
class RegisteredUser(models.Model): class RegisteredUser(models.Model):
""" """
This is simply a wrapper model for the user object which, if an object This is simply a wrapper model for the user object which, if an object
exists, indicates that that user is registered. exists, indicates that that user is registered.
""" """
...@@ -58,13 +65,15 @@ class RegisteredUser(models.Model): ...@@ -58,13 +65,15 @@ class RegisteredUser(models.Model):
str(RegisteredUser) 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) @receiver(post_save, sender=User)
def handle_regUser_creation(sender, instance, created, **kwargs): def handle_regUser_creation(sender, instance, created, **kwargs):
""" """
When a post_save is called on a User object (and it is newly created), this When a post_save is called on a User object (and it is newly created), this
is called to create an associated RegisteredUser is called to create an associated RegisteredUser
""" """
...@@ -105,7 +114,9 @@ class URL(models.Model): ...@@ -105,7 +114,9 @@ class URL(models.Model):
print(URL) 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: class Meta:
""" """
...@@ -124,13 +135,14 @@ class URL(models.Model): ...@@ -124,13 +135,14 @@ class URL(models.Model):
if cache.get("hashids_counter") is None: if cache.get("hashids_counter") is None:
cache.set("hashids_counter", URL.objects.count()) cache.set("hashids_counter", URL.objects.count())
cache.incr("hashids_counter") cache.incr("hashids_counter")
short = hashids.encrypt(cache.get("hashids_counter")) short = HASHIDS.encrypt(cache.get("hashids_counter"))
tries = 1 tries = 1
while tries < 100: while tries < 100:
try: try:
URL.objects.get(short__iexact = short) URL.objects.get(short__iexact=short)
tries += 1 tries += 1
cache.incr("hashids_counter") cache.incr("hashids_counter")
except URL.DoesNotExist as ex: except URL.DoesNotExist as ex:
print(ex)
return short return short
return None return None
"""
go/test_views.py
References:
- http://stackoverflow.com/a/11887308
"""
# Future Imports # Future Imports
from __future__ import unicode_literals, absolute_import, print_function, division from __future__ import (absolute_import, division, print_function,
unicode_literals)
# Django Imports # Django Imports
from django.test import TestCase from django.test import TestCase
# App Imports # 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): class IndexTest(TestCase):
""" """
......
"""
go/views.py
"""
# Future Imports # Future Imports
from __future__ import unicode_literals, absolute_import, print_function, division from __future__ import unicode_literals, absolute_import, print_function, division
...@@ -12,7 +16,6 @@ from django.utils import timezone ...@@ -12,7 +16,6 @@ from django.utils import timezone
from django.core.exceptions import PermissionDenied # ValidationError from django.core.exceptions import PermissionDenied # ValidationError
from django.core.mail import send_mail, EmailMessage from django.core.mail import send_mail, EmailMessage
from django.contrib.auth import REDIRECT_FIELD_NAME 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.contrib.auth.decorators import user_passes_test, login_required
from django.shortcuts import render, get_object_or_404, redirect from django.shortcuts import render, get_object_or_404, redirect
...@@ -61,9 +64,7 @@ def index(request): ...@@ -61,9 +64,7 @@ def index(request):
# If there is a 500 error returned, handle it # If there is a 500 error returned, handle it
if res == 500: if res == 500:
return HttpResponseServerError( return HttpResponseServerError(render(request, '500.html'))
render(request, 'admin/500.html', {})
)
# Redirect to the shiny new URL # Redirect to the shiny new URL
return redirect('view', res.short) return redirect('view', res.short)
...@@ -86,7 +87,7 @@ def index(request): ...@@ -86,7 +87,7 @@ def index(request):
@ratelimit(key='user', rate='25/d', method='POST', block=True) @ratelimit(key='user', rate='25/d', method='POST', block=True)
def post(request, url_form): 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 # We don't commit the url object yet because we need to add its
...@@ -138,8 +139,8 @@ def post(request, url_form): ...@@ -138,8 +139,8 @@ def post(request, url_form):
def view(request, short): def view(request, short):
""" """
This view allows the user to view details about a URL. Note that they This view allows the user to "view details" about a URL. Note that they
do not need to be logged in to view info. do not need to be logged in to view this information.
""" """
# Get the current domain info # Get the current domain info
...@@ -204,7 +205,8 @@ def delete(request, short): ...@@ -204,7 +205,8 @@ def delete(request, short):
@login_required @login_required
def signup(request): 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 # Do not display signup page to registered or approved users
...@@ -317,7 +319,7 @@ def redirection(request, short): ...@@ -317,7 +319,7 @@ def redirection(request, short):
# If the user is trying to make a Go link to itself, we 404 them # If the user is trying to make a Go link to itself, we 404 them
if url.target == domain + short: 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 the user is coming from a QR request then increment qrclicks
if 'qr' in request.GET: if 'qr' in request.GET:
...@@ -339,8 +341,8 @@ def staff_member_required(view_func, redirect_field_name=REDIRECT_FIELD_NAME, lo ...@@ -339,8 +341,8 @@ def staff_member_required(view_func, redirect_field_name=REDIRECT_FIELD_NAME, lo
return user_passes_test( return user_passes_test(
lambda u: u.is_active and u.is_staff, lambda u: u.is_active and u.is_staff,
login_url = login_url, login_url=login_url,
redirect_field_name = redirect_field_name redirect_field_name=redirect_field_name
)(view_func) )(view_func)
@staff_member_required @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