Commit 972dfbd2 authored by David Haynes's avatar David Haynes 🙆
Browse files

Remove all instances of Testing and merge crispy-forms to index

- :dhaynes: making history here
parent ea4afc65
......@@ -3,69 +3,11 @@ from go.models import URL, RegisteredUser
from django.core.exceptions import ValidationError
from captcha.fields import CaptchaField
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Fieldset, Submit, HTML, Div, Field, Button
from crispy_forms.bootstrap import StrictButton, FormActions, PrependedText, Accordion, AccordionGroup, Alert
from crispy_forms.layout import Layout, Fieldset, Submit, HTML, Div, Field
from crispy_forms.bootstrap import StrictButton, PrependedText, Accordion, AccordionGroup
class URLForm(forms.ModelForm):
# Custom target URL field
target = forms.URLField(
required=True,
label='Long URL',
max_length=1000,
widget=forms.URLInput(attrs={
'placeholder': 'https://'
})
)
# Check to make sure the short url has not been used
def unique_short(value):
try:
URL.objects.get(short__iexact=value)
except URL.DoesNotExist:
return
raise ValidationError('Short url already exists.')
# 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,
)
DAY = '1 Day'
WEEK = '1 Week'
MONTH = '1 Month'
NEVER = 'Never'
EXPIRATION_CHOICES = (
(DAY, DAY),
(WEEK, WEEK),
(MONTH, MONTH),
(NEVER, NEVER),
)
# Add a custom expiration choice field.
expires = forms.ChoiceField(
required=True,
label='Expiration',
choices=EXPIRATION_CHOICES,
initial=NEVER,
widget=forms.RadioSelect(),
)
class Meta:
model = URL
fields = ('target',)
exclude = ('owner', 'short', 'date_created', 'clicks', 'expires')
class URLFormTest(forms.ModelForm):
# Custom target URL field
target = forms.URLField(
required=True,
......@@ -115,7 +57,7 @@ class URLFormTest(forms.ModelForm):
)
def __init__(self, *args, **kwargs):
super(URLFormTest, self).__init__(*args, **kwargs)
super(URLForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_method = 'POST'
......@@ -230,88 +172,3 @@ class SignupForm(forms.ModelForm):
class Meta:
model = RegisteredUser
fields = '__all__'
class ExampleForm(forms.Form):
like_website = forms.TypedChoiceField(
label = "Do you like this website?",
choices = ((1, "Yes"), (0, "No")),
coerce = lambda x: bool(int(x)),
widget = forms.RadioSelect,
initial = '1',
required = True,
)
favorite_food = forms.CharField(
label = "What is your favorite food?",
max_length = 80,
required = True,
)
favorite_color = forms.CharField(
label = "What is your favorite color?",
max_length = 80,
required = True,
)
favorite_number = forms.IntegerField(
label = "Favorite number",
required = False,
)
notes = forms.CharField(
label = "Additional notes or feedback",
required = False,
)
def __init__(self, *args, **kwargs):
super(ExampleForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_id = 'id-exampleForm'
self.helper.form_method = 'post'
self.helper.form_action = 'submit_survey'
self.helper.form_class = 'form-horizontal'
self.helper.label_class = 'col-sm-2'
self.helper.field_class = 'col-md-4'
self.helper.help_text_inline = True
self.helper.error_text_inline = False
self.helper.layout = Layout(
Alert(content='<strong>Warning!</strong> Best check yo self, you\'re not looking too good.'),
Fieldset(
'Tell us your favorite stuff {{ request.user.username }}',
Accordion(
AccordionGroup('First Group',
Div(
HTML("""
<h4>Here you select whether you like the website or not:</h4>
"""),
'like_website',
style="background: rgb(#F6F6F6);",
title="First Group",
css_class="first_group",
),
),
AccordionGroup('Second Group',
PrependedText('favorite_color',
'https://',
placeholder="www."
),
)
),
Field(
HTML("""
<br />
"""),
'favorite_number',
'favorite_food',
HTML("""
<p>We use notes to get better, <strong>please help us {{ request.user.username }}</strong></p>
"""),
'notes',
css_class="inputfields"),
),
FormActions(
StrictButton('Success', css_class="btn-success"),
)
)
{% extends 'layouts/base.html' %}
{% load crispy_forms_tags %}
{% block title %}
Go - A University Branded URL Shortener
......@@ -8,108 +8,20 @@ Go - A University Branded URL Shortener
{% block content %}
<style>
.asteriskField {
display: none;
}
</style>
<div class="page-header" id="banner">
<div class="row">
<div class="col-md-12">
<h1><strong>Shorten URL</strong></h1>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="well">
<form class="form-horizontal" method="post" action="{%url 'index'%}">
{% csrf_token %}
<fieldset>
{% if form.non_field_errors %}
<div class="form-group has-error">
<div class="col-md-12">
{% for error in form.non_field_errors %}
<span class="help-block">{{ error }}</p>
{% endfor %}
</div>
</div>
{% endif %}
{% if form.target.errors %}
<div class="form-group has-error">
{% else %}
<div class="form-group">
{% endif %}
<div class="col-md-4">
<label class="control-label" for="{{ form.target.id_for_label }}">
{{ form.target.label }}</label>
</div>
<div class="col-md-8">
{{ form.target }}
{% for error in form.target.errors %}
<span class="help-block">{{error}}</span>
{% endfor %}
</div>
</div>
{% if form.short.errors %}
<div class="form-group has-error">
{% else %}
<div class="form-group">
{% endif %}
<div class="col-md-4">
<label class="control-label" for="{{ form.short.id_for_label }}">
{{ form.short.label }}</label>
</div>
<div class="col-md-8">
{{ form.short }}
{% for error in form.short.errors %}
<span class="help-block">{{error}}</span>
{% endfor %}
</div>
</div>
{% if form.expires.errors %}
<div class="form-group has-error">
{% else %}
<div class="form-group">
{% endif %}
<div class="col-md-4">
<label class="control-label" for="{{ form.expires.id_for_label }}">
{{ form.expires.label }}</label>
</div>
<div class="col-md-8">
{% for option in form.expires %}
<p>
{{ option }}
</p>
{% endfor %}
{% for error in form.expires.errors %}
<span class="help-block">{{error}}</span>
{% endfor %}
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<input type="submit" value="Shorten"
class="btn btn-primary btn-lg btn-block" />
</div>
</div>
</fieldset>
</form>
</div>
</div>
</div>
{% crispy form %}
{% endblock %}
{% extends 'layouts/base.html' %}
{% load crispy_forms_tags %}
{% block title %}
Go - A Product of Vapor
{% endblock %}
{% block content %}
<style>
.asteriskField {
display: none;
}
</style>
<div class="page-header" id="banner">
<div class="row">
<div class="col-md-12">
<h1><strong>Shorten URL</strong></h1>
</div>
</div>
</div>
<div class="well">
{% crispy form %}
</div>
{% endblock %}
from go.models import URL, RegisteredUser
from go.forms import URLForm, SignupForm, ExampleForm, URLFormTest
from go.forms import URLForm, SignupForm
from datetime import timedelta
from django.conf import settings
from django.http import HttpResponseServerError # Http404
......@@ -70,74 +70,6 @@ def error_500(request):
},
)
def test(request):
"""
Testing page, pls ignore.
"""
# If the user is not authenticated, show them a public landing page.
if not request.user.is_authenticated():
return render(request, 'public_landing.html')
# If the user isn't registered, don't give them any leeway.
if not is_approved(request.user):
return render(request, 'not_registered.html')
url_form = URLFormTest() # unbound form
if request.method == 'POST':
url_form = URLFormTest(request.POST) # bind dat form
if url_form.is_valid():
# We don't commit the url object yet because we need to add its
# owner, and parse its date field.
url = url_form.save(commit=False)
url.owner = request.user
# If the user entered a short url, it's already been validated,
# so accept it. If they did not, however, then generate a
# random one and use that instead.
short = url_form.cleaned_data.get('short').strip()
if len(short) > 0:
url.short = short
else:
# If the user didn't enter a short url, generate a random
# one. However, if a random one can't be generated, return
# a 500 server error.
random_short = URL.generate_valid_short()
if random_short is None:
return HttpResponseServerError(
render(request, '500.html', {})
)
else:
url.short = random_short
# Grab the expiration field value. It's currently an unsable
# string value, so we need to parse it into a datetime object
# relative to right now.
expires = url_form.cleaned_data.get('expires')
if expires == URLFormTest.DAY:
url.expires = timezone.now() + timedelta(days=1)
elif expires == URLFormTest.WEEK:
url.expires = timezone.now() + timedelta(weeks=1)
elif expires == URLFormTest.MONTH:
url.expires = timezone.now() + timedelta(weeks=3)
else:
pass # leave the field NULL
# Make sure that our new URL object is clean, then save it and
# let's redirect to view this baby.
url.full_clean()
url.save()
return redirect('view', url.short)
return render(request, 'test.html', {
'form': url_form,
},
)
##############################################################################
"""
Define user views here.
......
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