Commit b1d365ea authored by David Haynes's avatar David Haynes

Subclass URLForm to create EditForm

- It does not add anything, rather just modifies the Crispy Forms layout
- I do love me some crispy forms
parent 3d9b0fad
Pipeline #1181 passed with stage
in 58 seconds
......@@ -235,6 +235,76 @@ class URLForm(ModelForm):
# what attributes are included
fields = ['target']
class EditForm(URLForm):
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)
# Define the basics for crispy-forms
self.helper = FormHelper()
self.helper.form_method = 'POST'
# Some xtra vars for form css purposes
self.helper.form_class = 'form-horizontal'
self.helper.label_class = 'col-md-1'
self.helper.field_class = 'col-md-6'
# The main "layout" defined
self.helper.layout = Layout(
Fieldset('',
#######################
Accordion(
# Step 1: Long URL
AccordionGroup('Step 1: Long URL',
Div(
HTML("""
<h4>Modify the URL you would like to shorten:</h4>
<br />"""),
'target',
style="background: rgb(#F6F6F6);"),
active=True,
template='crispy/accordian-group.html'),
# Step 2: Short URL
AccordionGroup('Step 2: Short URL',
Div(
HTML("""
<h4>Modify the Go address:</h4>
<br />"""),
PrependedText(
'short', 'https://go.gmu.edu/', template='crispy/customPrepended.html'),
style="background: rgb(#F6F6F6);"),
active=True,
template='crispy/accordian-group.html',),
# Step 3: Expiration
AccordionGroup('Step 3: URL Expiration',
Div(
HTML("""
<h4>Modify the expiration date:</h4>
<br />"""),
'expires',
Field('expires_custom', template="crispy/customDateField.html"),
style="background: rgb(#F6F6F6);"),
active=True,
template='crispy/accordian-group.html'),
# FIN
template='crispy/accordian.html'),
#######################
HTML("""
<br />"""),
StrictButton('Submit Changes', css_class="btn btn-primary btn-md col-md-4", type='submit')))
class Meta(URLForm.Meta):
# what attributes are included
fields = URLForm.Meta.fields
class SignupForm(ModelForm):
"""
The form that is used when a user is signing up to be a RegisteredUser
......
......@@ -17,7 +17,7 @@ from django.contrib.auth.models import User
from django.test import TestCase
# App Imports
from go.forms import SignupForm, URLForm
from go.forms import SignupForm, URLForm, EditForm
from go.models import URL, RegisteredUser
class URLFormTest(TestCase):
......@@ -134,6 +134,21 @@ class URLFormTest(TestCase):
print(form.errors)
self.assertFalse(form.is_valid())
class EditForm(TestCase):
"""
Test cases for the edit URL form.
As currently this form inherits from the URLForm and does not add any fields,
we cannot test any values. It exists purely for aesthetics.
"""
def test_django_test(self):
"""
Default test case, does not actually test anything
"""
self.assertEqual("Hello World!", "Hello World!")
class SignupFormTest(TestCase):
"""
Test cases for the Signup form
......
......@@ -24,7 +24,7 @@ from django.utils import timezone
from ratelimit.decorators import ratelimit
# App Imports
from go.forms import SignupForm, URLForm
from go.forms import SignupForm, URLForm, EditForm
from go.models import URL, RegisteredUser
......@@ -220,7 +220,7 @@ def edit(request, short):
if request.method == 'POST':
# Now we initialize the form again but this time we have the POST
# request
url_form = URLForm(request.POST, host=request.META.get('HTTP_HOST'))
url_form = EditForm(request.POST, host=request.META.get('HTTP_HOST'))
# Make a copy of the old URL
copy = url
......@@ -292,7 +292,7 @@ def edit(request, short):
# Initial data set here
if url.expires != None:
# Initialize a URL form with an expire date
url_form = URLForm(host=request.META.get('HTTP_HOST'), initial={
url_form = EditForm(host=request.META.get('HTTP_HOST'), initial={
'target': url.target,
'short': url.short,
'expires': 'Custom Date',
......@@ -300,7 +300,7 @@ def edit(request, short):
}) # unbound form
else:
# Initialize a URL form without an expire date
url_form = URLForm(host=request.META.get('HTTP_HOST'), initial={
url_form = EditForm(host=request.META.get('HTTP_HOST'), initial={
'target': url.target,
'short': url.short,
'expires': 'Never',
......
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