def password_reset(self): created = not self.password password = secrets.token_urlsafe(16) self.set_password(password) self.save() password_email_template = template.loader.get_template( 'mrsuser/user_password_email.txt', ) Caller( callback='djcall.django.email_send', kwargs=dict( subject=( '[MRS] Votre mot de passe' if created else '[MRS] Votre nouveau mot de passe' ), body=password_email_template.render(dict( created=created, user=self, password=password, BASE_URL=settings.BASE_URL, )), to=[self.email], reply_to=[settings.TEAM_EMAIL], ) ).spool('mail')
def post(self, request, *args, **kwargs): if self.object.status != self.object.STATUS_NEW: return http.HttpResponseBadRequest() self.object.status = self.object.STATUS_CANCELED self.object.status_datetime = datetime.now() self.object.save() self.object.logentries.create( action=MRSRequest.STATUS_CANCELED, comment='Annulation', ) body = template.loader.get_template( 'mrsrequest/cancelation_email.txt' ).render(dict(object=self.object, base_url=settings.BASE_URL)).strip() Caller( callback='djcall.django.email_send', kwargs=dict( subject=f'MRS: Annulation demande {self.object.display_id}', body=body.strip(), to=[self.object.insured.email], reply_to=[self.object.caisse.liquidation_email], ) ).spool('mail') return generic.TemplateView.get(self, request, *args, **kwargs)
def test_call_execute_result(): call = Caller( callback='djcall.test_models.mockito', kwargs=dict(id=1), ).call() assert call.result == 1 assert call.status == call.STATUS_SUCCESS assert call.caller.status == call.STATUS_SUCCESS
def form_valid(self, form): Caller( callback='djcall.django.email_send', kwargs=form.get_email_kwargs(), ).spool('mail') form.save() self.success = True return generic.TemplateView.get(self, self.request)
def mail_insured(self, mrsrequest=None): mrsrequest = mrsrequest or self.object Caller(callback='djcall.django.email_send', kwargs=dict( subject=self.mail_render('title', mrsrequest), body=self.mail_render('body', mrsrequest), to=[(mrsrequest or self.object).insured.email], reply_to=[settings.TEAM_EMAIL], )).spool('mail')
def form_valid(self): Caller(callback='djcall.django.email_send', kwargs=dict( subject=self.form.cleaned_data['subject'], body=self.form.cleaned_data['body'], to=[self.object.insured.email], reply_to=[self.object.caisse.liquidation_email], )).spool('mail') self.form.cleaned_data['template'].counter += 1 self.form.cleaned_data['template'].save() return super().form_valid()
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 stat_update(sender, instance, **kwargs): Caller( callback='mrsstat.models.update_stat_for_mrsrequest', kwargs=dict(pk=instance.pk), ).spool('stat')
def post_mrsrequest(self, request, *args, **kwargs): if not self.has_perm(): return http.HttpResponseBadRequest() 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)
def test_str(): assert str(Caller(callback='lol')) == 'lol()' assert str(Caller(callback='lol', kwargs=dict(a=1, b=2))) == 'lol(a=1, b=2)'
def test_python_callback(): caller = Caller(callback='djcall.models.Caller.objects.all') assert caller.python_callback == Caller.objects.all