Commit 08a6dc8cc1748f0d84f6061d6e89ae08b589e0e3
- Diff rendering mode:
- inline
- side by side
pm/forum/models.py
(19 / 7)
|   | |||
| 31 | 31 | creation = models.DateTimeField(auto_now_add=True) | |
| 32 | 32 | is_postit = models.BooleanField() | |
| 33 | 33 | is_closed = models.BooleanField() | |
| 34 | |||
| 34 | |||
| 35 | def first_post(self): | ||
| 36 | return Post.objects.filter(topic=self)[0] | ||
| 35 | 37 | def last_read_post(self): | |
| 36 | 38 | read = TopicRead.objects.select_related( | |
| 37 | 39 | ).filter(topic=self, user=get_current_user() | |
| 38 | ).latest('post__creation') | ||
| 39 | return read.post | ||
| 40 | ) | ||
| 41 | if len(read) == 0: | ||
| 42 | return self.first_post() | ||
| 43 | else: | ||
| 44 | return read[0].post | ||
| 40 | 45 | ||
| 41 | 46 | @models.permalink | |
| 42 | 47 | def get_absolute_url(self): | |
| … | … | ||
| 99 | 99 | return Post.objects.filter(topic=id).order_by('creation')[0] | |
| 100 | 100 | ||
| 101 | 101 | def mark_read(topic): | |
| 102 | t = TopicRead(post=topic.last_post, topic=topic, user=get_current_user()) | ||
| 103 | t.save() | ||
| 102 | TopicRead.objects.filter(topic=topic,user=get_current_user()).delete() | ||
| 103 | t = TopicRead(post=topic.last_post, topic=topic, user=get_current_user()) | ||
| 104 | t.save() | ||
| 104 | 105 | ||
| 105 | def list_last_topics(nb,private): | ||
| 106 | def list_last_topics(nb,private,auth): | ||
| 106 | 107 | topics = Topic.objects.annotate(models.Count('post')) | |
| 107 | 108 | if not private: | |
| 108 | 109 | topics = topics.filter(forum__staff_only = False) | |
| 109 | 110 | ||
| 110 | return topics.order_by('-last_post__creation')[:nb] | ||
| 111 | topics = list(topics.order_by('-last_post__creation')[:nb]) | ||
| 112 | if auth: | ||
| 113 | for topic in topics: | ||
| 114 | p = topic.last_read_post().creation | ||
| 115 | topic.post__count -= Post.objects.filter(topic = topic, creation__lt=p).count() + 1 | ||
| 116 | |||
| 117 | return topics | ||
| 111 | 118 | ||
| 112 | 119 | def set_last_post(forum_id): | |
| 113 | 120 | forum = Forum.objects.get(pk=forum_id) |
pm/pages/home.py
(1 / 1)
|   | |||
| 14 | 14 | big_tuts = tuts[0:3] | |
| 15 | 15 | small_tuts = tuts[3:] | |
| 16 | 16 | ||
| 17 | last_topics = list_last_topics(6, request.user.is_staff) | ||
| 17 | last_topics = list_last_topics(6, request.user.is_staff, request.user.is_authenticated()) | ||
| 18 | 18 | ||
| 19 | 19 | return render_to_response('home.html', {'big_news':big_news, 'small_news':small_news, | |
| 20 | 20 | 'big_tuts':big_tuts, 'small_tuts':small_tuts, |

