Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
SRCT
go
Commits
70214fcf
Unverified
Commit
70214fcf
authored
Mar 13, 2017
by
David Haynes
Browse files
Format docstrings and approach PEP8 compliance
def main(): """ words about the function main() """ print("Hello World!")
parent
9894fcd7
Pipeline
#1077
passed with stage
in 1 minute and 6 seconds
Changes
11
Pipelines
2
Hide whitespace changes
Inline
Side-by-side
go/go/admin.py
View file @
70214fcf
...
...
@@ -9,26 +9,29 @@ from django.contrib.auth.models import User
# App Imports
from
go.models
import
URL
,
RegisteredUser
"""
Define what attributes display in the URL Admin
"""
class
URLAdmin
(
admin
.
ModelAdmin
):
"""
Define what attributes display in the URL Admin
"""
list_display
=
(
"target"
,
"short"
,
"owner"
,
"clicks"
,
"date_created"
,
"expires"
)
# Register URLAdmin
admin
.
site
.
register
(
URL
,
URLAdmin
)
"""
Define an inline admin descriptor for User model
"""
class
RegisteredUserInline
(
admin
.
StackedInline
):
"""
Define an inline admin descriptor for User model
"""
model
=
RegisteredUser
can_delete
=
False
"""
Define a new User admin
"""
class
UserAdmin
(
UserAdmin
):
"""
Define a new User admin
"""
# see above class that we defined
inlines
=
(
RegisteredUserInline
,
)
...
...
go/go/forms.py
View file @
70214fcf
# Future Imports
from
__future__
import
unicode_literals
,
absolute_import
,
print_function
,
division
# Python stdlib Imports
from
datetime
import
date
,
datetime
,
timedelta
from
six.moves
import
urllib
# Django Imports
from
django
import
forms
from
django.core.exceptions
import
ValidationError
...
...
@@ -15,13 +19,11 @@ from crispy_forms.helper import FormHelper
from
crispy_forms.layout
import
Layout
,
Fieldset
,
Submit
,
HTML
,
Div
,
Field
from
crispy_forms.bootstrap
import
StrictButton
,
PrependedText
,
Accordion
,
AccordionGroup
from
bootstrap3_datetime.widgets
import
DateTimePicker
from
datetime
import
date
,
datetime
,
timedelta
from
six.moves
import
urllib
"""
The form that is used in URL creation.
"""
class
URLForm
(
forms
.
ModelForm
):
"""
The form that is used in URL creation.
"""
# Prevent redirect loop links
def
clean_target
(
self
):
...
...
@@ -34,7 +36,7 @@ class URLForm(forms.ModelForm):
except
urllib
.
error
.
URLError
as
e
:
# to permit users to enter sites that return most errors, but
# prevent them from entering sites that result in an HTTP 300 error
if
any
(
int
(
str
(
e
)[
11
:
14
])
==
errorNum
for
errorNum
in
range
(
300
,
308
)):
if
any
(
int
(
str
(
e
)[
11
:
14
])
==
errorNum
for
errorNum
in
range
(
300
,
308
)):
raise
ValidationError
(
"Link results in a 300 error"
)
else
:
final_url
=
""
...
...
@@ -67,12 +69,12 @@ class URLForm(forms.ModelForm):
# Custom short-url field with validators.
short
=
forms
.
SlugField
(
required
=
False
,
label
=
'Short URL (Optional)'
,
widget
=
forms
.
TextInput
(),
validators
=
[
unique_short
],
max_length
=
20
,
min_length
=
3
,
required
=
False
,
label
=
'Short URL (Optional)'
,
widget
=
forms
.
TextInput
(),
validators
=
[
unique_short
],
max_length
=
20
,
min_length
=
3
,
)
# define some string date standards
...
...
@@ -93,11 +95,11 @@ class URLForm(forms.ModelForm):
# Add preset expiration choices.
expires
=
forms
.
ChoiceField
(
required
=
True
,
label
=
'Expiration (Required)'
,
choices
=
EXPIRATION_CHOICES
,
initial
=
NEVER
,
widget
=
forms
.
RadioSelect
(),
required
=
True
,
label
=
'Expiration (Required)'
,
choices
=
EXPIRATION_CHOICES
,
initial
=
NEVER
,
widget
=
forms
.
RadioSelect
(),
)
# Check if the selected date is a valid date
...
...
@@ -112,12 +114,12 @@ class URLForm(forms.ModelForm):
# Add a custom expiration choice.
expires_custom
=
forms
.
DateTimeField
(
required
=
False
,
label
=
'Custom Date'
,
input_formats
=
[
'%m-%d-%Y'
],
validators
=
[
valid_date
],
initial
=
lambda
:
datetime
.
now
()
+
timedelta
(
days
=
1
),
widget
=
DateTimePicker
(
required
=
False
,
label
=
'Custom Date'
,
input_formats
=
[
'%m-%d-%Y'
],
validators
=
[
valid_date
],
initial
=
lambda
:
datetime
.
now
()
+
timedelta
(
days
=
1
),
widget
=
DateTimePicker
(
options
=
{
"format"
:
"MM-DD-YYYY"
,
"pickTime"
:
False
,
...
...
@@ -154,9 +156,9 @@ class URLForm(forms.ModelForm):
<h4>Paste the URL you would like to shorten:</h4>
<br />"""
),
'target'
,
style
=
"background: rgb(#F6F6F6);"
),
active
=
True
,
template
=
'crispy/accordian-group.html'
),
style
=
"background: rgb(#F6F6F6);"
),
active
=
True
,
template
=
'crispy/accordian-group.html'
),
# Step 2: Short URL
AccordionGroup
(
'Step 2: Short URL'
,
...
...
@@ -166,9 +168,9 @@ class URLForm(forms.ModelForm):
<br />"""
),
PrependedText
(
'short'
,
'https://go.gmu.edu/'
,
template
=
'crispy/customPrepended.html'
),
style
=
"background: rgb(#F6F6F6);"
),
active
=
True
,
template
=
'crispy/accordian-group.html'
,),
style
=
"background: rgb(#F6F6F6);"
),
active
=
True
,
template
=
'crispy/accordian-group.html'
,),
# Step 3: Expiration
AccordionGroup
(
'Step 3: URL Expiration'
,
...
...
@@ -178,12 +180,12 @@ class URLForm(forms.ModelForm):
<br />"""
),
'expires'
,
Field
(
'expires_custom'
,
template
=
"crispy/customDateField.html"
),
style
=
"background: rgb(#F6F6F6);"
),
active
=
True
,
template
=
'crispy/accordian-group.html'
),
style
=
"background: rgb(#F6F6F6);"
),
active
=
True
,
template
=
'crispy/accordian-group.html'
),
# FIN
template
=
'crispy/accordian.html'
),
# FIN
template
=
'crispy/accordian.html'
),
#######################
HTML
(
"""
<br />"""
),
...
...
@@ -194,42 +196,44 @@ class URLForm(forms.ModelForm):
# what model this form is for
model
=
URL
# what attributes are included
fields
=
[
'target'
,
]
fields
=
[
'target'
]
"""
The form that is used when a user is signing up to be a RegisteredUser
"""
class
SignupForm
(
forms
.
ModelForm
):
"""
The form that is used when a user is signing up to be a RegisteredUser
"""
# The full name of the RegisteredUser
full_name
=
forms
.
CharField
(
required
=
True
,
label
=
'Full Name (Required)'
,
max_length
=
100
,
widget
=
forms
.
TextInput
(),
required
=
True
,
label
=
'Full Name (Required)'
,
max_length
=
100
,
widget
=
forms
.
TextInput
(),
)
# The RegisteredUser's chosen organization
organization
=
forms
.
CharField
(
required
=
True
,
label
=
'Organization (Required)'
,
max_length
=
100
,
widget
=
forms
.
TextInput
(),
required
=
True
,
label
=
'Organization (Required)'
,
max_length
=
100
,
widget
=
forms
.
TextInput
(),
)
# The RegisteredUser's reason for signing up to us Go
description
=
forms
.
CharField
(
required
=
False
,
label
=
'Description (Optional)'
,
max_length
=
200
,
widget
=
forms
.
Textarea
(),
required
=
False
,
label
=
'Description (Optional)'
,
max_length
=
200
,
widget
=
forms
.
Textarea
(),
)
# A user becomes registered when they agree to the TOS
registered
=
forms
.
BooleanField
(
required
=
True
,
# ***Need to replace lower url with production URL*** ie. go.gmu.edu/about#terms
label
=
mark_safe
(
'Do you accept the <a href="http://127.0.0.1:8000/about#terms">Terms of Service</a>?'
),
label
=
mark_safe
(
'Do you accept the <a href="http://127.0.0.1:8000/about#terms">Terms of Service</a>?'
),
)
# on initialization of the form, crispy forms renders this layout
...
...
@@ -262,4 +266,4 @@ class SignupForm(forms.ModelForm):
# what model this form is for
model
=
RegisteredUser
# what attributes are included
fields
=
[
'full_name'
,
'organization'
,
'description'
,
'registered'
,
]
fields
=
[
'full_name'
,
'organization'
,
'description'
,
'registered'
]
go/go/management/commands/expirelinks.py
View file @
70214fcf
...
...
@@ -10,6 +10,10 @@ from go.models import URL
# Define a new custom django-admin command
class
Command
(
BaseCommand
):
"""
Remove expired links from the database
"""
# Define help text for this command
help
=
'Removes expired links from the database'
...
...
go/go/management/commands/test_expirelinks.py
View file @
70214fcf
...
...
@@ -7,14 +7,15 @@ from django.test import TestCase
# App Imports
from
.expirelinks
import
*
"""
Test cases for the functions in expirelinks
"""
class
ExpireLinksTest
(
TestCase
):
"""
Default test case, does not actually test anything
Test cases for the functions in expirelinks
"""
def
test_Django_Test
(
self
):
"""
Default test case, does not actually test anything
"""
self
.
assertEqual
(
"Hello World!"
,
"Hello World!"
)
go/go/templatetags/go_extras.py
View file @
70214fcf
...
...
@@ -12,17 +12,18 @@ from go.models import RegisteredUser
# filters are registered.
register
=
template
.
Library
()
"""
Helper template function to check if a user is registered.
givenUser: The User object that we are checking to see if they are registered
or not.
"""
@
register
.
filter
def
is_registered
(
givenUser
):
def
is_registered
(
given_user
):
"""
Helper template function to check if a user is registered.
given_user: The User object that we are checking to see if they are registered
or not.
"""
# try getting the RegisteredUser of the current user
try
:
getRegisteredUser
=
RegisteredUser
.
objects
.
get
(
user
=
given
U
ser
)
getRegisteredUser
=
RegisteredUser
.
objects
.
get
(
user
=
given
_u
ser
)
# if it works then the user is registered
return
getRegisteredUser
.
registered
# This should never happen
...
...
@@ -31,19 +32,20 @@ def is_registered(givenUser):
# if they don't exist then they are not registered
return
False
"""
Helper template function to check if a user is approved.
givenUser: The User object that we are checking to see if they are approved
or not.
"""
@
register
.
filter
def
is_approved
(
givenUser
):
def
is_approved
(
given_user
):
"""
Helper template function to check if a user is approved.
given_user: The User object that we are checking to see if they are approved
or not.
"""
# try getting the RegisteredUser of the current user
try
:
get
R
egistered
U
ser
=
RegisteredUser
.
objects
.
get
(
user
=
given
U
ser
)
get
_r
egistered
_u
ser
=
RegisteredUser
.
objects
.
get
(
user
=
given
_u
ser
)
# if they exist, return whether or not they are approved (boolean)
return
get
R
egistered
U
ser
.
approved
return
get
_r
egistered
_u
ser
.
approved
# This should never happen
except
RegisteredUser
.
DoesNotExist
as
ex
:
print
(
ex
)
...
...
go/go/templatetags/test_go_extras.py
View file @
70214fcf
...
...
@@ -9,22 +9,25 @@ from django.contrib.auth.models import User
from
.go_extras
import
is_registered
,
is_approved
from
go.models
import
RegisteredUser
"""
Test cases for the template helper functions in go_extras.py
"""
class
GoExtrasTest
(
TestCase
):
"""
Create a dummy user to be tested against.
Test cases for the template helper functions in go_extras.py
"""
def
setUp
(
self
):
"""
Create a dummy user to be tested against.
"""
User
.
objects
.
create
(
username
=
'dhaynes'
,
password
=
'password'
)
"""
Test the is_registered function to see if it gives correct false answers
"""
def
test_is_registeredFalse
(
self
):
# is_registered ------------------------------------------------------------
def
test_is_registered_false
(
self
):
"""
Test the is_registered function to see if it gives correct false answers
"""
getUser
=
User
.
objects
.
get
(
username
=
'dhaynes'
)
getRegisteredUser
=
RegisteredUser
.
objects
.
get
(
user
=
getUser
)
...
...
@@ -33,10 +36,11 @@ class GoExtrasTest(TestCase):
self
.
assertFalse
(
is_registered
(
getUser
))
"""
Test the is_registered function to see if it gives correct true answers
"""
def
test_is_registeredTrue
(
self
):
def
test_is_registered_true
(
self
):
"""
Test the is_registered function to see if it gives correct true answers
"""
getUser
=
User
.
objects
.
get
(
username
=
'dhaynes'
)
getRegisteredUser
=
RegisteredUser
.
objects
.
get
(
user
=
getUser
)
...
...
@@ -45,10 +49,13 @@ class GoExtrasTest(TestCase):
self
.
assertTrue
(
is_registered
(
getUser
))
"""
Test the is_registered function to see if it gives correct false answers
"""
def
test_is_approvedFalse
(
self
):
# is_approved --------------------------------------------------------------
def
test_is_approved_false
(
self
):
"""
Test the is_registered function to see if it gives correct false answers
"""
getUser
=
User
.
objects
.
get
(
username
=
'dhaynes'
)
getRegisteredUser
=
RegisteredUser
.
objects
.
get
(
user
=
getUser
)
...
...
@@ -58,10 +65,11 @@ class GoExtrasTest(TestCase):
self
.
assertFalse
(
is_approved
(
getUser
))
"""
Test the is_registered function to see if it gives correct true answers
"""
def
test_is_approvedTrue
(
self
):
def
test_is_approved_true
(
self
):
"""
Test the is_registered function to see if it gives correct true answers
"""
getUser
=
User
.
objects
.
get
(
username
=
'dhaynes'
)
getRegisteredUser
=
RegisteredUser
.
objects
.
get
(
user
=
getUser
)
...
...
go/go/test_cas_callbacks.py
View file @
70214fcf
...
...
@@ -7,48 +7,54 @@ from django.test import TestCase
# App Imports
from
go.cas_callbacks
import
pfparse
,
pfinfo
,
create_user
"""
Test cases for the functions in call_callbacks.
"""
class
CasCallbacksTest
(
TestCase
):
"""
Presently enrolled student who has been added to peoplefinder
Test cases for the functions in call_callbacks.
"""
def
test_pf_peoplefinder_method
(
self
):
"""
Presently enrolled student who has been added to peoplefinder
"""
actual
=
pfinfo
(
'dhaynes3'
)
expected
=
[
'David'
,
'Haynes'
]
self
.
assertEqual
(
expected
,
actual
)
"""
Test the parsing method to ensure that first and last names are seperated
accordingly and correctly.
"""
def
test_pfparse_peoplefinder_method
(
self
):
"""
Test the parsing method to ensure that first and last names are seperated
accordingly and correctly.
"""
actual
=
pfparse
(
"Haynes, David M"
)
expected
=
[
'David'
,
'Haynes'
]
self
.
assertEqual
(
expected
,
actual
)
"""
student no longer in peoplefinder, or who hasn't yet been added
"""
def
test_pfinfo_ldap_method
(
self
):
"""
student no longer in peoplefinder, or who hasn't yet been added
"""
actual
=
pfinfo
(
'lfaraone'
)
expected
=
[
'Luke W'
,
'Faraone'
]
self
.
assertEqual
(
expected
,
actual
)
"""
student employees will have their staff info return before their student info
"""
def
test_pfinfo_employee_method
(
self
):
"""
student employees will have their staff info return before their student info
"""
actual
=
pfinfo
(
'nander13'
)
expected
=
[
'Nicholas'
,
'Anderson'
]
self
.
assertEqual
(
expected
,
actual
)
"""
a name not found for either (should never happen, but gracefully handle anyway)
"""
def
test_pfinfo_dne
(
self
):
"""
a name not found for either (should never happen, but gracefully handle anyway)
"""
actual
=
pfinfo
(
'bobama'
)
expected
=
[
''
,
''
]
self
.
assertEqual
(
expected
,
actual
)
go/go/test_forms.py
View file @
70214fcf
...
...
@@ -7,25 +7,26 @@ from django.test import TestCase
# App Imports
from
go.forms
import
*
"""
Test cases for the URL form
"""
class
URLFormTest
(
TestCase
):
"""
Default test case, does not actually test anything
Test cases for the URL form
"""
def
test_Django_Test
(
self
):
def
test_django_test
(
self
):
"""
Default test case, does not actually test anything
"""
self
.
assertEqual
(
"Hello World!"
,
"Hello World!"
)
"""
Test cases for the Signup form
"""
class
SignupForm
(
TestCase
):
"""
Default test case, does not actually test anything
Test cases for the Signup form
"""
def
test_Django_Test
(
self
):
def
test_django_test
(
self
):
"""
Default test case, does not actually test anything
"""
self
.
assertEqual
(
"Hello World!"
,
"Hello World!"
)
go/go/test_models.py
View file @
70214fcf
...
...
@@ -21,7 +21,7 @@ class RegisteredUserTest(TestCase):
User
.
objects
.
create
(
username
=
'dhaynes'
,
password
=
'password'
)
# User
# User
---------------------------------------------------------------------
def
test_registereduser_creation
(
self
):
"""
...
...
@@ -32,11 +32,11 @@ class RegisteredUserTest(TestCase):
getRegisteredUser
=
RegisteredUser
.
objects
.
get
(
user
=
getUser
)
self
.
assertTrue
(
getRegisteredUser
)
# full_name
# full_name
----------------------------------------------------------------
# organization
# organization
-------------------------------------------------------------
# description
# description
--------------------------------------------------------------
def
test_description_blank
(
self
):
"""
...
...
@@ -58,13 +58,13 @@ class RegisteredUserTest(TestCase):
self
.
assertEqual
(
getRegisteredUser
.
description
,
"We're going to build a big beautiful testcase"
)
# registered
# registered
---------------------------------------------------------------
# approved
# approved
-----------------------------------------------------------------
# blocked
# blocked
------------------------------------------------------------------
# __str__
# __str__
------------------------------------------------------------------
def
test_check_str
(
self
):
"""
...
...
@@ -83,22 +83,22 @@ class URLTest(TestCase):
Test cases for the URL Model
"""
# owner
# owner
--------------------------------------------------------------------
# date_created
# date_created
-------------------------------------------------------------
# target
# target
-------------------------------------------------------------------
# short
# short
--------------------------------------------------------------------
# clicks
# clicks
-------------------------------------------------------------------
# qrclicks
# qrclicks
-----------------------------------------------------------------
# socialclicks
# socialclicks
-------------------------------------------------------------
# expires
# expires
------------------------------------------------------------------
# __str__
# __str__
------------------------------------------------------------------
# generate_valid_short
\ No newline at end of file
# generate_valid_short -----------------------------------------------------
\ No newline at end of file
go/go/test_views.py
View file @
70214fcf
...
...
@@ -7,79 +7,86 @@ from django.test import TestCase
# App Imports
from
go.views
import
*
"""
Test cases for the index view
"""
class
IndexTest
(
TestCase
):
"""
Default test case, does not actually test anything
Test cases for the index view
"""
def
test_Django_Test
(
self
):
def
test_django_test
(
self
):
"""
Default test case, does not actually test anything
"""
self
.
assertEqual
(
"Hello World!"
,
"Hello World!"
)
"""
Test cases for the "view" view
"""
class
ViewTest
(
TestCase
):
"""
Default test case, does not actually test anything
Test cases for the "view" view