Quellcode durchsuchen

add count form completed

Denis Merigoux vor 10 Jahren
Ursprung
Commit
eed77ce2ad
5 geänderte Dateien mit 35 neuen und 13 gelöschten Zeilen
  1. 3 3
      census/models.py
  2. 17 2
      census/templates/addcountTemplate.html
  3. 1 1
      census/templates/coursTemplate.html
  4. 14 7
      census/views.py
  5. BIN
      db.sqlite3

+ 3 - 3
census/models.py

@@ -48,11 +48,11 @@ class Lesson(models.Model):
48 48
 
49 49
 	def __str__(self):
50 50
 		if ((self.number is not None) & (self.title is not None)):
51
-			return "%s %s : %s (%s,%s)" % (self.course.name,self.number,self.title,self.course.promotion,self.date)
51
+			return "%s %s : %s (%s, %s)" % (self.course.name,self.number,self.title,self.course.promotion,self.date)
52 52
 		elif (self.number is not None):
53
-			return "%s %s (%s,%s)" % (self.course.name,self.number,self.course.promotion,self.date)
53
+			return "%s %s (%s, %s)" % (self.course.name,self.number,self.course.promotion,self.date)
54 54
 		elif (self.title is not None):
55
-			return "%s : %s (%s,%s)" % (self.course.name,self.title,self.course.promotion,self.date)
55
+			return "%s : %s (%s, %s)" % (self.course.name,self.title,self.course.promotion,self.date)
56 56
 		else:
57 57
 			return "%s (%s,%s)" % (self.course.name,self.course.promotion,self.date)
58 58
 

+ 17 - 2
census/templates/addcountTemplate.html

@@ -4,7 +4,7 @@
4 4
 {% block content %}
5 5
 <div class="container-fluid">
6 6
 	<div class="row">
7
-		<div class="col-md-4 col-md-offset-4">
7
+		<div class="col-md-6 col-md-offset-3">
8 8
 			<div class="panel panel-primary">
9 9
 				<div class="panel-heading">
10 10
 					<h2 class="panel-title">Ajouter un comptage à la base de données</h2>
@@ -12,7 +12,22 @@
12 12
 				<div class="panel-body">
13 13
 					<form action="{% url 'census.views.addcount' %}" method="post">
14 14
 					{% csrf_token %}
15
-					{{form}}
15
+					{{ form.non_field_errors }}
16
+					<div class="list-group">
17
+						{% for field in form%}
18
+						<div class="list-group-item">
19
+    					{{ field.errors }}
20
+    						<div style="overflow:auto">
21
+    							<label for="{{ field.id_for_label }}">{{field.label}} :</label>
22
+    							<span style='float:right'>{{ field }}</span>
23
+    						</div>
24
+						</div>
25
+						{% endfor %}
26
+					</div>
27
+					<div style="text-align:center; margin-bottom:10px">
28
+					<button type="submit" class="btn btn-primary">Compter !</button>
29
+					</div>
30
+					<p>Pour modifier un comptage déjà enregistré, contacte l'<a href="mailto:denis.merigoux@polytechnique.edu">administrateur du site</a>.</p>
16 31
 					</form>
17 32
 				</div>
18 33
 			</div>

+ 1 - 1
census/templates/coursTemplate.html

@@ -1,5 +1,5 @@
1 1
 {% extends 'baseTemplate.html' %}
2
-{% block title %}Conseils de comptage{% endblock %}
2
+{% block title %}{{course.name}} ({{course.promotion}}){% endblock %}
3 3
 {% block nav %}{% endblock %}
4 4
 {% block content %}
5 5
 <div class="container-fluid">

+ 14 - 7
census/views.py

@@ -1,6 +1,7 @@
1 1
 from django.shortcuts import render
2 2
 from census.models import Course,Count,Lesson
3 3
 from django import forms
4
+from django.http import HttpResponseRedirect
4 5
 
5 6
 #Auxiliariy functions
6 7
 
@@ -39,8 +40,15 @@ def getCourseStatistics(course):
39 40
 	course.lessonscount= course.lessons.count()
40 41
 	return course
41 42
 
42
-class addCountForm(forms.Form):
43
-	course = forms.CharField(label="Cours",max_length=60)
43
+class addCountForm(forms.ModelForm):
44
+    class Meta:
45
+        model = Count
46
+    def clean_census(self):
47
+    	census = self.cleaned_data['census']
48
+    	enrolled = self.cleaned_data['lesson'].course.enrolled
49
+    	if (census>enrolled):
50
+        	raise forms.ValidationError("Tu as compté plus de gens qu'il n'y a de personnes inscrites dans ce cours !")
51
+    	return census  # Ne pas oublier de renvoyer le contenu du champ traité
44 52
 
45 53
 #Views
46 54
 
@@ -69,15 +77,14 @@ def addcount(request):
69 77
     if request.method == 'POST':
70 78
         # create a form instance and populate it with data from the request:
71 79
         form = addCountForm(request.POST)
80
+        form.fields["lesson"].queryset = Lesson.objects.all().order_by('-date')
72 81
         # check whether it's valid:
73 82
         if form.is_valid():
74
-            # process the data in form.cleaned_data as required
75
-            # ...
76
-            # redirect to a new URL:
77
-            return HttpResponseRedirect('/')
78
-
83
+            count = form.save()
84
+            return HttpResponseRedirect('/cours/'+ str(count.lesson.course.id))
79 85
     # if a GET (or any other method) we'll create a blank form
80 86
     else:
81 87
         form = addCountForm()
88
+        form.fields["lesson"].queryset = Lesson.objects.all().order_by('-date') 
82 89
 
83 90
     return render(request, 'addcountTemplate.html', {'form': form})

BIN
db.sqlite3