models.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. from django.conf import settings
  2. from django.db import models
  3. from django.contrib.auth.models import (
  4. BaseUserManager, AbstractBaseUser
  5. )
  6. from django.utils import timezone
  7. from desecapi import pdns
  8. import datetime, time
  9. class MyUserManager(BaseUserManager):
  10. def create_user(self, email, password=None, registration_remote_ip=None, captcha_required=False):
  11. """
  12. Creates and saves a User with the given email, date of
  13. birth and password.
  14. """
  15. if not email:
  16. raise ValueError('Users must have an email address')
  17. user = self.model(
  18. email=self.normalize_email(email),
  19. registration_remote_ip=registration_remote_ip,
  20. captcha_required=captcha_required,
  21. )
  22. user.set_password(password)
  23. user.save(using=self._db)
  24. return user
  25. def create_superuser(self, email, password):
  26. """
  27. Creates and saves a superuser with the given email, date of
  28. birth and password.
  29. """
  30. user = self.create_user(email,
  31. password=password
  32. )
  33. user.is_admin = True
  34. user.save(using=self._db)
  35. return user
  36. class User(AbstractBaseUser):
  37. email = models.EmailField(
  38. verbose_name='email address',
  39. max_length=191,
  40. unique=True,
  41. )
  42. is_active = models.BooleanField(default=True)
  43. is_admin = models.BooleanField(default=False)
  44. registration_remote_ip = models.CharField(max_length=1024, blank=True)
  45. captcha_required = models.BooleanField(default=False)
  46. created = models.DateTimeField(auto_now_add=True)
  47. objects = MyUserManager()
  48. USERNAME_FIELD = 'email'
  49. REQUIRED_FIELDS = []
  50. def get_full_name(self):
  51. return self.email
  52. def get_short_name(self):
  53. return self.email
  54. def __str__(self):
  55. return self.email
  56. def has_perm(self, perm, obj=None):
  57. "Does the user have a specific permission?"
  58. # Simplest possible answer: Yes, always
  59. return True
  60. def has_module_perms(self, app_label):
  61. "Does the user have permissions to view the app `app_label`?"
  62. # Simplest possible answer: Yes, always
  63. return True
  64. @property
  65. def is_staff(self):
  66. "Is the user a member of staff?"
  67. # Simplest possible answer: All admins are staff
  68. return self.is_admin
  69. def unlock(self):
  70. self.captcha_required = False
  71. for domain in self.domain_set:
  72. domain.pdns_sync()
  73. self.save()
  74. class Domain(models.Model):
  75. created = models.DateTimeField(auto_now_add=True)
  76. updated = models.DateTimeField(null=True)
  77. name = models.CharField(max_length=191, unique=True)
  78. arecord = models.CharField(max_length=255, blank=True)
  79. aaaarecord = models.CharField(max_length=1024, blank=True)
  80. dyn = models.BooleanField(default=False)
  81. owner = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='domains')
  82. def pdns_resync(self):
  83. """
  84. Make sure that pdns gets the latest information about this domain/zone.
  85. Re-Syncing is relatively expensive and should not happen routinely.
  86. """
  87. # Create zone if needed
  88. if not pdns.zone_exists(self.name):
  89. pdns.create_native_zone(self.name)
  90. # update zone to latest information
  91. pdns.set_dyn_records(self.name, self.arecord, self.aaaarecord)
  92. def pdns_sync(self):
  93. """
  94. Command pdns updates as indicated by the local changes.
  95. """
  96. if self.owner.captcha_required:
  97. # suspend all updates
  98. pass
  99. new_domain = self.id is None
  100. changes_required = False
  101. # if this zone is new, create it
  102. if new_domain:
  103. pdns.create_native_zone(self.name)
  104. # for existing domains, see if records are changed
  105. if not new_domain:
  106. orig_domain = Domain.objects.get(id=self.id)
  107. changes_required = self.arecord != orig_domain.arecord or self.aaaarecord != orig_domain.aaaarecord
  108. # make changes if necessary
  109. if changes_required:
  110. pdns.set_dyn_records(self.name, self.arecord, self.aaaarecord)
  111. def save(self, *args, **kwargs):
  112. self.updated = timezone.now()
  113. self.pdns_sync()
  114. super(Domain, self).save(*args, **kwargs)
  115. class Meta:
  116. ordering = ('created',)
  117. def get_default_value_created():
  118. return timezone.now()
  119. def get_default_value_due():
  120. return timezone.now() + datetime.timedelta(days=7)
  121. def get_default_value_mref():
  122. return "ONDON" + str((timezone.now() - timezone.datetime(1970,1,1,tzinfo=timezone.utc)).total_seconds())
  123. class Donation(models.Model):
  124. created = models.DateTimeField(default=get_default_value_created)
  125. name = models.CharField(max_length=255)
  126. iban = models.CharField(max_length=34)
  127. bic = models.CharField(max_length=11)
  128. amount = models.DecimalField(max_digits=8,decimal_places=2)
  129. message = models.CharField(max_length=255, blank=True)
  130. due = models.DateTimeField(default=get_default_value_due)
  131. mref = models.CharField(max_length=32,default=get_default_value_mref)
  132. email = models.EmailField(max_length=255, blank=True)
  133. def save(self, *args, **kwargs):
  134. self.iban = self.iban[:6] + "xxx" # do NOT save account details
  135. super(Donation, self).save(*args, **kwargs) # Call the "real" save() method.
  136. class Meta:
  137. ordering = ('created',)