Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Updating Observation feature.

git-svn-id: https://django-notification.googlecode.com/svn/trunk@92 590c3fc9-4838-0410-bb95-17a0c9b37ca9
  • Loading branch information...
commit 02250bd70cc8988a855c3aeb63b11c15054bf83e 1 parent 270f71e
eduardo.padoan authored

Showing 1 changed file with 33 additions and 33 deletions. Show diff stats Hide diff stats

  1. +33 33 notification/models.py
66 notification/models.py
@@ -23,7 +23,7 @@
23 23
24 24
25 25 class NoticeType(models.Model):
26   -
  26 +
27 27 label = models.CharField(_('label'), max_length=40)
28 28 display = models.CharField(_('display'), max_length=50)
29 29 description = models.CharField(_('description'), max_length=100)
@@ -33,7 +33,7 @@ class NoticeType(models.Model):
33 33
34 34 def __unicode__(self):
35 35 return self.label
36   -
  36 +
37 37 class Meta:
38 38 verbose_name = _("notice type")
39 39 verbose_name_plural = _("notice types")
@@ -54,12 +54,12 @@ class NoticeSetting(models.Model):
54 54 Indicates, for a given user, whether to send notifications
55 55 of a given type to a given medium.
56 56 """
57   -
  57 +
58 58 user = models.ForeignKey(User, verbose_name=_('user'))
59 59 notice_type = models.ForeignKey(NoticeType, verbose_name=_('notice type'))
60 60 medium = models.CharField(_('medium'), max_length=1, choices=NOTICE_MEDIA)
61 61 send = models.BooleanField(_('send'))
62   -
  62 +
63 63 class Meta:
64 64 verbose_name = _("notice setting")
65 65 verbose_name_plural = _("notice settings")
@@ -85,7 +85,7 @@ def notices_for(self, user, archived=False, unseen=None):
85 85
86 86 If archived=False, it only include notices not archived.
87 87 If archived=True, it returns all notices for that user.
88   -
  88 +
89 89 If unseen=None, it includes all notices.
90 90 If unseen=True, return only unseen notices.
91 91 If unseen=False, return only seen notices.
@@ -106,27 +106,27 @@ def unseen_count_for(self, user):
106 106 return self.filter(user=user, unseen=True).count()
107 107
108 108 class Notice(models.Model):
109   -
  109 +
110 110 user = models.ForeignKey(User, verbose_name=_('user'))
111 111 message = models.TextField(_('message'))
112 112 notice_type = models.ForeignKey(NoticeType, verbose_name=_('notice type'))
113 113 added = models.DateTimeField(_('added'), default=datetime.datetime.now)
114 114 unseen = models.BooleanField(_('unseen'), default=True)
115 115 archived = models.BooleanField(_('archived'), default=False)
116   -
  116 +
117 117 objects = NoticeManager()
118   -
  118 +
119 119 def __unicode__(self):
120 120 return self.message
121   -
  121 +
122 122 def archive(self):
123 123 self.archived = True
124 124 self.save()
125   -
  125 +
126 126 def is_unseen(self):
127 127 """
128 128 returns value of self.unseen but also changes it to false.
129   -
  129 +
130 130 Use this in a template to mark an unseen notice differently the first
131 131 time it is shown.
132 132 """
@@ -135,7 +135,7 @@ def is_unseen(self):
135 135 self.unseen = False
136 136 self.save()
137 137 return unseen
138   -
  138 +
139 139 class Meta:
140 140 ordering = ["-added"]
141 141 verbose_name = _("notice")
@@ -144,11 +144,11 @@ class Meta:
144 144 @models.permalink
145 145 def get_absolute_url(self):
146 146 return ("notification_notice", [str(self.pk)])
147   -
  147 +
148 148 def create_notice_type(label, display, description, default=2):
149 149 """
150 150 Creates a new NoticeType.
151   -
  151 +
152 152 This is intended to be used by other apps as a post_syncdb manangement step.
153 153 """
154 154 try:
@@ -186,22 +186,22 @@ def get_formatted_messages(formats, label, context):
186 186 def send(recipient, label, extra_context={}, issue_notice=True):
187 187 """
188 188 Creates a new notice.
189   -
  189 +
190 190 This is intended to be how other apps create new notices.
191   -
  191 +
192 192 notification.send(user, 'friends_invite_sent', {
193 193 'spam': 'eggs',
194 194 'foo': 'bar',
195 195 )
196 196 """
197 197 notice_type = NoticeType.objects.get(label=label)
198   -
  198 +
199 199 current_site = Site.objects.get_current()
200 200 notices_url = u"http://%s%s" % (
201 201 unicode(current_site),
202 202 reverse("notification_notices"),
203 203 )
204   -
  204 +
205 205 current_language = get_language()
206 206
207 207 formats = (
@@ -242,7 +242,7 @@ def send(recipient, label, extra_context={}, issue_notice=True):
242 242 subject = ''.join(render_to_string('notification/email_subject.txt', {
243 243 'message': messages['short'],
244 244 }, context).splitlines())
245   -
  245 +
246 246 body = render_to_string('notification/email_body.txt', {
247 247 'message': messages['plain'],
248 248 }, context)
@@ -266,7 +266,7 @@ def all_for(self, observed, signal):
266 266 content_type = ContentType.objects.get_for_model(observed)
267 267 observed_items = self.filter(content_type=content_type, object_id=observed.id, signal=signal)
268 268 return observed_items
269   -
  269 +
270 270 def get_for(self, observed, observer, signal):
271 271 content_type = ContentType.objects.get_for_model(observed)
272 272 observed_item = self.get(content_type=content_type, object_id=observed.id, user=observer, signal=signal)
@@ -276,39 +276,39 @@ def get_for(self, observed, observer, signal):
276 276 class ObservedItem(models.Model):
277 277
278 278 user = models.ForeignKey(User, verbose_name=_('user'))
279   -
  279 +
280 280 content_type = models.ForeignKey(ContentType)
281 281 object_id = models.PositiveIntegerField()
282 282 observed_object = generic.GenericForeignKey('content_type', 'object_id')
283   -
  283 +
284 284 notice_type = models.ForeignKey(NoticeType, verbose_name=_('notice type'))
285   - message_template = models.TextField(verbose_name=_('message template'))
286   -
  285 +
287 286 added = models.DateTimeField(_('added'), default=datetime.datetime.now)
288   -
  287 +
289 288 # the signal that will be listened to send the notice
290 289 signal = models.TextField(verbose_name=_('signal'))
291   -
  290 +
292 291 objects = ObservedItemManager()
293   -
  292 +
294 293 class Meta:
295 294 ordering = ['-added']
296 295 verbose_name = _('observed item')
297 296 verbose_name_plural = _('observed items')
298   -
  297 +
299 298 def send_notice(self):
300   - send([self.user], self.notice_type.label, self.message_template,
301   - [self.observed_object])
  299 + send([self.user], self.notice_type.label,
  300 + {'observed': self.observed_object})
302 301
303 302
304   -def observe(observed, observer, notice_type_label, message_template, signal='post_save'):
  303 +def observe(observed, observer, notice_type_label, signal='post_save'):
305 304 """
306 305 Create a new ObservedItem.
307   -
  306 +
308 307 To be used by applications to register a user as an observer for some object.
309 308 """
310 309 notice_type = NoticeType.objects.get(label=notice_type_label)
311   - observed_item = ObservedItem(user=observer, observed_object=observed, notice_type=notice_type, message_template=message_template, signal=signal)
  310 + observed_item = ObservedItem(user=observer, observed_object=observed,
  311 + notice_type=notice_type, signal=signal)
312 312 observed_item.save()
313 313 return observed_item
314 314

0 comments on commit 02250bd

Please sign in to comment.
Something went wrong with that request. Please try again.