class Donation(ExportModelOperationsMixin('Donation'), models.Model): @staticmethod def _created_default(): return timezone.now() @staticmethod def _due_default(): return timezone.now() + timedelta(days=7) @staticmethod def _mref_default(): return "ONDON" + str(time.time()) created = models.DateTimeField(default=_created_default.__func__) name = models.CharField(max_length=255) iban = models.CharField(max_length=34) bic = models.CharField(max_length=11, blank=True) amount = models.DecimalField(max_digits=8, decimal_places=2) message = models.CharField(max_length=255, blank=True) due = models.DateTimeField(default=_due_default.__func__) mref = models.CharField(max_length=32, default=_mref_default.__func__) email = models.EmailField(max_length=255, blank=True) class Meta: managed = False
class AuthenticatedChangeEmailUserAction(AuthenticatedUserAction): new_email = models.EmailField() class Meta: managed = False @property def _state_fields(self): return super()._state_fields + [self.new_email] def _act(self): self.user.change_email(self.new_email)
class User(AbstractBaseUser): email = models.EmailField( verbose_name='email address', max_length=191, unique=True, ) is_active = models.BooleanField(default=True) is_admin = models.BooleanField(default=False) registration_remote_ip = models.CharField(max_length=1024, blank=True) locked = models.DateTimeField(null=True, blank=True) created = models.DateTimeField(auto_now_add=True) limit_domains = models.IntegerField(default=settings.LIMIT_USER_DOMAIN_COUNT_DEFAULT, null=True, blank=True) dyn = models.BooleanField(default=False) objects = MyUserManager() USERNAME_FIELD = 'email' REQUIRED_FIELDS = [] def get_full_name(self): return self.email def get_short_name(self): return self.email def get_or_create_first_token(self): try: token = Token.objects.filter(user=self).earliest('created') except Token.DoesNotExist: token = Token.objects.create(user=self) return token.key def __str__(self): return self.email # noinspection PyMethodMayBeStatic def has_perm(self, *_): """Does the user have a specific permission?""" # Simplest possible answer: Yes, always return True # noinspection PyMethodMayBeStatic def has_module_perms(self, *_): """Does the user have permissions to view the app `app_label`?""" # Simplest possible answer: Yes, always return True @property def is_staff(self): """Is the user a member of staff?""" # Simplest possible answer: All admins are staff return self.is_admin
class Donation(ExportModelOperationsMixin('Donation'), models.Model): created = models.DateTimeField(default=get_default_value_created) name = models.CharField(max_length=255) iban = models.CharField(max_length=34) bic = models.CharField(max_length=11, blank=True) amount = models.DecimalField(max_digits=8, decimal_places=2) message = models.CharField(max_length=255, blank=True) due = models.DateTimeField(default=get_default_value_due) mref = models.CharField(max_length=32, default=get_default_value_mref) email = models.EmailField(max_length=255, blank=True) class Meta: managed = False
class Donation(models.Model): created = models.DateTimeField(default=get_default_value_created) name = models.CharField(max_length=255) iban = models.CharField(max_length=34) bic = models.CharField(max_length=11) amount = models.DecimalField(max_digits=8, decimal_places=2) message = models.CharField(max_length=255, blank=True) due = models.DateTimeField(default=get_default_value_due) mref = models.CharField(max_length=32, default=get_default_value_mref) email = models.EmailField(max_length=255, blank=True) def save(self, *args, **kwargs): self.iban = self.iban[:6] + "xxx" # do NOT save account details super().save(*args, **kwargs) class Meta: ordering = ('created',)
class User(ExportModelOperationsMixin('User'), AbstractBaseUser): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) email = models.EmailField( verbose_name='email address', max_length=191, unique=True, ) is_active = models.BooleanField(default=True) is_admin = models.BooleanField(default=False) created = models.DateTimeField(auto_now_add=True) limit_domains = models.IntegerField( default=settings.LIMIT_USER_DOMAIN_COUNT_DEFAULT, null=True, blank=True) objects = MyUserManager() USERNAME_FIELD = 'email' REQUIRED_FIELDS = [] def get_full_name(self): return self.email def get_short_name(self): return self.email def __str__(self): return self.email # noinspection PyMethodMayBeStatic def has_perm(self, *_): """Does the user have a specific permission?""" # Simplest possible answer: Yes, always return True # noinspection PyMethodMayBeStatic def has_module_perms(self, *_): """Does the user have permissions to view the app `app_label`?""" # Simplest possible answer: Yes, always return True @property def is_staff(self): """Is the user a member of staff?""" # Simplest possible answer: All admins are staff return self.is_admin def activate(self): self.is_active = True self.save() def change_email(self, email): old_email = self.email self.email = email self.validate_unique() self.save() self.send_email('change-email-confirmation-old-email', recipient=old_email) def change_password(self, raw_password): self.set_password(raw_password) self.save() self.send_email('password-change-confirmation') def send_email(self, reason, context=None, recipient=None): fast_lane = 'email_fast_lane' slow_lane = 'email_slow_lane' lanes = { 'activate': slow_lane, 'activate-with-domain': slow_lane, 'change-email': slow_lane, 'change-email-confirmation-old-email': fast_lane, 'password-change-confirmation': fast_lane, 'reset-password': fast_lane, 'delete-user': fast_lane, 'domain-dyndns': fast_lane, } if reason not in lanes: raise ValueError( f'Cannot send email to user {self.pk} without a good reason: {reason}' ) context = context or {} context.setdefault( 'link_expiration_hours', settings.VALIDITY_PERIOD_VERIFICATION_SIGNATURE // timedelta(hours=1)) content = get_template(f'emails/{reason}/content.txt').render(context) content += f'\nSupport Reference: user_id = {self.pk}\n' footer = get_template('emails/footer.txt').render() logger.warning( f'Queuing email for user account {self.pk} (reason: {reason})') return EmailMessage( subject=get_template(f'emails/{reason}/subject.txt').render( context).strip(), body=content + footer, from_email=get_template('emails/from.txt').render(), to=[recipient or self.email], connection=get_connection(lane=lanes[reason], debug={ 'user': self.pk, 'reason': reason })).send()