def post(self, request, profile): token_dict = request.session.get('token') current_request_token = request.POST.get('token') if not token_dict or not(current_request_token in token_dict.keys()): return HttpResponseRedirect(request.path) if token_dict[current_request_token]: requestion = Requestion.objects.get( pk=token_dict[current_request_token]) return HttpResponseRedirect(self.redirect_to(requestion)) context = self.get_context_data(profile=profile) form = self.requestion_form(request.POST) benefits_form = self.benefits_form(data=request.POST) DocumentFormset = self.get_documents_formset() if DocumentFormset: formset = DocumentFormset( data=request.POST, queryset=EvidienceDocument.objects.filter( template__destination=BENEFIT_DOCUMENT)) else: formset = None if all((form.is_valid(), benefits_form.is_valid(), (not formset or formset.is_valid()))): if not profile: profile = self.create_profile() requestion = form.save(profile=profile) pref_sadiks = form.cleaned_data.get('pref_sadiks') benefits_form.instance = requestion requestion = benefits_form.save() find_closest_kg(requestion, save=True) if formset: formset.instance = requestion benefit_documents = formset.save() else: benefit_documents = None context_dict = { 'requestion': requestion, 'pref_sadiks': pref_sadiks, 'benefit_documents': benefit_documents, 'areas': form.cleaned_data.get('areas')} context_dict.update(dict([(field, benefits_form.cleaned_data[field]) for field in benefits_form.changed_data])) Logger.objects.create_for_action( self.logger_action, context_dict=context_dict, extra={ 'user': request.user, 'obj': requestion, 'added_pref_sadiks': pref_sadiks}) messages.info(request, u'Добавлена заявка %s' % requestion.requestion_number) request.session['token'][current_request_token] = requestion.id request.session.modified = True return HttpResponseRedirect(self.redirect_to(requestion)) else: context.update({'form': form, 'benefits_form': benefits_form, 'formset': formset, 'openlayers_js': get_openlayers_js()}) return self.render_to_response(context)
def handle(self, *args, **options): print "Calculating closest kindergartens to all requestions in queue" if options['only_empty']: requestions = Requestion.objects.filter( closest_kg=None).select_related('area') else: requestions = Requestion.objects.all().select_related('area') n = 0 total = requestions.count() for requestion in requestions: if n % 100 == 0: print "Calculating {} requestion from {} total".format(n, total) print "Done {:.2f}%".format(n / float(total) * 100) if requestion.location: find_closest_kg(requestion, verbose=True) n += 1 print "Done!"
def post(self, request, requestion): context = self.get_context_data(requestion) change_requestion_form = self.change_requestion_form( request.POST, instance=requestion) change_benefits_form = BenefitsForm(request.POST, instance=requestion) pref_sadiks_form = PreferredSadikForm(request.POST, instance=requestion) DocumentFormset = self.get_documents_formset() if DocumentFormset: formset = self.get_documents_formset()(request.POST, instance=requestion, queryset=EvidienceDocument.objects.filter( template__destination=BENEFIT_DOCUMENT)) else: formset = None can_change_benefits = self.can_change_benefits(requestion) if not requestion.editable: messages.error(request, u'Заявка %s не может быть изменена' % requestion) return HttpResponseRedirect(reverse('account_requestion_info', args=[requestion.id])) if all((change_requestion_form.is_valid(), change_benefits_form.is_valid(), pref_sadiks_form.is_valid(), (not formset or formset.is_valid()))): context_dict = {'requestion': requestion, 'changed_data': [], 'cleaned_data': {}} data_changed = False extra = {'user': request.user, 'obj': requestion} if change_requestion_form.has_changed(): data_changed = True change_requestion_form.save() context_dict['changed_data'].extend(change_requestion_form.changed_data) context_dict['cleaned_data'].update(change_requestion_form.cleaned_data) # изменение льгот возможно только для документально неподтврежденных if can_change_benefits: if change_benefits_form.has_changed(): data_changed = True change_benefits_form.save() context_dict['changed_data'].extend(change_benefits_form.changed_data) context_dict['cleaned_data'].update(change_benefits_form.cleaned_data) if formset and formset.has_changed(): benefit_documents = formset.save() data_changed = True context_dict['cleaned_data'].update({'benefit_documents': benefit_documents}) if pref_sadiks_form.has_changed(): data_changed = True pref_sadiks = set(requestion.pref_sadiks.all()) pref_sadiks_form.save() new_pref_sadiks = set(requestion.pref_sadiks.all()) added_pref_sadiks = new_pref_sadiks - pref_sadiks removed_pref_sadiks = pref_sadiks - new_pref_sadiks context_dict['changed_data'].extend(pref_sadiks_form.changed_data) context_dict['cleaned_data'].update(pref_sadiks_form.cleaned_data) extra.update({'added_pref_sadiks': added_pref_sadiks}) extra.update({'removed_pref_sadiks': removed_pref_sadiks}) if data_changed: if 'location' in context_dict['changed_data']: find_closest_kg(requestion, save=True) Logger.objects.create_for_action(self.logger_action, context_dict=context_dict, extra=extra) messages.success(request, u'Изменения в заявке %s сохранены' % requestion) else: messages.error(request, u'Заявка %s не была изменена' % requestion) return HttpResponseRedirect(self.redirect_to(requestion)) context.update({ 'formset': formset, 'change_requestion_form': change_requestion_form, 'change_benefits_form': change_benefits_form, 'pref_sadiks_form': pref_sadiks_form, 'use_districts': USE_DISTRICTS, }) return self.render_to_response(context)