Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

abstract_models.py 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. import hashlib
  2. import random
  3. from django.conf import settings
  4. from django.contrib.auth import models as auth_models
  5. from django.core.urlresolvers import reverse
  6. from django.db import models
  7. from django.template import Template, Context, TemplateDoesNotExist
  8. from django.template.loader import get_template
  9. from django.utils import timezone
  10. from django.utils.translation import ugettext_lazy as _
  11. from oscar.apps.customer.managers import CommunicationTypeManager
  12. from oscar.core.compat import AUTH_USER_MODEL
  13. if hasattr(auth_models, 'BaseUserManager'):
  14. # Only define custom UserModel when Django >= 1.5
  15. class UserManager(auth_models.BaseUserManager):
  16. def create_user(self, email, password=None, **extra_fields):
  17. """
  18. Creates and saves a User with the given username, email and
  19. password.
  20. """
  21. now = timezone.now()
  22. if not email:
  23. raise ValueError('The given email must be set')
  24. email = UserManager.normalize_email(email)
  25. user = self.model(
  26. email=email, is_staff=False, is_active=True,
  27. is_superuser=False,
  28. last_login=now, date_joined=now, **extra_fields)
  29. user.set_password(password)
  30. user.save(using=self._db)
  31. return user
  32. def create_superuser(self, email, password, **extra_fields):
  33. u = self.create_user(email, password, **extra_fields)
  34. u.is_staff = True
  35. u.is_active = True
  36. u.is_superuser = True
  37. u.save(using=self._db)
  38. return u
  39. class AbstractUser(auth_models.AbstractBaseUser,
  40. auth_models.PermissionsMixin):
  41. """
  42. An abstract base user suitable for use in Oscar projects.
  43. This is basically a copy of the core AbstractUser model but without a
  44. username field
  45. """
  46. email = models.EmailField(_('email address'), unique=True)
  47. first_name = models.CharField(
  48. _('First name'), max_length=255, blank=True)
  49. last_name = models.CharField(
  50. _('Last name'), max_length=255, blank=True)
  51. is_staff = models.BooleanField(
  52. _('Staff status'), default=False,
  53. help_text=_('Designates whether the user can log into this admin '
  54. 'site.'))
  55. is_active = models.BooleanField(
  56. _('Active'), default=True,
  57. help_text=_('Designates whether this user should be treated as '
  58. 'active. Unselect this instead of deleting accounts.'))
  59. date_joined = models.DateTimeField(_('date joined'),
  60. default=timezone.now)
  61. objects = UserManager()
  62. USERNAME_FIELD = 'email'
  63. class Meta:
  64. verbose_name = _('User')
  65. verbose_name_plural = _('Users')
  66. abstract = True
  67. def get_full_name(self):
  68. full_name = '%s %s' % (self.first_name, self.last_name)
  69. return full_name.strip()
  70. def get_short_name(self):
  71. return self.first_name
  72. class AbstractEmail(models.Model):
  73. """
  74. This is a record of all emails sent to a customer.
  75. Normally, we only record order-related emails.
  76. """
  77. user = models.ForeignKey(AUTH_USER_MODEL, related_name='emails',
  78. verbose_name=_("User"))
  79. subject = models.TextField(_('Subject'), max_length=255)
  80. body_text = models.TextField(_("Body Text"))
  81. body_html = models.TextField(_("Body HTML"), blank=True, null=True)
  82. date_sent = models.DateTimeField(_("Date Sent"), auto_now_add=True)
  83. class Meta:
  84. abstract = True
  85. verbose_name = _('Email')
  86. verbose_name_plural = _('Emails')
  87. def __unicode__(self):
  88. return _("Email to %(user)s with subject '%(subject)s'") % {
  89. 'user': self.user.username, 'subject': self.subject}
  90. class AbstractCommunicationEventType(models.Model):
  91. """
  92. A 'type' of communication. Like a order confirmation email.
  93. """
  94. # Code used for looking up this event programmatically.
  95. # eg. PASSWORD_RESET
  96. code = models.SlugField(_('Code'), max_length=128)
  97. #: Name is the friendly description of an event for use in the admin
  98. name = models.CharField(
  99. _('Name'), max_length=255,
  100. help_text=_("This is just used for organisational purposes"))
  101. # We allow communication types to be categorised
  102. ORDER_RELATED = _('Order related')
  103. USER_RELATED = _('User related')
  104. category = models.CharField(_('Category'), max_length=255,
  105. default=ORDER_RELATED)
  106. # Template content for emails
  107. email_subject_template = models.CharField(
  108. _('Email Subject Template'), max_length=255, blank=True, null=True)
  109. email_body_template = models.TextField(
  110. _('Email Body Template'), blank=True, null=True)
  111. email_body_html_template = models.TextField(
  112. _('Email Body HTML Template'), blank=True, null=True,
  113. help_text=_("HTML template"))
  114. # Template content for SMS messages
  115. sms_template = models.CharField(_('SMS Template'), max_length=170,
  116. blank=True, null=True,
  117. help_text=_("SMS template"))
  118. date_created = models.DateTimeField(_("Date Created"), auto_now_add=True)
  119. date_updated = models.DateTimeField(_("Date Updated"), auto_now=True)
  120. objects = CommunicationTypeManager()
  121. # File templates
  122. email_subject_template_file = 'customer/emails/commtype_%s_subject.txt'
  123. email_body_template_file = 'customer/emails/commtype_%s_body.txt'
  124. email_body_html_template_file = 'customer/emails/commtype_%s_body.html'
  125. sms_template_file = 'customer/sms/commtype_%s_body.txt'
  126. class Meta:
  127. abstract = True
  128. verbose_name = _("Communication event type")
  129. verbose_name_plural = _("Communication event types")
  130. def get_messages(self, ctx=None):
  131. """
  132. Return a dict of templates with the context merged in
  133. We look first at the field templates but fail over to
  134. a set of file templates that follow a conventional path.
  135. """
  136. code = self.code.lower()
  137. # Build a dict of message name to Template instances
  138. templates = {'subject': 'email_subject_template',
  139. 'body': 'email_body_template',
  140. 'html': 'email_body_html_template',
  141. 'sms': 'sms_template'}
  142. for name, attr_name in templates.items():
  143. field = getattr(self, attr_name, None)
  144. if field is not None:
  145. # Template content is in a model field
  146. templates[name] = Template(field)
  147. else:
  148. # Model field is empty - look for a file template
  149. template_name = getattr(self, "%s_file" % attr_name) % code
  150. try:
  151. templates[name] = get_template(template_name)
  152. except TemplateDoesNotExist:
  153. templates[name] = None
  154. # Pass base URL for serving images within HTML emails
  155. if ctx is None:
  156. ctx = {}
  157. ctx['static_base_url'] = getattr(
  158. settings, 'OSCAR_STATIC_BASE_URL', None)
  159. messages = {}
  160. for name, template in templates.items():
  161. messages[name] = template.render(Context(ctx)) if template else ''
  162. # Ensure the email subject doesn't contain any newlines
  163. messages['subject'] = messages['subject'].replace("\n", "")
  164. messages['subject'] = messages['subject'].replace("\r", "")
  165. return messages
  166. def __unicode__(self):
  167. return self.name
  168. def is_order_related(self):
  169. return self.category == self.ORDER_RELATED
  170. def is_user_related(self):
  171. return self.category == self.USER_RELATED
  172. class AbstractNotification(models.Model):
  173. recipient = models.ForeignKey(AUTH_USER_MODEL,
  174. related_name='notifications', db_index=True)
  175. # Not all notifications will have a sender.
  176. sender = models.ForeignKey(AUTH_USER_MODEL, null=True)
  177. # HTML is allowed in this field as it can contain links
  178. subject = models.CharField(max_length=255)
  179. body = models.TextField()
  180. # Some projects may want to categorise their notifications. You may want
  181. # to use this field to show a different icons next to the notification.
  182. category = models.CharField(max_length=255, null=True)
  183. INBOX, ARCHIVE = 'Inbox', 'Archive'
  184. choices = (
  185. (INBOX, _(INBOX)),
  186. (ARCHIVE, _(ARCHIVE)))
  187. location = models.CharField(max_length=32, choices=choices,
  188. default=INBOX)
  189. date_sent = models.DateTimeField(auto_now_add=True)
  190. date_read = models.DateTimeField(blank=True, null=True)
  191. class Meta:
  192. ordering = ('-date_sent',)
  193. abstract = True
  194. def __unicode__(self):
  195. return self.subject
  196. def archive(self):
  197. self.location = self.ARCHIVE
  198. self.save()
  199. archive.alters_data = True
  200. @property
  201. def is_read(self):
  202. return self.date_read is not None
  203. class AbstractProductAlert(models.Model):
  204. """
  205. An alert for when a product comes back in stock
  206. """
  207. product = models.ForeignKey('catalogue.Product')
  208. # A user is only required if the notification is created by a
  209. # registered user, anonymous users will only have an email address
  210. # attached to the notification
  211. user = models.ForeignKey(AUTH_USER_MODEL, db_index=True, blank=True,
  212. null=True, related_name="alerts",
  213. verbose_name=_('User'))
  214. email = models.EmailField(_("Email"), db_index=True, blank=True, null=True)
  215. # This key are used to confirm and cancel alerts for anon users
  216. key = models.CharField(_("Key"), max_length=128, null=True, db_index=True)
  217. # An alert can have two different statuses for authenticated
  218. # users ``ACTIVE`` and ``INACTIVE`` and anonymous users have an
  219. # additional status ``UNCONFIRMED``. For anonymous users a confirmation
  220. # and unsubscription key are generated when an instance is saved for
  221. # the first time and can be used to confirm and unsubscribe the
  222. # notifications.
  223. UNCONFIRMED, ACTIVE, CANCELLED, CLOSED = (
  224. 'Unconfirmed', 'Active', 'Cancelled', 'Closed')
  225. STATUS_CHOICES = (
  226. (UNCONFIRMED, _('Not yet confirmed')),
  227. (ACTIVE, _('Active')),
  228. (CANCELLED, _('Cancelled')),
  229. (CLOSED, _('Closed')),
  230. )
  231. status = models.CharField(_("Status"), max_length=20,
  232. choices=STATUS_CHOICES, default=ACTIVE)
  233. date_created = models.DateTimeField(_("Date created"), auto_now_add=True)
  234. date_confirmed = models.DateTimeField(_("Date confirmed"), blank=True,
  235. null=True)
  236. date_cancelled = models.DateTimeField(_("Date cancelled"), blank=True,
  237. null=True)
  238. date_closed = models.DateTimeField(_("Date closed"), blank=True, null=True)
  239. class Meta:
  240. abstract = True
  241. @property
  242. def is_anonymous(self):
  243. return self.user is None
  244. @property
  245. def can_be_confirmed(self):
  246. return self.status == self.UNCONFIRMED
  247. @property
  248. def can_be_cancelled(self):
  249. return self.status == self.ACTIVE
  250. @property
  251. def is_cancelled(self):
  252. return self.status == self.CANCELLED
  253. @property
  254. def is_active(self):
  255. return self.status == self.ACTIVE
  256. def confirm(self):
  257. self.status = self.ACTIVE
  258. self.date_confirmed = timezone.now()
  259. self.save()
  260. confirm.alters_data = True
  261. def cancel(self):
  262. self.status = self.CANCELLED
  263. self.date_cancelled = timezone.now()
  264. self.save()
  265. cancel.alters_data = True
  266. def close(self):
  267. self.status = self.CLOSED
  268. self.date_closed = timezone.now()
  269. self.save()
  270. close.alters_data = True
  271. def get_email_address(self):
  272. if self.user:
  273. return self.user.email
  274. else:
  275. return self.email
  276. def save(self, *args, **kwargs):
  277. if not self.id and not self.user:
  278. self.key = self.get_random_key()
  279. self.status = self.UNCONFIRMED
  280. # Ensure date fields get updated when saving from modelform (which just
  281. # calls save, and doesn't call the methods cancel(), confirm() etc).
  282. if self.status == self.CANCELLED and self.date_cancelled is None:
  283. self.date_cancelled = timezone.now()
  284. if not self.user and self.status == self.ACTIVE \
  285. and self.date_confirmed is None:
  286. self.date_confirmed = timezone.now()
  287. if self.status == self.CLOSED and self.date_closed is None:
  288. self.date_closed = timezone.now()
  289. return super(AbstractProductAlert, self).save(*args, **kwargs)
  290. def get_random_key(self):
  291. """
  292. Get a random generated key based on SHA-1 and email address
  293. """
  294. salt = hashlib.sha1(str(random.random())).hexdigest()
  295. return hashlib.sha1(salt + self.email).hexdigest()
  296. def get_confirm_url(self):
  297. return reverse('customer:alerts-confirm', kwargs={'key': self.key})
  298. def get_cancel_url(self):
  299. return reverse('customer:alerts-cancel-by-key', kwargs={'key':
  300. self.key})