Explorar el Código

add count form completed

Denis Merigoux hace 10 años
padre
commit
eed77ce2ad
Se han modificado 5 ficheros con 35 adiciones y 13 borrados
  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
 
48
 
49
 	def __str__(self):
49
 	def __str__(self):
50
 		if ((self.number is not None) & (self.title is not None)):
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
 		elif (self.number is not None):
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
 		elif (self.title is not None):
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
 		else:
56
 		else:
57
 			return "%s (%s,%s)" % (self.course.name,self.course.promotion,self.date)
57
 			return "%s (%s,%s)" % (self.course.name,self.course.promotion,self.date)
58
 
58
 

+ 17 - 2
census/templates/addcountTemplate.html

4
 {% block content %}
4
 {% block content %}
5
 <div class="container-fluid">
5
 <div class="container-fluid">
6
 	<div class="row">
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
 			<div class="panel panel-primary">
8
 			<div class="panel panel-primary">
9
 				<div class="panel-heading">
9
 				<div class="panel-heading">
10
 					<h2 class="panel-title">Ajouter un comptage à la base de données</h2>
10
 					<h2 class="panel-title">Ajouter un comptage à la base de données</h2>
12
 				<div class="panel-body">
12
 				<div class="panel-body">
13
 					<form action="{% url 'census.views.addcount' %}" method="post">
13
 					<form action="{% url 'census.views.addcount' %}" method="post">
14
 					{% csrf_token %}
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
 					</form>
31
 					</form>
17
 				</div>
32
 				</div>
18
 			</div>
33
 			</div>

+ 1 - 1
census/templates/coursTemplate.html

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

+ 14 - 7
census/views.py

1
 from django.shortcuts import render
1
 from django.shortcuts import render
2
 from census.models import Course,Count,Lesson
2
 from census.models import Course,Count,Lesson
3
 from django import forms
3
 from django import forms
4
+from django.http import HttpResponseRedirect
4
 
5
 
5
 #Auxiliariy functions
6
 #Auxiliariy functions
6
 
7
 
39
 	course.lessonscount= course.lessons.count()
40
 	course.lessonscount= course.lessons.count()
40
 	return course
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
 #Views
53
 #Views
46
 
54
 
69
     if request.method == 'POST':
77
     if request.method == 'POST':
70
         # create a form instance and populate it with data from the request:
78
         # create a form instance and populate it with data from the request:
71
         form = addCountForm(request.POST)
79
         form = addCountForm(request.POST)
80
+        form.fields["lesson"].queryset = Lesson.objects.all().order_by('-date')
72
         # check whether it's valid:
81
         # check whether it's valid:
73
         if form.is_valid():
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
     # if a GET (or any other method) we'll create a blank form
85
     # if a GET (or any other method) we'll create a blank form
80
     else:
86
     else:
81
         form = addCountForm()
87
         form = addCountForm()
88
+        form.fields["lesson"].queryset = Lesson.objects.all().order_by('-date') 
82
 
89
 
83
     return render(request, 'addcountTemplate.html', {'form': form})
90
     return render(request, 'addcountTemplate.html', {'form': form})

BIN
db.sqlite3