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
...
@@ -9,26 +9,29 @@ from django.contrib.auth.models import User
# App Imports
# App Imports
from
go.models
import
URL
,
RegisteredUser
from
go.models
import
URL
,
RegisteredUser
"""
Define what attributes display in the URL Admin
"""
class
URLAdmin
(
admin
.
ModelAdmin
):
class
URLAdmin
(
admin
.
ModelAdmin
):
"""
Define what attributes display in the URL Admin
"""
list_display
=
(
"target"
,
"short"
,
"owner"
,
"clicks"
,
"date_created"
,
"expires"
)
list_display
=
(
"target"
,
"short"
,
"owner"
,
"clicks"
,
"date_created"
,
"expires"
)
# Register URLAdmin
# Register URLAdmin
admin
.
site
.
register
(
URL
,
URLAdmin
)
admin
.
site
.
register
(
URL
,
URLAdmin
)
"""
Define an inline admin descriptor for User model
"""
class
RegisteredUserInline
(
admin
.
StackedInline
):
class
RegisteredUserInline
(
admin
.
StackedInline
):
"""
Define an inline admin descriptor for User model
"""
model
=
RegisteredUser
model
=
RegisteredUser
can_delete
=
False
can_delete
=
False
"""
Define a new User admin
"""
class
UserAdmin
(
UserAdmin
):
class
UserAdmin
(
UserAdmin
):
"""
Define a new User admin
"""
# see above class that we defined
# see above class that we defined
inlines
=
(
RegisteredUserInline
,
)
inlines
=
(
RegisteredUserInline
,
)
...
...
go/go/forms.py
View file @
70214fcf
# Future Imports
# Future Imports
from
__future__
import
unicode_literals
,
absolute_import
,
print_function
,
division
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
# Django Imports
from
django
import
forms
from
django
import
forms
from
django.core.exceptions
import
ValidationError
from
django.core.exceptions
import
ValidationError
...
@@ -15,13 +19,11 @@ from crispy_forms.helper import FormHelper
...
@@ -15,13 +19,11 @@ from crispy_forms.helper import FormHelper
from
crispy_forms.layout
import
Layout
,
Fieldset
,
Submit
,
HTML
,
Div
,
Field
from
crispy_forms.layout
import
Layout
,
Fieldset
,
Submit
,
HTML
,
Div
,
Field
from
crispy_forms.bootstrap
import
StrictButton
,
PrependedText
,
Accordion
,
AccordionGroup
from
crispy_forms.bootstrap
import
StrictButton
,
PrependedText
,
Accordion
,
AccordionGroup
from
bootstrap3_datetime.widgets
import
DateTimePicker
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
):
class
URLForm
(
forms
.
ModelForm
):
"""
The form that is used in URL creation.
"""
# Prevent redirect loop links
# Prevent redirect loop links
def
clean_target
(
self
):
def
clean_target
(
self
):
...
@@ -34,7 +36,7 @@ class URLForm(forms.ModelForm):
...
@@ -34,7 +36,7 @@ class URLForm(forms.ModelForm):
except
urllib
.
error
.
URLError
as
e
:
except
urllib
.
error
.
URLError
as
e
:
# to permit users to enter sites that return most errors, but
# to permit users to enter sites that return most errors, but
# prevent them from entering sites that result in an HTTP 300 error
# 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"
)
raise
ValidationError
(
"Link results in a 300 error"
)
else
:
else
:
final_url
=
""
final_url
=
""
...
@@ -67,12 +69,12 @@ class URLForm(forms.ModelForm):
...
@@ -67,12 +69,12 @@ class URLForm(forms.ModelForm):
# Custom short-url field with validators.
# Custom short-url field with validators.
short
=
forms
.
SlugField
(
short
=
forms
.
SlugField
(
required
=
False
,
required
=
False
,
label
=
'Short URL (Optional)'
,
label
=
'Short URL (Optional)'
,
widget
=
forms
.
TextInput
(),
widget
=
forms
.
TextInput
(),
validators
=
[
unique_short
],
validators
=
[
unique_short
],
max_length
=
20
,
max_length
=
20
,
min_length
=
3
,
min_length
=
3
,
)
)
# define some string date standards
# define some string date standards
...
@@ -93,11 +95,11 @@ class URLForm(forms.ModelForm):
...
@@ -93,11 +95,11 @@ class URLForm(forms.ModelForm):
# Add preset expiration choices.
# Add preset expiration choices.
expires
=
forms
.
ChoiceField
(
expires
=
forms
.
ChoiceField
(
required
=
True
,
required
=
True
,
label
=
'Expiration (Required)'
,
label
=
'Expiration (Required)'
,
choices
=
EXPIRATION_CHOICES
,
choices
=
EXPIRATION_CHOICES
,
initial
=
NEVER
,
initial
=
NEVER
,
widget
=
forms
.
RadioSelect
(),
widget
=
forms
.
RadioSelect
(),
)
)
# Check if the selected date is a valid date
# Check if the selected date is a valid date
...
@@ -112,12 +114,12 @@ class URLForm(forms.ModelForm):
...
@@ -112,12 +114,12 @@ class URLForm(forms.ModelForm):
# Add a custom expiration choice.
# Add a custom expiration choice.
expires_custom
=
forms
.
DateTimeField
(
expires_custom
=
forms
.
DateTimeField
(
required
=
False
,
required
=
False
,
label
=
'Custom Date'
,
label
=
'Custom Date'
,
input_formats
=
[
'%m-%d-%Y'
],
input_formats
=
[
'%m-%d-%Y'
],
validators
=
[
valid_date
],
validators
=
[
valid_date
],
initial
=
lambda
:
datetime
.
now
()
+
timedelta
(
days
=
1
),
initial
=
lambda
:
datetime
.
now
()
+
timedelta
(
days
=
1
),
widget
=
DateTimePicker
(
widget
=
DateTimePicker
(
options
=
{
options
=
{
"format"
:
"MM-DD-YYYY"
,
"format"
:
"MM-DD-YYYY"
,
"pickTime"
:
False
,
"pickTime"
:
False
,
...
@@ -154,9 +156,9 @@ class URLForm(forms.ModelForm):
...
@@ -154,9 +156,9 @@ class URLForm(forms.ModelForm):
<h4>Paste the URL you would like to shorten:</h4>
<h4>Paste the URL you would like to shorten:</h4>
<br />"""
),
<br />"""
),
'target'
,
'target'
,
style
=
"background: rgb(#F6F6F6);"
),
style
=
"background: rgb(#F6F6F6);"
),
active
=
True
,
active
=
True
,
template
=
'crispy/accordian-group.html'
),
template
=
'crispy/accordian-group.html'
),
# Step 2: Short URL
# Step 2: Short URL
AccordionGroup
(
'Step 2: Short URL'
,
AccordionGroup
(
'Step 2: Short URL'
,
...
@@ -166,9 +168,9 @@ class URLForm(forms.ModelForm):
...
@@ -166,9 +168,9 @@ class URLForm(forms.ModelForm):
<br />"""
),
<br />"""
),
PrependedText
(
PrependedText
(
'short'
,
'https://go.gmu.edu/'
,
template
=
'crispy/customPrepended.html'
),
'short'
,
'https://go.gmu.edu/'
,
template
=
'crispy/customPrepended.html'
),
style
=
"background: rgb(#F6F6F6);"
),
style
=
"background: rgb(#F6F6F6);"
),
active
=
True
,
active
=
True
,
template
=
'crispy/accordian-group.html'
,),
template
=
'crispy/accordian-group.html'
,),
# Step 3: Expiration
# Step 3: Expiration
AccordionGroup
(
'Step 3: URL Expiration'
,
AccordionGroup
(
'Step 3: URL Expiration'
,
...
@@ -178,12 +180,12 @@ class URLForm(forms.ModelForm):
...
@@ -178,12 +180,12 @@ class URLForm(forms.ModelForm):
<br />"""
),
<br />"""
),
'expires'
,
'expires'
,
Field
(
'expires_custom'
,
template
=
"crispy/customDateField.html"
),
Field
(
'expires_custom'
,
template
=
"crispy/customDateField.html"
),
style
=
"background: rgb(#F6F6F6);"
),
style
=
"background: rgb(#F6F6F6);"
),
active
=
True
,
active
=
True
,
template
=
'crispy/accordian-group.html'
),
template
=
'crispy/accordian-group.html'
),
# FIN
# FIN
template
=
'crispy/accordian.html'
),
template
=
'crispy/accordian.html'
),
#######################
#######################
HTML
(
"""
HTML
(
"""
<br />"""
),
<br />"""
),
...
@@ -194,42 +196,44 @@ class URLForm(forms.ModelForm):
...
@@ -194,42 +196,44 @@ class URLForm(forms.ModelForm):
# what model this form is for
# what model this form is for
model
=
URL
model
=
URL
# what attributes are included
# 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
):
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
# The full name of the RegisteredUser
full_name
=
forms
.
CharField
(
full_name
=
forms
.
CharField
(
required
=
True
,
required
=
True
,
label
=
'Full Name (Required)'
,
label
=
'Full Name (Required)'
,
max_length
=
100
,
max_length
=
100
,
widget
=
forms
.
TextInput
(),
widget
=
forms
.
TextInput
(),
)
)
# The RegisteredUser's chosen organization
# The RegisteredUser's chosen organization
organization
=
forms
.
CharField
(
organization
=
forms
.
CharField
(
required
=
True
,
required
=
True
,
label
=
'Organization (Required)'
,
label
=
'Organization (Required)'
,
max_length
=
100
,
max_length
=
100
,
widget
=
forms
.
TextInput
(),
widget
=
forms
.
TextInput
(),
)
)
# The RegisteredUser's reason for signing up to us Go
# The RegisteredUser's reason for signing up to us Go
description
=
forms
.
CharField
(
description
=
forms
.
CharField
(
required
=
False
,
required
=
False
,
label
=
'Description (Optional)'
,
label
=
'Description (Optional)'
,
max_length
=
200
,
max_length
=
200
,
widget
=
forms
.
Textarea
(),
widget
=
forms
.
Textarea
(),
)
)
# A user becomes registered when they agree to the TOS
# A user becomes registered when they agree to the TOS
registered
=
forms
.
BooleanField
(
registered
=
forms
.
BooleanField
(
required
=
True
,
required
=
True
,
# ***Need to replace lower url with production URL*** ie. go.gmu.edu/about#terms
# ***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
# on initialization of the form, crispy forms renders this layout
...
@@ -262,4 +266,4 @@ class SignupForm(forms.ModelForm):
...
@@ -262,4 +266,4 @@ class SignupForm(forms.ModelForm):
# what model this form is for
# what model this form is for
model
=
RegisteredUser
model
=
RegisteredUser
# what attributes are included
# 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
...
@@ -10,6 +10,10 @@ from go.models import URL
# Define a new custom django-admin command
# Define a new custom django-admin command
class
Command
(
BaseCommand
):
class
Command
(
BaseCommand
):
"""
Remove expired links from the database
"""
# Define help text for this command
# Define help text for this command
help
=
'Removes expired links from the database'
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
...
@@ -7,14 +7,15 @@ from django.test import TestCase
# App Imports
# App Imports
from
.expirelinks
import
*
from
.expirelinks
import
*
"""
Test cases for the functions in expirelinks
"""
class
ExpireLinksTest
(
TestCase
):
class
ExpireLinksTest
(
TestCase
):
"""
"""
Default test case, does not actually test anything
Test cases for the functions in expirelinks
"""
"""
def
test_Django_Test
(
self
):
def
test_Django_Test
(
self
):
"""
Default test case, does not actually test anything
"""
self
.
assertEqual
(
"Hello World!"
,
"Hello World!"
)
self
.
assertEqual
(
"Hello World!"
,
"Hello World!"
)
go/go/templatetags/go_extras.py
View file @
70214fcf
...
@@ -12,17 +12,18 @@ from go.models import RegisteredUser
...
@@ -12,17 +12,18 @@ from go.models import RegisteredUser
# filters are registered.
# filters are registered.
register
=
template
.
Library
()
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
@
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 getting the RegisteredUser of the current user
try
:
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
# if it works then the user is registered
return
getRegisteredUser
.
registered
return
getRegisteredUser
.
registered
# This should never happen
# This should never happen
...
@@ -31,19 +32,20 @@ def is_registered(givenUser):
...
@@ -31,19 +32,20 @@ def is_registered(givenUser):
# if they don't exist then they are not registered
# if they don't exist then they are not registered
return
False
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
@
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 getting the RegisteredUser of the current user
try
:
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)
# 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
# This should never happen
except
RegisteredUser
.
DoesNotExist
as
ex
:
except
RegisteredUser
.
DoesNotExist
as
ex
:
print
(
ex
)
print
(
ex
)
...
...
go/go/templatetags/test_go_extras.py
View file @
70214fcf
...
@@ -9,22 +9,25 @@ from django.contrib.auth.models import User
...
@@ -9,22 +9,25 @@ from django.contrib.auth.models import User
from
.go_extras
import
is_registered
,
is_approved
from
.go_extras
import
is_registered
,
is_approved
from
go.models
import
RegisteredUser
from
go.models
import
RegisteredUser
"""
Test cases for the template helper functions in go_extras.py
"""
class
GoExtrasTest
(
TestCase
):
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
):
def
setUp
(
self
):
"""
Create a dummy user to be tested against.
"""
User
.
objects
.
create
(
username
=
'dhaynes'
,
password
=
'password'
)
User
.
objects
.
create
(
username
=
'dhaynes'
,
password
=
'password'
)
"""
# is_registered ------------------------------------------------------------
Test the is_registered function to see if it gives correct false answers
"""
def
test_is_registered_false
(
self
):
def
test_is_registeredFalse
(
self
):
"""
Test the is_registered function to see if it gives correct false answers
"""
getUser
=
User
.
objects
.
get
(
username
=
'dhaynes'
)
getUser
=
User
.
objects
.
get
(
username
=
'dhaynes'
)
getRegisteredUser
=
RegisteredUser
.
objects
.
get
(
user
=
getUser
)
getRegisteredUser
=
RegisteredUser
.
objects
.
get
(
user
=
getUser
)
...
@@ -33,10 +36,11 @@ class GoExtrasTest(TestCase):
...
@@ -33,10 +36,11 @@ class GoExtrasTest(TestCase):
self
.
assertFalse
(
is_registered
(
getUser
))
self
.
assertFalse
(
is_registered
(
getUser
))
"""
def
test_is_registered_true
(
self
):
Test the is_registered function to see if it gives correct true answers
"""
"""
Test the is_registered function to see if it gives correct true answers
def
test_is_registeredTrue
(
self
):
"""
getUser
=
User
.
objects
.
get
(
username
=
'dhaynes'
)
getUser
=
User
.
objects
.
get
(
username
=
'dhaynes'
)
getRegisteredUser
=
RegisteredUser
.
objects
.
get
(
user
=
getUser
)
getRegisteredUser
=
RegisteredUser
.
objects
.
get
(
user
=
getUser
)
...
@@ -45,10 +49,13 @@ class GoExtrasTest(TestCase):
...
@@ -45,10 +49,13 @@ class GoExtrasTest(TestCase):
self
.
assertTrue
(
is_registered
(
getUser
))
self
.
assertTrue
(
is_registered
(
getUser
))
"""
# is_approved --------------------------------------------------------------
Test the is_registered function to see if it gives correct false answers
"""
def
test_is_approved_false
(
self
):
def
test_is_approvedFalse
(
self
):
"""
Test the is_registered function to see if it gives correct false answers
"""
getUser
=
User
.
objects
.
get
(
username
=
'dhaynes'
)
getUser
=
User
.
objects
.
get
(
username
=
'dhaynes'
)
getRegisteredUser
=
RegisteredUser
.
objects
.
get
(
user
=
getUser
)
getRegisteredUser
=
RegisteredUser
.
objects
.
get
(
user
=
getUser
)
...
@@ -58,10 +65,11 @@ class GoExtrasTest(TestCase):
...
@@ -58,10 +65,11 @@ class GoExtrasTest(TestCase):
self
.
assertFalse
(
is_approved
(
getUser
))
self
.
assertFalse
(
is_approved
(
getUser
))
"""
def
test_is_approved_true
(
self
):
Test the is_registered function to see if it gives correct true answers
"""
"""
Test the is_registered function to see if it gives correct true answers
def
test_is_approvedTrue
(
self
):
"""
getUser
=
User
.
objects
.
get
(
username
=
'dhaynes'
)
getUser
=
User
.
objects
.
get
(
username
=
'dhaynes'
)
getRegisteredUser
=
RegisteredUser
.
objects
.
get
(
user
=
getUser
)
getRegisteredUser
=
RegisteredUser
.
objects
.
get
(
user
=
getUser
)
...
...
go/go/test_cas_callbacks.py
View file @
70214fcf
...
@@ -7,48 +7,54 @@ from django.test import TestCase
...
@@ -7,48 +7,54 @@ from django.test import TestCase
# App Imports
# App Imports
from
go.cas_callbacks
import
pfparse
,
pfinfo
,
create_user
from
go.cas_callbacks
import
pfparse
,
pfinfo
,
create_user
"""
Test cases for the functions in call_callbacks.
"""
class
CasCallbacksTest
(
TestCase
):
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
):
def
test_pf_peoplefinder_method
(
self
):
"""
Presently enrolled student who has been added to peoplefinder
"""
actual
=
pfinfo
(
'dhaynes3'
)
actual
=
pfinfo
(
'dhaynes3'
)
expected
=
[
'David'
,
'Haynes'
]
expected
=
[
'David'
,
'Haynes'
]
self
.
assertEqual
(
expected
,
actual
)
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
):
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"
)
actual
=
pfparse
(
"Haynes, David M"
)
expected
=
[
'David'
,
'Haynes'
]
expected
=
[
'David'
,
'Haynes'
]
self
.
assertEqual
(
expected
,
actual
)
self
.
assertEqual
(
expected
,
actual
)
"""
student no longer in peoplefinder, or who hasn't yet been added
"""
def
test_pfinfo_ldap_method
(
self
):
def
test_pfinfo_ldap_method
(
self
):
"""
student no longer in peoplefinder, or who hasn't yet been added
"""
actual
=
pfinfo
(
'lfaraone'
)
actual
=
pfinfo
(
'lfaraone'
)
expected
=
[
'Luke W'
,
'Faraone'
]
expected
=
[
'Luke W'
,
'Faraone'
]
self
.
assertEqual
(
expected
,
actual
)
self
.
assertEqual
(
expected
,
actual
)
"""
student employees will have their staff info return before their student info
"""
def
test_pfinfo_employee_method
(
self
):
def
test_pfinfo_employee_method
(
self
):
"""
student employees will have their staff info return before their student info
"""
actual
=
pfinfo
(
'nander13'
)
actual
=
pfinfo
(
'nander13'
)
expected
=
[
'Nicholas'
,
'Anderson'
]
expected
=
[
'Nicholas'
,
'Anderson'
]
self
.
assertEqual
(
expected
,
actual
)
self
.
assertEqual
(
expected
,
actual
)
"""
a name not found for either (should never happen, but gracefully handle anyway)
"""
def
test_pfinfo_dne
(
self
):
def
test_pfinfo_dne
(
self
):
"""
a name not found for either (should never happen, but gracefully handle anyway)