You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

abstract_models.py 15KB

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