示例#1
0
    def post_mrsrequest(self, request, *args, **kwargs):
        if not self.has_perm():
            return http.HttpResponseBadRequest()

        # for display
        self.caisse_form = CaisseVoteForm(prefix='other')

        self.forms = collections.OrderedDict([
            ('mrsrequest',
             MRSRequestCreateForm(request.POST,
                                  mrsrequest_uuid=self.mrsrequest_uuid)),
            ('person', PersonForm(request.POST)),
            ('certify', CertifyForm(request.POST)),
        ])
        self.forms['transport'] = TransportIterativeForm(
            request.POST,
            instance=Transport(mrsrequest_id=self.mrsrequest_uuid),
        )
        for key, value in self.request.POST.items():
            if '-date_return' not in key:
                continue

            number = key.split('-')[0]
            self.forms['transport-{}'.format(number)] = form = TransportForm(
                request.POST,
                instance=Transport(mrsrequest_id=self.mrsrequest_uuid),
                prefix=number,
            )
            form.fields['date_depart'].label += ' {}'.format(number)
            form.fields['date_return'].label += ' {}'.format(number)

        with transaction.atomic():
            self.success = not self.form_errors() and self.save_mrsrequest()

        return generic.TemplateView.get(self, request, *args, **kwargs)
示例#2
0
文件: views.py 项目: angelo510/mrs
    def get(self, request, *args, **kwargs):
        self.object = MRSRequest()
        self.object.allow(request)
        self.mrsrequest_uuid = str(self.object.id)
        self.caisse_form = CaisseVoteForm(prefix='other')

        self.forms = collections.OrderedDict([
            ('mrsrequest', MRSRequestCreateForm(instance=self.object)),
            ('person', PersonForm(
                initial={k: v for k, v in request.GET.items()})),
            ('transport', TransportIterativeForm()),
            ('certify', CertifyForm()),
        ])

        return super().get(request, *args, **kwargs)
示例#3
0
    def post_caisse(self, request, *args, **kwargs):
        # needed for rendering
        self.forms = collections.OrderedDict([
            ('mrsrequest',
             MRSRequestCreateForm(initial={'caisse': 'other'},
                                  mrsrequest_uuid=self.mrsrequest_uuid)),
            ('person', PersonForm()),
            ('certify', CertifyForm()),
        ])
        self.forms['transport'] = TransportIterativeForm(
            instance=Transport(mrsrequest_id=self.mrsrequest_uuid), )

        self.caisse_form = CaisseVoteForm(request.POST, prefix='other')
        with transaction.atomic():
            self.success_caisse = (self.caisse_form.is_valid()
                                   and self.save_caisse())

        return generic.TemplateView.get(self, request, *args, **kwargs)
示例#4
0
    def get(self, request, *args, **kwargs):
        self.object = MRSRequest()
        self.object.allow(request)
        self.mrsrequest_uuid = str(self.object.id)
        self.caisse_form = CaisseVoteForm(prefix='other')

        self.forms = collections.OrderedDict([
            ('mrsrequest', MRSRequestCreateForm(instance=self.object, )),
            ('person',
             PersonForm(initial={k: v
                                 for k, v in request.GET.items()})),
            ('transport', TransportIterativeForm()),
            ('transport_formset', TransportFormSet()),
            ('certify', CertifyForm()),
            # Deactivate email consent for now, not used anymore by staff
            # ('use_email', UseEmailForm()),
        ])

        return super().get(request, *args, **kwargs)
示例#5
0
class MRSRequestCreateView(MRSRequestFormBaseView):
    template_name = 'mrsrequest/form.html'
    base = 'base.html'
    modes = [i[0] for i in Bill.MODE_CHOICES]
    extra_context = {
        'title_suffix': TITLE_SUFFIX,
    }

    def get(self, request, *args, **kwargs):
        self.object = MRSRequest()
        self.object.allow(request)
        self.mrsrequest_uuid = str(self.object.id)
        self.caisse_form = CaisseVoteForm(prefix='other')

        self.forms = collections.OrderedDict([
            ('mrsrequest', MRSRequestCreateForm(
                instance=self.object,
                initial=dict(caisse=request.GET.get('caisse', None)),
            )),
            ('person', PersonForm(
                initial={k: v for k, v in request.GET.items()})),
            ('transport', TransportIterativeForm()),
            ('transport_formset', TransportFormSet()),
            ('certify', CertifyForm()),
            ('use_email', UseEmailForm()),
        ])

        return super().get(request, *args, **kwargs)

    def caisses_json(self):
        caisses = {
            i.pk: dict(
                parking_enable=i.parking_enable,
            ) for i in Caisse.objects.all()
        }
        return json.dumps(caisses)

    def has_perm(self, exists=False):
        if not self.mrsrequest_uuid:  # require mrsrequest_uuid on post
            return False

        # view is supposed to create a new request
        try:
            if exists:
                if not MRSRequest.objects.filter(id=self.mrsrequest_uuid):
                    return False
            else:
                if MRSRequest.objects.filter(id=self.mrsrequest_uuid):
                    return False
        except ValidationError:  # badly formated uuid
            return False

        # Let's not even load the object from db if request aint allowed
        if not MRSRequest(id=self.mrsrequest_uuid).is_allowed(self.request):
            return False

        return True

    def post(self, request, *args, **kwargs):
        self.mrsrequest_uuid = self.request.POST.get('mrsrequest_uuid', None)
        caisse = request.POST.get('caisse', None)

        if caisse == 'other':
            return self.post_caisse(request, *args, **kwargs)
        elif 'rating-score' in request.POST:
            return self.post_rating(request, *args, **kwargs)
        else:
            return self.post_mrsrequest(request, *args, **kwargs)

    def post_rating(self, request, *args, **kwargs):
        self.mrsrequest_uuid = self.request.POST.get(
            'rating-mrsrequest_uuid', None)
        if not self.has_perm(exists=True):
            return http.HttpResponseBadRequest()

        self.rating_form = RatingForm(request.POST, prefix='rating')
        self.rating_form.instance.mrsrequest = MRSRequest.objects.get(
            pk=self.mrsrequest_uuid
        )

        if self.rating_form.is_valid():
            with transaction.atomic():
                self.success_rating = self.rating_form.save()

        return generic.TemplateView.get(self, request, *args, **kwargs)

    def rating_show(self):
        requests = MRSRequest.objects.filter(
            insured=self.object.insured,
        )

        previous = Rating.objects.filter(
            mrsrequest__in=requests,
        ).order_by('-creation_datetime').first()

        if previous:
            requests = requests.filter(
                creation_datetime__gte=previous.creation_datetime
            )

        return requests.count() >= 5

    def post_caisse(self, request, *args, **kwargs):
        # needed for rendering
        self.forms = collections.OrderedDict([
            ('mrsrequest', MRSRequestCreateForm(
                initial={'caisse': 'other'},
                mrsrequest_uuid=self.mrsrequest_uuid
            )),
            ('person', PersonForm()),
            ('certify', CertifyForm()),
        ])
        self.forms['transport'] = TransportIterativeForm()
        self.forms['transport_formset'] = TransportFormSet()

        self.caisse_form = CaisseVoteForm(request.POST, prefix='other')
        with transaction.atomic():
            self.success_caisse = (
                self.caisse_form.is_valid() and self.save_caisse())

        return generic.TemplateView.get(self, request, *args, **kwargs)

    def save_caisse(self):
        caisse = self.caisse_form.cleaned_data['caisse']

        email = self.caisse_form.cleaned_data.get('email', None)
        if email:
            Email.objects.create(email=email, caisse=caisse)

        caisse.score += 1
        caisse.save()

        return True

    def post_mrsrequest(self, request, *args, **kwargs):
        if not self.has_perm():
            return http.HttpResponseBadRequest()

        # for display
        self.caisse_form = CaisseVoteForm(prefix='other')

        self.forms = self.post_get_forms(request)

        self.success = self.confirm = False
        if not self.form_errors():
            confirmed = self.request.POST.get('confirm', False)

            # beware that form_confirms will provision forms with artificial
            # errors: do NOT call it if confirmed was POST-ed
            if confirmed or not self.form_confirms():
                with transaction.atomic():
                    self.save_mrsrequest()
                    self.success = True

                if self.conflicts_count:
                    # also increment the daily stat !
                    Caller(
                        callback='mrsstat.models.increment',
                        kwargs=dict(
                            name='mrsrequest_count_conflicting',
                            count=1,
                            date=today(),
                            caisse=self.forms['mrsrequest'].cleaned_data.get(
                                'caisse').pk,
                            recalculate=['mrsrequest_count_resolved'],
                        ),
                    ).spool('stat')

                if self.rating_show():
                    self.rating_form = RatingForm(
                        prefix='rating',
                        initial=dict(mrsrequest_uuid=self.mrsrequest_uuid),
                    )

            else:
                # will be needed later on by save_mrsrequest to calculate the
                # number of conflicts that the user has resolved on their own
                # for this MRSRequest
                self.session.setdefault(
                    'conflicts_initial',
                    self.conflicts_count,
                )

                # also increment the daily stat if not done already !
                if 'conflicts_initial_incremented' not in self.session:
                    Caller(
                        callback='mrsstat.models.increment',
                        kwargs=dict(
                            name='mrsrequest_count_conflicted',
                            count=1,
                            date=today(),
                            caisse=self.forms['mrsrequest'].cleaned_data.get(
                                'caisse').pk,
                            recalculate=['mrsrequest_count_resolved'],
                        ),
                    ).spool('stat')
                    self.session['conflicts_initial_incremented'] = True

                # trigger session backend write by session middleware
                self.request.session.modified = True
                self.confirm = True

        return generic.TemplateView.get(self, request, *args, **kwargs)

    @property
    def session(self):
        """Return a reference to the session dict for this MRSRequest."""
        return self.request.session.get(
            MRSRequest.SESSION_KEY
        ).get(
            self.mrsrequest_uuid
        )

    @property
    def conflicts_initial(self):
        return self.session.get('conflicts_initial', 0)

    @property
    def conflicts_count(self):
        '''
        return self.forms.get(
                'transport_formset'
            ).get_confirms_count()
        '''
        if '_conflicts_count' not in self.__dict__:
            self._conflicts_count = self.forms.get(
                'transport_formset'
            ).get_confirms_count()
        return self._conflicts_count

    def post_get_forms(self, request):
        forms = collections.OrderedDict([
            ('mrsrequest', MRSRequestCreateForm(
                request.POST,
                mrsrequest_uuid=self.mrsrequest_uuid
            )),
            ('person', PersonForm(request.POST)),
            ('certify', CertifyForm(request.POST)),
            ('use_email', UseEmailForm(request.POST)),
        ])
        forms['transport'] = TransportIterativeForm(request.POST)
        forms['transport_formset'] = TransportFormSet(request.POST)

        return forms

    def save_mrsrequest(self):
        person = self.forms['person'].get_or_create()
        mrsrequest = self.forms['mrsrequest'].instance

        self.forms['transport_formset'].set_confirms(
            person.nir, person.birth_date
        )
        conflicts_resolved = self.conflicts_initial - self.conflicts_count
        mrsrequest.conflicts_accepted = self.conflicts_count
        mrsrequest.conflicts_resolved = conflicts_resolved

        mrsrequest.insured = person
        self.object = self.forms['mrsrequest'].save()

        for form in self.forms['transport_formset'].forms:
            Transport.objects.create(
                date_depart=form.cleaned_data.get('date_depart'),
                date_return=form.cleaned_data.get('date_return'),
                mrsrequest=self.object,
            )

        if self.forms['use_email'].cleaned_data['use_email']:
            person.use_email = True

        person.conflicts_accepted += self.conflicts_count
        person.conflicts_resolved += conflicts_resolved
        person.save()

        mail_context = dict(view=self, base_url=settings.BASE_URL)
        Caller(
            callback='djcall.django.email_send',
            kwargs=dict(
                subject=template.loader.get_template(
                    'mrsrequest/success_mail_title.txt'
                ).render(mail_context).strip(),
                body=template.loader.get_template(
                    'mrsrequest/success_mail_body.txt'
                ).render(mail_context).strip(),
                to=[self.object.insured.email],
                reply_to=[settings.TEAM_EMAIL],
            )
        ).spool('mail')

        return True

    def form_confirms(self):
        self.forms['transport_formset'].add_confirms(
            nir=self.forms['person'].cleaned_data['nir'],
            birth_date=self.forms['person'].cleaned_data['birth_date'],
        )
        return self.form_errors()
示例#6
0
    def post_mrsrequest(self, request, *args, **kwargs):
        if not self.has_perm():
            return http.HttpResponseBadRequest()

        # for display
        self.caisse_form = CaisseVoteForm(prefix='other')

        self.forms = self.post_get_forms(request)

        self.success = self.confirm = False
        if not self.form_errors():
            confirmed = self.request.POST.get('confirm', False)

            # beware that form_confirms will provision forms with artificial
            # errors: do NOT call it if confirmed was POST-ed
            if confirmed or not self.form_confirms():
                with transaction.atomic():
                    self.save_mrsrequest()
                    self.success = True

                if self.conflicts_count:
                    # also increment the daily stat !
                    Caller(
                        callback='mrsstat.models.increment',
                        kwargs=dict(
                            name='mrsrequest_count_conflicting',
                            count=1,
                            date=today(),
                            caisse=self.forms['mrsrequest'].cleaned_data.get(
                                'caisse').pk,
                            recalculate=['mrsrequest_count_resolved'],
                        ),
                    ).spool('stat')

                if self.rating_show():
                    self.rating_form = RatingForm(
                        prefix='rating',
                        initial=dict(mrsrequest_uuid=self.mrsrequest_uuid),
                    )

            else:
                # will be needed later on by save_mrsrequest to calculate the
                # number of conflicts that the user has resolved on their own
                # for this MRSRequest
                self.session.setdefault(
                    'conflicts_initial',
                    self.conflicts_count,
                )

                # also increment the daily stat if not done already !
                if 'conflicts_initial_incremented' not in self.session:
                    Caller(
                        callback='mrsstat.models.increment',
                        kwargs=dict(
                            name='mrsrequest_count_conflicted',
                            count=1,
                            date=today(),
                            caisse=self.forms['mrsrequest'].cleaned_data.get(
                                'caisse').pk,
                            recalculate=['mrsrequest_count_resolved'],
                        ),
                    ).spool('stat')
                    self.session['conflicts_initial_incremented'] = True

                # trigger session backend write by session middleware
                self.request.session.modified = True
                self.confirm = True

        return generic.TemplateView.get(self, request, *args, **kwargs)
示例#7
0
文件: views.py 项目: angelo510/mrs
class MRSRequestCreateView(generic.TemplateView):
    template_name = 'mrsrequest/form.html'
    base = 'base.html'

    def get(self, request, *args, **kwargs):
        self.object = MRSRequest()
        self.object.allow(request)
        self.mrsrequest_uuid = str(self.object.id)
        self.caisse_form = CaisseVoteForm(prefix='other')

        self.forms = collections.OrderedDict([
            ('mrsrequest', MRSRequestCreateForm(instance=self.object)),
            ('person', PersonForm(
                initial={k: v for k, v in request.GET.items()})),
            ('transport', TransportIterativeForm()),
            ('certify', CertifyForm()),
        ])

        return super().get(request, *args, **kwargs)

    def has_perm(self):
        if not self.mrsrequest_uuid:  # require mrsrequest_uuid on post
            return False

        # view is supposed to create a new request
        try:
            if MRSRequest.objects.filter(id=self.mrsrequest_uuid):
                return False
        except ValidationError:  # badly formated uuid
            return False

        # Let's not even load the object from db if request aint allowed
        if not MRSRequest(id=self.mrsrequest_uuid).is_allowed(self.request):
            return False

        return True

    def post(self, request, *args, **kwargs):
        self.mrsrequest_uuid = self.request.POST.get('mrsrequest_uuid', None)
        caisse = request.POST.get('caisse', None)

        if caisse == 'other':
            return self.post_caisse(request, *args, **kwargs)
        else:
            return self.post_mrsrequest(request, *args, **kwargs)

    def post_caisse(self, request, *args, **kwargs):
        # needed for rendering
        self.forms = collections.OrderedDict([
            ('mrsrequest', MRSRequestCreateForm(
                initial={'caisse': 'other'},
                mrsrequest_uuid=self.mrsrequest_uuid
            )),
            ('person', PersonForm()),
            ('certify', CertifyForm()),
        ])
        self.forms['transport'] = TransportIterativeForm(
            instance=Transport(mrsrequest_id=self.mrsrequest_uuid),
        )

        self.caisse_form = CaisseVoteForm(request.POST, prefix='other')
        with transaction.atomic():
            self.success_caisse = (
                self.caisse_form.is_valid() and self.save_caisse())

        return generic.TemplateView.get(self, request, *args, **kwargs)

    def save_caisse(self):
        caisse = self.caisse_form.cleaned_data['caisse']

        email = self.caisse_form.cleaned_data.get('email', None)
        if email:
            Email.objects.create(email=email, caisse=caisse)

        caisse.score += 1
        caisse.save()

        return True

    def post_mrsrequest(self, request, *args, **kwargs):
        if not self.has_perm():
            return http.HttpResponseBadRequest()

        # for display
        self.caisse_form = CaisseVoteForm(prefix='other')

        self.forms = collections.OrderedDict([
            ('mrsrequest', MRSRequestCreateForm(
                request.POST,
                mrsrequest_uuid=self.mrsrequest_uuid
            )),
            ('person', PersonForm(request.POST)),
            ('certify', CertifyForm(request.POST)),
        ])
        self.forms['transport'] = TransportIterativeForm(
            request.POST,
            instance=Transport(mrsrequest_id=self.mrsrequest_uuid),
        )
        for key, value in self.request.POST.items():
            if '-date_return' not in key:
                continue

            number = key.split('-')[0]
            self.forms['transport-{}'.format(number)] = form = TransportForm(
                request.POST,
                instance=Transport(mrsrequest_id=self.mrsrequest_uuid),
                prefix=number,
            )
            form.fields['date_depart'].label += ' {}'.format(number)
            form.fields['date_return'].label += ' {}'.format(number)

        with transaction.atomic():
            self.success = not self.form_errors() and self.save_mrsrequest()

        return generic.TemplateView.get(self, request, *args, **kwargs)

    def save_mrsrequest(self):
        self.forms['mrsrequest'].instance.insured = (
            self.forms['person'].get_or_create())
        self.forms['mrsrequest'].instance.creation_ip = get_client_ip(
            self.request)[0]
        self.object = self.forms['mrsrequest'].save()
        self.forms['transport'].save()
        for name, form in self.forms.items():
            if 'transport-' not in name:
                continue
            form.save()

        email = EmailMessage(
            template.loader.get_template(
                'mrsrequest/success_mail_title.txt'
            ).render(dict(view=self)).strip(),
            template.loader.get_template(
                'mrsrequest/success_mail_body.txt'
            ).render(dict(view=self)).strip(),
            settings.DEFAULT_FROM_EMAIL,
            [self.object.insured.email],
            reply_to=[settings.TEAM_EMAIL],
        )
        email.send()

        return True

    def form_errors(self):
        return [
            (form.errors, form.non_field_errors)
            for form in self.forms.values()
            if not form.is_valid()
        ]