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
2fb09317
Unverified
Commit
2fb09317
authored
Mar 20, 2017
by
David Haynes
Browse files
Pendatic fix for comments
- one day I'll make up my mind on style
parent
46c5a592
Pipeline
#1105
passed with stage
in 1 minute and 1 second
Changes
11
Pipelines
1
Show whitespace changes
Inline
Side-by-side
go/go/admin.py
View file @
2fb09317
go/go/cas_callbacks.py
View file @
2fb09317
...
...
@@ -5,13 +5,14 @@ from __future__ import unicode_literals, absolute_import, print_function, divisi
from
django.contrib.auth.models
import
User
from
django.conf
import
settings
# th
ird party i
mports
#
O
th
er I
mports
import
requests
"""
parse what peoplefinder sends back to us and make a list out of it
"""
def
pfparse
(
pf_name_result
):
"""
Parse what peoplefinder sends back to us and make a list out of it
"""
# name comes in format of Anderson, Nicholas J
name_list
=
pf_name_result
.
split
(
','
)
# there's random whitespace with the first name
...
...
@@ -27,10 +28,11 @@ def pfparse(pf_name_result):
new_name_list
=
[
first_name
,
name_list
[
0
]]
return
new_name_list
"""
get information from peoplefinder
"""
def
pfinfo
(
uname
):
"""
Get information from peoplefinder
"""
base_url
=
settings
.
PF_URL
url
=
base_url
+
"basic/all/"
+
str
(
uname
)
try
:
...
...
@@ -65,16 +67,16 @@ def pfinfo(uname):
# if the name is not in peoplefinder, return empty first and last name
except
IndexError
as
ex
:
print
(
"Name not found in peoplefinder."
)
return
[
''
,
''
]
return
[
''
,
''
]
except
Exception
as
ex
:
print
(
"Unknown peoplefinder error:"
,
ex
)
print
(
"Returning empty user info tuple."
)
return
[
''
,
''
]
"""
create a django user based off of the peoplefinder info we parsed earlier
"""
def
create_user
(
tree
):
"""
Create a django user based off of the peoplefinder info we parsed earlier
"""
print
(
"Parsing CAS information."
)
try
:
...
...
go/go/forms.py
View file @
2fb09317
...
...
@@ -2,7 +2,7 @@
from
__future__
import
unicode_literals
,
absolute_import
,
print_function
,
division
# Python stdlib Imports
from
datetime
import
date
,
datetime
,
timedelta
from
datetime
import
datetime
,
timedelta
from
six.moves
import
urllib
# Django Imports
...
...
@@ -25,8 +25,13 @@ class URLForm(forms.ModelForm):
The form that is used in URL creation.
"""
# Prevent redirect loop links
# target -------------------------------------------------------------------
def
clean_target
(
self
):
"""
Prevent redirect loop links
"""
# get the entered target link
target
=
self
.
cleaned_data
.
get
(
'target'
)
try
:
...
...
@@ -57,13 +62,19 @@ class URLForm(forms.ModelForm):
})
)
# Check to make sure the short url has not been used
# short --------------------------------------------------------------------
def
unique_short
(
value
):
"""
Check to make sure the short url has not been used
"""
try
:
# if we're able to get a URL with the same short url
URL
.
objects
.
get
(
short__iexact
=
value
)
except
URL
.
DoesNotExist
as
ex
:
return
# then raise a ValidationError
raise
ValidationError
(
'Short url already exists.'
)
...
...
@@ -77,14 +88,16 @@ class URLForm(forms.ModelForm):
min_length
=
3
,
)
# define some string date standards
# expires ------------------------------------------------------------------
# Define some string date standards
DAY
=
'1 Day'
WEEK
=
'1 Week'
MONTH
=
'1 Month'
CUSTOM
=
'Custom Date'
NEVER
=
'Never'
#
d
efine a tuple of string date standards to be used as our date choices
#
D
efine a tuple of string date standards to be used as our date choices
EXPIRATION_CHOICES
=
(
(
DAY
,
DAY
),
(
WEEK
,
WEEK
),
...
...
@@ -102,8 +115,11 @@ class URLForm(forms.ModelForm):
widget
=
forms
.
RadioSelect
(),
)
# Check if the selected date is a valid date
def
valid_date
(
value
):
"""
Check if the selected date is a valid date
"""
# a valid date is one that is greater than today
if
value
>
timezone
.
now
():
return
...
...
@@ -130,8 +146,11 @@ class URLForm(forms.ModelForm):
)
)
# on initialization of the form, crispy forms renders this layout
def
__init__
(
self
,
*
args
,
**
kwargs
):
"""
On initialization of the form, crispy forms renders this layout
"""
# Grab that host info
self
.
host
=
kwargs
.
pop
(
'host'
,
None
)
super
(
URLForm
,
self
).
__init__
(
*
args
,
**
kwargs
)
...
...
@@ -191,8 +210,11 @@ class URLForm(forms.ModelForm):
<br />"""
),
StrictButton
(
'Shorten'
,
css_class
=
"btn btn-primary btn-md col-md-4"
,
type
=
'submit'
)))
# metadata about this ModelForm
class
Meta
:
"""
Metadata about this ModelForm
"""
# what model this form is for
model
=
URL
# what attributes are included
...
...
@@ -230,15 +252,20 @@ class SignupForm(forms.ModelForm):
# 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
# ***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>?'
),
)
# on initialization of the form, crispy forms renders this layout
def
__init__
(
self
,
request
,
*
args
,
**
kwargs
):
# Necessary to call request in forms.py, is otherwise restricted to views.py and models.py
"""
On initialization of the form, crispy forms renders this layout
"""
# Necessary to call request in forms.py, is otherwise restricted to
# views.py and models.py
self
.
request
=
request
super
(
SignupForm
,
self
).
__init__
(
*
args
,
**
kwargs
)
self
.
helper
=
FormHelper
()
...
...
@@ -261,8 +288,11 @@ class SignupForm(forms.ModelForm):
StrictButton
(
'Submit'
,
css_class
=
'btn btn-primary btn-md col-md-4'
,
type
=
'submit'
),
css_class
=
'col-md-6'
)))
# metadata about this ModelForm
class
Meta
:
"""
Metadata about this ModelForm
"""
# what model this form is for
model
=
RegisteredUser
# what attributes are included
...
...
go/go/models.py
View file @
2fb09317
...
...
@@ -53,15 +53,21 @@ class RegisteredUser(models.Model):
# Is this User Blocked?
blocked
=
models
.
BooleanField
(
default
=
False
)
# str(RegisteredUser)
def
__str__
(
self
):
"""
str(RegisteredUser)
"""
return
'<Registered User: %s - Approval Status: %s>'
%
(
self
.
user
,
self
.
approved
)
# When a post_save is called on a User object (and it is newly created), this is
# called to create an associated RegisteredUser
@
receiver
(
post_save
,
sender
=
User
)
def
handle_regUser_creation
(
sender
,
instance
,
created
,
**
kwargs
):
"""
When a post_save is called on a User object (and it is newly created), this
is called to create an associated RegisteredUser
"""
if
created
:
RegisteredUser
.
objects
.
create
(
user
=
instance
)
...
...
@@ -94,19 +100,27 @@ class URL(models.Model):
# does this Go link expire on a certain date
expires
=
models
.
DateTimeField
(
blank
=
True
,
null
=
True
)
# print(URL)
def
__str__
(
self
):
"""
print(URL)
"""
return
'<Owner: %s - Target URL: %s>'
%
(
self
.
owner
.
user
,
self
.
target
)
# metadata for URL's
class
Meta
:
"""
metadata for URLs
"""
# they should be ordered by their short links
ordering
=
[
'short'
]
# legacy method to ensure that generated short URL's are valid
# should be updated to be simpler
@
staticmethod
def
generate_valid_short
():
"""
legacy method to ensure that generated short URL's are valid
should be updated to be simpler
"""
if
cache
.
get
(
"hashids_counter"
)
is
None
:
cache
.
set
(
"hashids_counter"
,
URL
.
objects
.
count
())
cache
.
incr
(
"hashids_counter"
)
...
...
go/go/test_cas_callbacks.py
View file @
2fb09317
...
...
@@ -5,7 +5,7 @@ from __future__ import unicode_literals, absolute_import, print_function, divisi
from
django.test
import
TestCase
# App Imports
from
go.cas_callbacks
import
pfparse
,
pfinfo
,
create_user
from
go.cas_callbacks
import
pfparse
,
pfinfo
class
CasCallbacksTest
(
TestCase
):
"""
...
...
go/go/test_forms.py
View file @
2fb09317
...
...
@@ -5,7 +5,7 @@ from __future__ import unicode_literals, absolute_import, print_function, divisi
from
django.test
import
TestCase
# App Imports
from
go.forms
import
*
from
go.forms
import
URLForm
,
SignupForm
class
URLFormTest
(
TestCase
):
"""
...
...
go/go/test_models.py
View file @
2fb09317
go/go/test_views.py
View file @
2fb09317
...
...
@@ -5,7 +5,7 @@ from __future__ import unicode_literals, absolute_import, print_function, divisi
from
django.test
import
TestCase
# App Imports
from
go.views
import
*
from
go.views
import
index
,
view
,
my_links
,
delete
,
signup
,
redirection
,
useradmin
class
IndexTest
(
TestCase
):
"""
...
...
go/go/views.py
View file @
2fb09317
# Future Imports
from
__future__
import
unicode_literals
,
absolute_import
,
print_function
,
division
# Python stdlib imports
from
datetime
import
timedelta
# Django Imports
from
django.conf
import
settings
from
django.http
import
HttpResponseServerError
# Http404
...
...
@@ -12,15 +15,14 @@ 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
# Other imports
from
ratelimit.decorators
import
ratelimit
# App Imports
from
go.models
import
URL
,
RegisteredUser
from
go.forms
import
URLForm
,
SignupForm
# Other Imports
from
datetime
import
timedelta
def
index
(
request
):
"""
This view handles the homepage that the user is presented with when
...
...
@@ -79,12 +81,12 @@ def index(request):
'form'
:
url_form
,
})
#
r
ate limits are completely arbitrary
#
R
ate limits are completely arbitrary
@
ratelimit
(
key
=
'user'
,
rate
=
'3/m'
,
method
=
'POST'
,
block
=
True
)
@
ratelimit
(
key
=
'user'
,
rate
=
'25/d'
,
method
=
'POST'
,
block
=
True
)
def
post
(
request
,
url_form
):
"""
f
unction that handles POST requests for the URL creation ProcessLookupError
F
unction that handles POST requests for the URL creation ProcessLookupError
"""
# We don't commit the url object yet because we need to add its
...
...
go/settings/test_urls.py
View file @
2fb09317
...
...
@@ -3,157 +3,174 @@ from __future__ import unicode_literals, absolute_import, print_function, divisi
# Django Imports
from
django.test
import
TestCase
from
django.urls
import
reverse
,
resolve
# App Imports
from
.urls
import
*
from
django.urls
import
reverse
# https://stackoverflow.com/questions/18987051/how-do-i-unit-test-django-urls
"""
Test cases for the urls
"""
class
UrlsTest
(
TestCase
):
"""
Test cases for the urls
"""
def
test_index_reverse
(
self
):
"""
/ - Homepage url.
"""
def
test_index_reverse
(
self
):
url
=
reverse
(
'index'
)
self
.
assertEqual
(
url
,
'/'
)
def
test_view_reverse_chars
(
self
):
"""
/view/<short> - View URL data.
"""
def
test_view_reverse_chars
(
self
):
url
=
reverse
(
'view'
,
args
=
[
'dhaynes'
])
self
.
assertEqual
(
url
,
'/view/dhaynes'
)
def
test_view_reverse_ints
(
self
):
"""
/view/<short> - View URL data.
"""
def
test_view_reverse_ints
(
self
):
url
=
reverse
(
'view'
,
args
=
[
'123456789'
])
self
.
assertEqual
(
url
,
'/view/123456789'
)
def
test_view_reverse_chars_ints
(
self
):
"""
/view/<short> - View URL data.
"""
def
test_view_reverse_chars_ints
(
self
):
url
=
reverse
(
'view'
,
args
=
[
'dhaynes123'
])
self
.
assertEqual
(
url
,
'/view/dhaynes123'
)
def
test_view_reverse_full_slug
(
self
):
"""
/view/<short> - View URL data.
"""
def
test_view_reverse_full_slug
(
self
):
url
=
reverse
(
'view'
,
args
=
[
'dhaynes123_-'
])
self
.
assertEqual
(
url
,
'/view/dhaynes123_-'
)
def
test_about_reverse
(
self
):
"""
/about - About page.
"""
def
test_about_reverse
(
self
):
url
=
reverse
(
'about'
)
self
.
assertEqual
(
url
,
'/about'
)
def
test_signup_reverse
(
self
):
"""
/signup - Signup page for access.
"""
def
test_signup_reverse
(
self
):
url
=
reverse
(
'signup'
)
self
.
assertEqual
(
url
,
'/signup'
)
def
test_my_links_reverse
(
self
):
"""
/myLinks - My-Links page, view and review links.
"""
def
test_my_links_reverse
(
self
):
url
=
reverse
(
'my_links'
)
self
.
assertEqual
(
url
,
'/myLinks'
)
def
test_delete_reverse_chars
(
self
):
"""
/delete/<short> - Delete a link, no content display.
"""
def
test_delete_reverse_chars
(
self
):
url
=
reverse
(
'delete'
,
args
=
[
'dhaynes'
])
self
.
assertEqual
(
url
,
'/delete/dhaynes'
)
def
test_delete_reverse_ints
(
self
):
"""
/delete/<short> - Delete a link, no content display.
"""
def
test_delete_reverse_ints
(
self
):
url
=
reverse
(
'delete'
,
args
=
[
'123456789'
])
self
.
assertEqual
(
url
,
'/delete/123456789'
)
def
test_delete_reverse_chars_ints
(
self
):
"""
/delete/<short> - Delete a link, no content display.
"""
def
test_delete_reverse_chars_ints
(
self
):
url
=
reverse
(
'delete'
,
args
=
[
'dhaynes123'
])
self
.
assertEqual
(
url
,
'/delete/dhaynes123'
)
def
test_delete_reverse_full_slug
(
self
):
"""
/delete/<short> - Delete a link, no content display.
"""
def
test_delete_reverse_full_slug
(
self
):
url
=
reverse
(
'delete'
,
args
=
[
'dhaynes123_-'
])
self
.
assertEqual
(
url
,
'/delete/dhaynes123_-'
)
def
test_registered_reverse
(
self
):
"""
/registered - registration complete page
"""
def
test_registered_reverse
(
self
):
url
=
reverse
(
'registered'
)
self
.
assertEqual
(
url
,
'/registered'
)
# The /admin URL is not tested as it is never resolves in source and generally
# Django yells at you if the admin page breaks
def
test_useradmin
(
self
):
"""
/useradmin - user approval interface
"""
def
test_useradmin
(
self
):
url
=
reverse
(
'useradmin'
)
self
.
assertEqual
(
url
,
'/useradmin'
)
def
test_useradmin
(
self
):
"""
/login - login portal
"""
def
test_useradmin
(
self
):
url
=
reverse
(
'go_login'
)
self
.
assertEqual
(
url
,
'/login'
)
def
test_useradmin
(
self
):
"""
/logout - logout portal
"""
def
test_useradmin
(
self
):
url
=
reverse
(
'go_logout'
)
self
.
assertEqual
(
url
,
'/logout'
)
def
test_delete_chars
(
self
):
"""
/<short> - Redirect to a go link.
"""
def
test_delete_chars
(
self
):
url
=
reverse
(
'redirection'
,
args
=
[
'dhaynes'
])
self
.
assertEqual
(
url
,
'/dhaynes'
)
def
test_delete_ints
(
self
):
"""
/<short> - Redirect to a go link.
"""
def
test_delete_ints
(
self
):
url
=
reverse
(
'redirection'
,
args
=
[
'123456789'
])
self
.
assertEqual
(
url
,
'/123456789'
)
def
test_delete_chars_ints
(
self
):
"""
/<short> - Redirect to a go link.
"""
def
test_delete_chars_ints
(
self
):
url
=
reverse
(
'redirection'
,
args
=
[
'dhaynes123'
])
self
.
assertEqual
(
url
,
'/dhaynes123'
)
def
test_delete_full_slug
(
self
):
"""
/<short> - Redirect to a go link.
"""
def
test_delete_full_slug
(
self
):
url
=
reverse
(
'redirection'
,
args
=
[
'dhaynes123_-'
])
self
.
assertEqual
(
url
,
'/dhaynes123_-'
)
go/settings/urls.py
View file @
2fb09317
...
...
@@ -2,10 +2,9 @@
from
__future__
import
unicode_literals
,
absolute_import
,
print_function
,
division
# Django Imports
from
django.conf.urls
import
include
,
url
from
django.conf.urls
import
url
import
django.contrib.auth.views
from
django.contrib
import
admin
from
django.conf
import
settings
from
django.views.decorators.cache
import
cache_page
from
django.views.generic
import
TemplateView
...
...
@@ -18,7 +17,8 @@ admin.autodiscover()
# Main list of project URL's
urlpatterns
=
[
# / - Homepage url. Cached for 1 second (this is the page you see after logging in, so having it show as not logged in is strange)
# / - Homepage url. Cached for 1 second (this is the page you see after
# logging in, so having it show as not logged in is strange)
url
(
r
'^$'
,
cache_page
(
1
)(
go
.
views
.
index
),
name
=
'index'
),
# /view/<short> - View URL data. Cached for 15 minutes
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment