Skip to content

Commit 7aa856a

Browse files
Alexandre Pintotapaswenipathak
authored andcommitted
[contests]Enable crispy forms, datepicker; cleanup
1 parent f01f550 commit 7aa856a

File tree

8 files changed

+69
-54
lines changed

8 files changed

+69
-54
lines changed

oshc/main/forms.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from django import forms
2+
from crispy_forms.helper import FormHelper
3+
from .models import Contest
4+
5+
6+
class ContestForm(forms.ModelForm):
7+
def __init__(self, *args, **kwargs):
8+
super(ContestForm, self).__init__(*args, **kwargs)
9+
self.helper = FormHelper()
10+
self.helper.form_tag = False
11+
self.helper.label_class = 'col-lg-2'
12+
self.helper.field_class = 'col-lg-8'
13+
self.fields['start_date'].widget.attrs['class'] = 'datepicker'
14+
self.fields['end_date'].widget.attrs['class'] = 'datepicker'
15+
16+
class Meta:
17+
model = Contest
18+
fields = ['name', 'link', 'description', 'start_date', 'end_date']

oshc/main/static/main/css/bootstrap-datetimepicker.min.css

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

oshc/main/static/main/js/bootstrap-datetimepicker.min.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

oshc/main/static/main/js/moment.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

oshc/main/templates/contest_edit.html

Lines changed: 27 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,36 @@
11
{% extends 'base.html' %}
2+
{% load static %}
3+
{% load crispy_forms_tags %}
24

35
{% block content %}
4-
5-
<form name="newContest" method="POST" class="form-horizontal" action = "{% url 'add_contest' %}">
6-
{% csrf_token %}
7-
<div class="form-group">
8-
<label style="margin-top:70px;" for="name" class="control-label col-xs-2">Name:</label>
9-
<div class="col-xs-3">
10-
<input type="text" style="margin-top:70px;" class="form-control" id="name" pattern="([A-Za-z\s-])\w+" name="name" placeholder="Contest Name" required>
11-
</div>
12-
</div>
13-
14-
<div class="form-group">
15-
<label for="link" class="control-label col-xs-2">Contest's Website:</label>
16-
<div class="col-xs-3">
17-
<input type="url" class="form-control" id="link" name="link" placeholder="https://www.testweb.com" required>
18-
</div>
19-
</div>
20-
21-
<div class="form-group">
22-
<label for="start_date" class="control-label col-xs-2">Start Date:</label>
23-
<div class="col-xs-3">
24-
<input type="start_date" class="form-control" id="start_date" name="start_date" placeholder="YYYY-MM-DD" pattern="(?:20)[0-9]{2}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-9])|(?:(?!02)(?:0[1-9]|1[0-2])-(?:30))|(?:(?:0[13578]|1[02])-31))" required>
25-
</div>
26-
</div>
27-
28-
<div class="form-group">
29-
<label for="end_date" class="control-label col-xs-2">End Date:</label>
30-
<div class="col-xs-3">
31-
<input type="end_date" class="form-control" id="end_date" name="end_date" placeholder="YYYY-MM-DD" pattern="(?:20)[0-9]{2}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-9])|(?:(?!02)(?:0[1-9]|1[0-2])-(?:30))|(?:(?:0[13578]|1[02])-31))" required>
32-
</div>
33-
</div>
34-
6+
<link href="{% static 'main/css/bootstrap-datetimepicker.min.css' %}" rel="stylesheet">
7+
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
8+
<script src="{% static 'main/js/moment.min.js' %}"></script>
9+
<script src="{% static 'main/js/bootstrap-datetimepicker.min.js' %}"></script>
10+
<form name="newContest" method="POST" style="margin-top:70px;" class="form-horizontal"
11+
action="{% url 'add_contest' %}">
12+
{% crispy form %}
3513
<div class="form-group">
36-
<label for="desc" class="control-label col-xs-2">Description:</label>
37-
<div class="col-xs-3">
38-
<textarea class="form-control" id="desc" name="desc" placeholder="Tell something about it.." rows="3"></textarea>
39-
</div>
40-
</div>
41-
42-
<div class="form-group">
43-
<div class="col-xs-offset-3 col-xs-8">
14+
<div style="margin-bottom: 10px;" class="col-xs-offset-5 col-xs-8">
4415
<button type="submit" class="save btn btn-default">SUBMIT</button>
4516
</div>
4617
</div>
47-
4818
</form>
4919

50-
{% endblock %}
20+
<script type="text/javascript">
21+
jQuery(document).ready(function ($) {
22+
$(function () {
23+
$('.datepicker').datetimepicker(
24+
{
25+
format: 'YYYY-MM-DD',
26+
}
27+
);
28+
});
29+
30+
// protect against invalid time ranges
31+
$("#id_start_date").on("dp.change", function (e) {
32+
$('#id_end_date').data("DateTimePicker").minDate(e.date);
33+
});
34+
});
35+
</script>
36+
{% endblock %}

oshc/main/views.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from datetime import datetime
55
from django.shortcuts import render_to_response
66
from django.template import RequestContext
7+
from .forms import ContestForm
78

89

910
def home(request):
@@ -23,21 +24,19 @@ def contests(request):
2324

2425

2526
def contest_new(request):
26-
return render(request, 'contest_edit.html')
27+
form = ContestForm()
28+
return render(request, 'contest_edit.html', {'form': form})
2729

2830

2931
def add_contest(request):
30-
contest = Contest()
31-
contest.name = request.POST.get("name", "null")
32-
contest.link = request.POST.get("link", "null")
33-
start_date = request.POST.get("start_date", "null")
34-
end_date = request.POST.get("end_date", "null")
35-
contest.description = request.POST.get("desc", "null")
36-
contest.end_date = datetime.strptime(end_date, '%Y-%m-%d').date()
37-
contest.start_date = datetime.strptime(start_date, '%Y-%m-%d').date()
38-
contest.save()
39-
return HttpResponseRedirect("/submit_contest/")
40-
32+
if request.method == 'POST':
33+
form = ContestForm(request.POST)
34+
if form.is_valid():
35+
form.save()
36+
return HttpResponseRedirect("/submit_contest/")
37+
else:
38+
form = ContestForm()
39+
return render(request, 'contest_edit.html', {'form': form})
4140

4241
def submit_contest(request):
4342
return render(request, 'contest_submission.html')

oshc/oshc/settings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
'django.contrib.sessions',
4949
'django.contrib.messages',
5050
'django.contrib.staticfiles',
51+
'crispy_forms',
5152
)
5253

5354
MIDDLEWARE_CLASSES = (
@@ -159,3 +160,5 @@
159160
EMAIL_PORT = 587
160161
EMAIL_USE_TLS = True
161162
LOGIN_REDIRECT_URL = "/"
163+
164+
CRISPY_TEMPLATE_PACK = 'bootstrap3'

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ gunicorn==19.7.1
55
psycopg2==2.7.3.1
66
pytz==2017.2
77
whitenoise==3.3.1
8-
django-allauth==0.33.0
8+
django-allauth==0.33.0
9+
django-crispy-forms==1.7.0

0 commit comments

Comments
 (0)