Commit 187a98d4be47c5814c222ace1ac10ccf2c5e552e
1 parent
bc6a9694
Exists in
master
and in
3 other branches
Adjusting webpages notification
Showing
3 changed files
with
139 additions
and
162 deletions
Show diff stats
webpage/forms.py
| 1 | 1 | # coding=utf-8 |
| 2 | 2 | from django import forms |
| 3 | -from django.forms.models import inlineformset_factory | |
| 4 | 3 | from django.utils.translation import ugettext_lazy as _ |
| 5 | 4 | from django.utils.html import strip_tags |
| 6 | 5 | |
| 7 | -from pendencies.forms import PendenciesForm | |
| 8 | -from pendencies.models import Pendencies | |
| 9 | - | |
| 10 | 6 | from subjects.models import Tag |
| 11 | 7 | |
| 12 | 8 | from .models import Webpage |
| 13 | 9 | |
| 14 | 10 | class WebpageForm(forms.ModelForm): |
| 15 | 11 | subject = None |
| 16 | - control_subject = forms.CharField(widget = forms.HiddenInput()) | |
| 17 | - | |
| 12 | + | |
| 18 | 13 | def __init__(self, *args, **kwargs): |
| 19 | 14 | super(WebpageForm, self).__init__(*args, **kwargs) |
| 20 | 15 | |
| ... | ... | @@ -23,8 +18,6 @@ class WebpageForm(forms.ModelForm): |
| 23 | 18 | if self.instance.id: |
| 24 | 19 | self.subject = self.instance.topic.subject |
| 25 | 20 | self.initial['tags'] = ", ".join(self.instance.tags.all().values_list("name", flat = True)) |
| 26 | - | |
| 27 | - self.initial['control_subject'] = self.subject.id | |
| 28 | 21 | |
| 29 | 22 | self.fields['students'].queryset = self.subject.students.all() |
| 30 | 23 | self.fields['groups'].queryset = self.subject.group_subject.all() |
| ... | ... | @@ -101,6 +94,4 @@ class WebpageForm(forms.ModelForm): |
| 101 | 94 | if not new_tag in self.instance.tags.all(): |
| 102 | 95 | self.instance.tags.add(new_tag) |
| 103 | 96 | |
| 104 | - return self.instance | |
| 105 | - | |
| 106 | -InlinePendenciesFormset = inlineformset_factory(Webpage, Pendencies, form = PendenciesForm, extra = 1, can_delete = True) | |
| 107 | 97 | \ No newline at end of file |
| 98 | + return self.instance | |
| 108 | 99 | \ No newline at end of file | ... | ... |
webpage/templates/webpages/_form.html
| ... | ... | @@ -102,103 +102,98 @@ |
| 102 | 102 | </div> |
| 103 | 103 | </div> |
| 104 | 104 | <div id="notifications" class="panel-collapse collapse"> |
| 105 | - {{ pendencies_form.management_form }} | |
| 106 | - {{ pendencies_form.non_form_errors }} | |
| 107 | - | |
| 108 | - {% for notify in pendencies_form %} | |
| 109 | - <div class="notifies"> | |
| 110 | - <div style="text-align:left"> | |
| 111 | - {% if notify.instance.pk %}{{ notify.DELETE }}{% endif %} | |
| 112 | - {% render_field notify.id %} | |
| 113 | - {% render_field notify.resource %} | |
| 114 | - {% render_field notify.subject class='pend_subj' %} | |
| 115 | - | |
| 116 | - <div class="form-group{% if notify.has_error %} has-error {% endif %} row"> | |
| 117 | - <label for="{{ notify.action.auto_id }}" class="pull-left action_label contol-label"> | |
| 118 | - {% trans 'Action not performed by the user' %}: | |
| 119 | - </label> | |
| 120 | - <div class="col-md-3"> | |
| 121 | - {% render_field notify.action class='form-control' %} | |
| 122 | - </div> | |
| 123 | - | |
| 124 | - <br clear="all" /> | |
| 125 | - | |
| 126 | - <span id="helpBlock" class="help-block">{{ notify.action.help_text }}</span> | |
| 127 | - | |
| 128 | - {% if notify.action.errors %} | |
| 129 | - <div class="alert alert-danger alert-dismissible" role="alert"> | |
| 130 | - <button type="button" class="close" data-dismiss="alert" aria-label="Close"> | |
| 131 | - <span aria-hidden="true">×</span> | |
| 132 | - </button> | |
| 133 | - <ul> | |
| 134 | - {% for error in notify.action.errors %} | |
| 135 | - <li>{{ error }}</li> | |
| 136 | - {% endfor %} | |
| 137 | - </ul> | |
| 138 | - </div> | |
| 139 | - {% endif %} | |
| 140 | - </div> | |
| 141 | - <br clear="all" /> | |
| 142 | - <div class="row"> | |
| 143 | - <div class="col-md-offset-2"> | |
| 144 | - <p>{% trans 'Wished period' %}: </p> | |
| 145 | - </div> | |
| 146 | - </div> | |
| 147 | - <div class="form-group{% if notify.has_error %} has-error {% endif %} row"> | |
| 148 | - <div class="col-lg-offset-3 col-md-offset-3 col-sm-offset-3 col-lg-2 col-md-2 col-sm-2 col-xs-3 checkbox"> | |
| 149 | - <label> | |
| 150 | - {% render_field notify.begin_date_check class="begin_date" %} {{ notify.begin_date.label }} | |
| 151 | - </label> | |
| 152 | - </div> | |
| 153 | - <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4"> | |
| 154 | - {% render_field notify.begin_date class='form-control datetime-picker begin_date_input' %} | |
| 155 | - </div> | |
| 156 | - </div> | |
| 157 | - <div class="row"> | |
| 158 | - <span id="helpBlock" class="help-block">{{ notify.begin_date.help_text }}</span> | |
| 159 | - | |
| 160 | - {% if notify.begin_date.errors %} | |
| 161 | - <div class="alert alert-danger alert-dismissible" role="alert"> | |
| 162 | - <button type="button" class="close" data-dismiss="alert" aria-label="Close"> | |
| 163 | - <span aria-hidden="true">×</span> | |
| 164 | - </button> | |
| 165 | - <ul> | |
| 166 | - {% for error in notify.begin_date.errors %} | |
| 167 | - <li>{{ error }}</li> | |
| 168 | - {% endfor %} | |
| 169 | - </ul> | |
| 170 | - </div> | |
| 171 | - {% endif %} | |
| 172 | - </div> | |
| 173 | - <div class="form-group{% if notify.has_error %} has-error {% endif %} row"> | |
| 174 | - <div class="col-lg-offset-3 col-md-offset-3 col-sm-offset-3 col-lg-2 col-md-2 col-sm-2 col-xs-3 checkbox"> | |
| 175 | - <label> | |
| 176 | - {% render_field notify.end_date_check class="end_date" %} {{ notify.end_date.label }} | |
| 177 | - </label> | |
| 178 | - </div> | |
| 179 | - <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4"> | |
| 180 | - {% render_field notify.end_date class='form-control datetime-picker end_date_input' %} | |
| 181 | - </div> | |
| 182 | - </div> | |
| 183 | - <div class="row"> | |
| 184 | - <span id="helpBlock" class="help-block">{{ notify.end_date.help_text }}</span> | |
| 185 | - | |
| 186 | - {% if notify.end_date.errors %} | |
| 187 | - <div class="alert alert-danger alert-dismissible" role="alert"> | |
| 188 | - <button type="button" class="close" data-dismiss="alert" aria-label="Close"> | |
| 189 | - <span aria-hidden="true">×</span> | |
| 190 | - </button> | |
| 191 | - <ul> | |
| 192 | - {% for error in notify.end_date.errors %} | |
| 193 | - <li>{{ error }}</li> | |
| 194 | - {% endfor %} | |
| 195 | - </ul> | |
| 196 | - </div> | |
| 197 | - {% endif %} | |
| 198 | - </div> | |
| 199 | - </div> | |
| 105 | + | |
| 106 | + <div class="notifies"> | |
| 107 | + <div style="text-align:left"> | |
| 108 | + {% render_field pendencies_form.id %} | |
| 109 | + {% render_field pendencies_form.resource %} | |
| 110 | + {% render_field pendencies_form.subject class='pend_subj' %} | |
| 111 | + | |
| 112 | + <div class="form-group{% if pendencies_form.has_error %} has-error {% endif %} row"> | |
| 113 | + <label for="{{ pendencies_form.action.auto_id }}" class="pull-left action_label contol-label"> | |
| 114 | + {% trans 'Action not performed by the user' %}: | |
| 115 | + </label> | |
| 116 | + <div class="col-md-3"> | |
| 117 | + {% render_field pendencies_form.action class='form-control' %} | |
| 118 | + </div> | |
| 119 | + | |
| 120 | + <br clear="all" /> | |
| 121 | + | |
| 122 | + <span id="helpBlock" class="help-block">{{ pendencies_form.action.help_text }}</span> | |
| 123 | + | |
| 124 | + {% if pendencies_form.action.errors %} | |
| 125 | + <div class="alert alert-danger alert-dismissible" role="alert"> | |
| 126 | + <button type="button" class="close" data-dismiss="alert" aria-label="Close"> | |
| 127 | + <span aria-hidden="true">×</span> | |
| 128 | + </button> | |
| 129 | + <ul> | |
| 130 | + {% for error in pendencies_form.action.errors %} | |
| 131 | + <li>{{ error }}</li> | |
| 132 | + {% endfor %} | |
| 133 | + </ul> | |
| 134 | + </div> | |
| 135 | + {% endif %} | |
| 136 | + </div> | |
| 137 | + <br clear="all" /> | |
| 138 | + <div class="row"> | |
| 139 | + <div class="col-md-12"> | |
| 140 | + <p>{% trans 'Wished period' %}: </p> | |
| 141 | + </div> | |
| 142 | + </div> | |
| 143 | + <div class="form-group{% if pendencies_form.has_error %} has-error {% endif %} row"> | |
| 144 | + <div class="col-lg-2 col-md-2 col-sm-2 col-xs-3 checkbox"> | |
| 145 | + <label> | |
| 146 | + {% render_field pendencies_form.begin_date_check class="begin_date" %} {{ pendencies_form.begin_date.label }} | |
| 147 | + </label> | |
| 148 | + </div> | |
| 149 | + <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4"> | |
| 150 | + {% render_field pendencies_form.begin_date class='form-control datetime-picker begin_date_input' %} | |
| 151 | + </div> | |
| 152 | + </div> | |
| 153 | + <div class="row"> | |
| 154 | + <span id="helpBlock" class="help-block">{{ pendencies_form.begin_date.help_text }}</span> | |
| 155 | + | |
| 156 | + {% if pendencies_form.begin_date.errors %} | |
| 157 | + <div class="alert alert-danger alert-dismissible" role="alert"> | |
| 158 | + <button type="button" class="close" data-dismiss="alert" aria-label="Close"> | |
| 159 | + <span aria-hidden="true">×</span> | |
| 160 | + </button> | |
| 161 | + <ul> | |
| 162 | + {% for error in pendencies_form.begin_date.errors %} | |
| 163 | + <li>{{ error }}</li> | |
| 164 | + {% endfor %} | |
| 165 | + </ul> | |
| 166 | + </div> | |
| 167 | + {% endif %} | |
| 168 | + </div> | |
| 169 | + <div class="form-group{% if pendencies_form.has_error %} has-error {% endif %} row"> | |
| 170 | + <div class="col-lg-2 col-md-2 col-sm-2 col-xs-3 checkbox"> | |
| 171 | + <label> | |
| 172 | + {% render_field pendencies_form.end_date_check class="end_date" %} {{ pendencies_form.end_date.label }} | |
| 173 | + </label> | |
| 174 | + </div> | |
| 175 | + <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4"> | |
| 176 | + {% render_field pendencies_form.end_date class='form-control datetime-picker end_date_input' %} | |
| 177 | + </div> | |
| 178 | + </div> | |
| 179 | + <div class="row"> | |
| 180 | + <span id="helpBlock" class="help-block">{{ pendencies_form.end_date.help_text }}</span> | |
| 181 | + | |
| 182 | + {% if pendencies_form.end_date.errors %} | |
| 183 | + <div class="alert alert-danger alert-dismissible" role="alert"> | |
| 184 | + <button type="button" class="close" data-dismiss="alert" aria-label="Close"> | |
| 185 | + <span aria-hidden="true">×</span> | |
| 186 | + </button> | |
| 187 | + <ul> | |
| 188 | + {% for error in pendencies_form.end_date.errors %} | |
| 189 | + <li>{{ error }}</li> | |
| 190 | + {% endfor %} | |
| 191 | + </ul> | |
| 192 | + </div> | |
| 193 | + {% endif %} | |
| 194 | + </div> | |
| 200 | 195 | </div> |
| 201 | - {% endfor %} | |
| 196 | + </div> | |
| 202 | 197 | </div> |
| 203 | 198 | </div> |
| 204 | 199 | |
| ... | ... | @@ -333,40 +328,16 @@ |
| 333 | 328 | </form> |
| 334 | 329 | <script type="text/javascript"> |
| 335 | 330 | $(function() { |
| 336 | - $('.notifies').formset({ | |
| 337 | - addText: '{% trans "Add new notification" %}', | |
| 338 | - deleteText: '{% trans "Remove this notification" %}', | |
| 339 | - prefix: '{{ pendencies_form.prefix }}', | |
| 340 | - added: function (row) { | |
| 341 | - var locale = navigator.language || navigator.userLanguage; | |
| 342 | - | |
| 343 | - $(row).find('.datetime-picker').each(function () { | |
| 344 | - $(this).datetimepicker({ | |
| 345 | - locale: locale | |
| 346 | - }); | |
| 347 | - }); | |
| 348 | - | |
| 349 | - $('.begin_date_input').on('click', function () { | |
| 350 | - var checkbox = $(this).parent().parent().find('.begin_date'); | |
| 351 | - | |
| 352 | - $(checkbox).prop('checked', true); | |
| 353 | - }); | |
| 354 | - | |
| 355 | - $('.end_date_input').on('click', function () { | |
| 356 | - var checkbox = $(this).parent().parent().find('.end_date'); | |
| 357 | - | |
| 358 | - $(checkbox).prop('checked', true); | |
| 359 | - }); | |
| 360 | - | |
| 361 | - subject = $("#id_control_subject").val(); | |
| 362 | - | |
| 363 | - $(row).find('.pend_subj').val(subject); | |
| 364 | - } | |
| 365 | - }); | |
| 331 | + var begin_val = $('.begin_date_input').val(), | |
| 332 | + end_val = $('.end_date_input').val(); | |
| 366 | 333 | |
| 367 | - subject = $("#id_control_subject").val(); | |
| 334 | + if (begin_val != '') { | |
| 335 | + $(".begin_date").prop('checked', true); | |
| 336 | + } | |
| 368 | 337 | |
| 369 | - $('.pend_subj').val(subject); | |
| 338 | + if (end_val != '') { | |
| 339 | + $(".end_date").prop('checked', true); | |
| 340 | + } | |
| 370 | 341 | |
| 371 | 342 | {% if not pendencies_form.is_valid and pendencies_form.is_bound %} |
| 372 | 343 | $("#notifications").collapse('toggle'); | ... | ... |
webpage/views.py
| ... | ... | @@ -9,7 +9,9 @@ from amadeus.permissions import has_subject_permissions, has_resource_permission |
| 9 | 9 | |
| 10 | 10 | from topics.models import Topic |
| 11 | 11 | |
| 12 | -from .forms import WebpageForm, InlinePendenciesFormset | |
| 12 | +from pendencies.forms import PendenciesForm | |
| 13 | + | |
| 14 | +from .forms import WebpageForm | |
| 13 | 15 | from .models import Webpage |
| 14 | 16 | |
| 15 | 17 | class NewWindowView(LoginRequiredMixin, generic.DetailView): |
| ... | ... | @@ -81,7 +83,7 @@ class CreateView(LoginRequiredMixin, generic.edit.CreateView): |
| 81 | 83 | slug = self.kwargs.get('slug', '') |
| 82 | 84 | topic = get_object_or_404(Topic, slug = slug) |
| 83 | 85 | |
| 84 | - pendencies_form = InlinePendenciesFormset(initial = [{'subject': topic.subject.id, 'actions': [("", "-------"),("view", _("Visualize"))]}]) | |
| 86 | + pendencies_form = PendenciesForm(initial = {'subject': topic.subject.id, 'actions': [("", "-------"),("view", _("Visualize"))]}) | |
| 85 | 87 | |
| 86 | 88 | return self.render_to_response(self.get_context_data(form = form, pendencies_form = pendencies_form)) |
| 87 | 89 | |
| ... | ... | @@ -94,7 +96,7 @@ class CreateView(LoginRequiredMixin, generic.edit.CreateView): |
| 94 | 96 | slug = self.kwargs.get('slug', '') |
| 95 | 97 | topic = get_object_or_404(Topic, slug = slug) |
| 96 | 98 | |
| 97 | - pendencies_form = InlinePendenciesFormset(self.request.POST, initial = [{'subject': topic.subject.id, 'actions': [("", "-------"),("view", _("Visualize"))]}]) | |
| 99 | + pendencies_form = PendenciesForm(self.request.POST, initial = {'subject': topic.subject.id, 'actions': [("", "-------"),("view", _("Visualize"))]}) | |
| 98 | 100 | |
| 99 | 101 | if (form.is_valid() and pendencies_form.is_valid()): |
| 100 | 102 | return self.form_valid(form, pendencies_form) |
| ... | ... | @@ -128,15 +130,12 @@ class CreateView(LoginRequiredMixin, generic.edit.CreateView): |
| 128 | 130 | |
| 129 | 131 | self.object.save() |
| 130 | 132 | |
| 131 | - pendencies_form.instance = self.object | |
| 132 | - pendencies_form.save(commit = False) | |
| 133 | + pend_form = pendencies_form.save(commit = False) | |
| 134 | + pend_form.resource = self.object | |
| 135 | + | |
| 136 | + if not pend_form.action == "": | |
| 137 | + pend_form.save() | |
| 133 | 138 | |
| 134 | - for pform in pendencies_form.forms: | |
| 135 | - pend_form = pform.save(commit = False) | |
| 136 | - | |
| 137 | - if not pend_form.action == "": | |
| 138 | - pend_form.save() | |
| 139 | - | |
| 140 | 139 | return redirect(self.get_success_url()) |
| 141 | 140 | |
| 142 | 141 | def get_context_data(self, **kwargs): |
| ... | ... | @@ -183,6 +182,24 @@ class UpdateView(LoginRequiredMixin, generic.UpdateView): |
| 183 | 182 | |
| 184 | 183 | return super(UpdateView, self).dispatch(request, *args, **kwargs) |
| 185 | 184 | |
| 185 | + def get(self, request, *args, **kwargs): | |
| 186 | + self.object = self.get_object() | |
| 187 | + | |
| 188 | + form_class = self.get_form_class() | |
| 189 | + form = self.get_form(form_class) | |
| 190 | + | |
| 191 | + slug = self.kwargs.get('topic_slug', '') | |
| 192 | + topic = get_object_or_404(Topic, slug = slug) | |
| 193 | + | |
| 194 | + pend_form = self.object.pendencies_resource.all() | |
| 195 | + | |
| 196 | + if len(pend_form) > 0: | |
| 197 | + pendencies_form = PendenciesForm(instance = pend_form[0], initial = {'subject': topic.subject.id, 'actions': [("", "-------"),("view", _("Visualize"))]}) | |
| 198 | + else: | |
| 199 | + pendencies_form = PendenciesForm(initial = {'subject': topic.subject.id, 'actions': [("", "-------"),("view", _("Visualize"))]}) | |
| 200 | + | |
| 201 | + return self.render_to_response(self.get_context_data(form = form, pendencies_form = pendencies_form)) | |
| 202 | + | |
| 186 | 203 | def post(self, request, *args, **kwargs): |
| 187 | 204 | self.object = self.get_object() |
| 188 | 205 | |
| ... | ... | @@ -192,7 +209,12 @@ class UpdateView(LoginRequiredMixin, generic.UpdateView): |
| 192 | 209 | slug = self.kwargs.get('topic_slug', '') |
| 193 | 210 | topic = get_object_or_404(Topic, slug = slug) |
| 194 | 211 | |
| 195 | - pendencies_form = InlinePendenciesFormset(self.request.POST, instance = self.object, initial = [{'subject': topic.subject.id, 'actions': [("", "-------"),("view", _("Visualize"))]}]) | |
| 212 | + pend_form = self.object.pendencies_resource.all() | |
| 213 | + | |
| 214 | + if len(pend_form) > 0: | |
| 215 | + pendencies_form = PendenciesForm(self.request.POST, instance = pend_form[0], initial = {'subject': topic.subject.id, 'actions': [("", "-------"),("view", _("Visualize"))]}) | |
| 216 | + else: | |
| 217 | + pendencies_form = PendenciesForm(self.request.POST, initial = {'subject': topic.subject.id, 'actions': [("", "-------"),("view", _("Visualize"))]}) | |
| 196 | 218 | |
| 197 | 219 | if (form.is_valid() and pendencies_form.is_valid()): |
| 198 | 220 | return self.form_valid(form, pendencies_form) |
| ... | ... | @@ -210,14 +232,11 @@ class UpdateView(LoginRequiredMixin, generic.UpdateView): |
| 210 | 232 | |
| 211 | 233 | self.object.save() |
| 212 | 234 | |
| 213 | - pendencies_form.instance = self.object | |
| 214 | - pendencies_form.save(commit = False) | |
| 235 | + pend_form = pendencies_form.save(commit = False) | |
| 236 | + pend_form.resource = self.object | |
| 215 | 237 | |
| 216 | - for form in pendencies_form.forms: | |
| 217 | - pend_form = form.save(commit = False) | |
| 218 | - | |
| 219 | - if not pend_form.action == "": | |
| 220 | - pend_form.save() | |
| 238 | + if not pend_form.action == "": | |
| 239 | + pend_form.save() | |
| 221 | 240 | |
| 222 | 241 | return redirect(self.get_success_url()) |
| 223 | 242 | |
| ... | ... | @@ -232,10 +251,6 @@ class UpdateView(LoginRequiredMixin, generic.UpdateView): |
| 232 | 251 | context['topic'] = topic |
| 233 | 252 | context['subject'] = topic.subject |
| 234 | 253 | |
| 235 | - if not self.request.POST: | |
| 236 | - context['form'] = WebpageForm(instance=self.object, initial = {'subject': topic.subject}) | |
| 237 | - context['pendencies_form'] = InlinePendenciesFormset(instance=self.object, initial = [{'subject': topic.subject.id, 'actions': [("", "-------"),("view", _("Visualize"))]}]) | |
| 238 | - | |
| 239 | 254 | return context |
| 240 | 255 | |
| 241 | 256 | def get_success_url(self): | ... | ... |