class MessageFactory(factory.django.DjangoModelFactory): class Meta: model = Message class Params: html_content = '{} {}'.format( settings.OPTOUTS['UNSUBSCRIBE_PLACEHOLDER'], settings.HOSTED['WEB_LINK_PLACEHOLDER']) name = factory.LazyAttribute(lambda x: faker.sentence(nb_words=3)) creation_date = factory.LazyAttribute(lambda x: datetime.now(pytz.UTC)) sender_email = factory.LazyAttribute(lambda x: faker.email()) sender_name = factory.LazyAttribute(lambda x: faker.name()) identifier = factory.LazyAttribute(lambda x: mk_base64_uuid()) html = factory.LazyAttribute(lambda o: '<body>{} {}</body>'.format( o.html_content, faker.text(max_nb_chars=100))) @factory.post_generation def auto_create_sending_domain(self, create, extracted, create_sending_domain=True): if not create: return if create_sending_domain: SendingDomain.objects.create(name=self.name, organization=self.author.organization)
class UserFactory(factory.django.DjangoModelFactory): class Meta: model = MunchUser organization = factory.SubFactory(OrganizationFactory) identifier = factory.LazyAttribute(lambda x: faker.email()) last_login = factory.LazyAttribute(lambda x: timezone.now()) secret = factory.LazyAttribute(lambda x: mk_base64_uuid()) is_active = True password = factory.PostGenerationMethodCall('set_password', 'password') @factory.post_generation def groups(self, create, extracted, **kwargs): if not create: return if extracted: for group in extracted: if group in [ 'users', 'collaborators', 'managers', 'administrators']: self.groups.add(Group.objects.filter(name=group)[0])
def send_simple_envelope(self, envelope, identifier=None, priority=50): if not identifier: identifier = envelope.headers.get( settings.MAILSEND['X_MESSAGE_ID_HEADER']) if identifier is None: identifier = mk_base64_uuid() envelope.headers.add_header( settings.MAILSEND['X_MESSAGE_ID_HEADER'], identifier) raw_mail, created = RawMail.objects.get_or_create( content=envelope.message) mail = Mail.objects.create(identifier=identifier, headers=dict(envelope.headers), message=raw_mail, sender=envelope.sender, recipient=envelope.recipients[0]) mailstatus = self.mailstatus_class(mail=mail, status=AbstractMailStatus.QUEUED, destination_domain=extract_domain( envelope.recipients[0])) self.record_status_task(mailstatus, identifier, settings.MAILSEND.get('SMTP_WORKER_EHLO_AS')) self.send_envelope(envelope, priority=priority)
def regen_credentials(self): self.username = mk_base64_uuid() self.secret = mk_base64_uuid()
def regen_secret(self): self.secret = mk_base64_uuid()
def get_mail_identifier(): return mk_base64_uuid('t-')
def apply(self, envelope): envelope.headers[settings.TRANSACTIONAL[ 'X_USER_ID_HEADER']] = str(envelope.user.pk) envelope.headers[settings.TRANSACTIONAL[ 'X_MESSAGE_ID_HEADER']] = mk_base64_uuid(prefix='t-')
def send_simple_envelope(self, envelope): self._record_sending(mk_base64_uuid(), envelope.recipients[0])
def send_simple_message(self, message): self._record_sending(mk_base64_uuid(), message.to[0])
def send_messages(self, email_messages): if not email_messages: return for message in email_messages: self._record_sending(mk_base64_uuid(), message.to[0]) return len(email_messages)
def save(self, *args, **kwargs): if not self.id: # put a clear preview identifier self.identifier = 'preview' + mk_base64_uuid() super().save(*args, **kwargs)