def email_watchers(self, notified_users): emailed = self.thread.email_watchers(self.request, self.type_prefix, self.post) for user in emailed: if not user in notified_users: if user.pk == self.thread.start_poster_id: alert = user.alert(ugettext_lazy("%(username)s has replied to your thread %(thread)s").message) else: alert = user.alert(ugettext_lazy("%(username)s has replied to thread %(thread)s that you are watching").message) alert.profile('username', self.request.user) alert.post('thread', self.type_prefix, self.thread, self.post) alert.save_all()
def notify_users(self): try: post_content = self.md except AttributeError: post_content = False notified_users = [] if post_content: try: if (self.quote and self.quote.user_id and self.quote.user.username_slug in post_content.mentions): del post_content.mentions[self.quote.user.username_slug] if not self.quote.user in self.post.mentions.all(): notified_users.append(self.quote.user) self.post.mentions.add(self.quote.user) alert = self.quote.user.alert( ugettext_lazy( "%(username)s has replied to your post in thread %(thread)s" ).message) alert.profile('username', self.request.user) alert.post('thread', self.type_prefix, self.thread, self.post) alert.save_all() except KeyError: pass if post_content.mentions: notified_users += [ x for x in post_content.mentions.itervalues() ] self.post.notify_mentioned(self.request, self.type_prefix, post_content.mentions) self.post.save(force_update=True) self.email_watchers(notified_users)
def notify_users(self): try: post_mentions = self.md.mentions except AttributeError: post_mentions = False notified_users = [] if post_mentions: try: if (self.quote and self.quote.user_id and self.quote.user.username_slug in post_mentions): del post_mentions[self.quote.user.username_slug] if not self.quote.user in self.post.mentions.all(): notified_users.append(self.quote.user) self.post.mentions.add(self.quote.user) alert = self.quote.user.alert(ugettext_lazy("%(username)s has replied to your post in thread %(thread)s").message) alert.profile('username', self.request.user) alert.post('thread', self.type_prefix, self.thread, self.post) alert.save_all() except KeyError: pass if post_mentions: notified_users += [x for x in post_mentions.values()] self.post.notify_mentioned(self.request, self.type_prefix, post_mentions) self.post.save(force_update=True) self.email_watchers(notified_users)
def username(request): if not request.acl.usercp.show_username_change(): return error404(request) changes_left = request.acl.usercp.changes_left(request.user) next_change = None if request.acl.usercp.changes_expire() and not changes_left: next_change = request.user.namechanges.filter( date__gte=timezone.now() - timedelta(days=request.acl.usercp.acl['changes_expire']), ).order_by('-date')[0] next_change = next_change.date + timedelta(days=request.acl.usercp.acl['changes_expire']) message = request.messages.get_message('usercp_username') if request.method == 'POST': if not changes_left: message = Message(_("You have exceeded the maximum number of name changes."), 'error') form = UsernameChangeForm(request=request) else: org_username = request.user.username form = UsernameChangeForm(request.POST, request=request) if form.is_valid(): request.user.set_username(form.cleaned_data['username']) request.user.save(force_update=True) request.user.sync_username() request.user.namechanges.create(date=timezone.now(), old_username=org_username) request.messages.set_flash(Message(_("Your username has been changed.")), 'success', 'usercp_username') # Alert followers of namechange alert_time = timezone.now() bulk_alerts = [] alerted_users = [] for follower in request.user.follows_set.iterator(): alerted_users.append(follower.pk) alert = Alert(user=follower, message=ugettext_lazy("User that you are following, %(username)s, has changed his name to %(newname)s").message, date=alert_time) alert.strong('username', org_username) alert.profile('newname', request.user) alert.hydrate() bulk_alerts.append(alert) if bulk_alerts: Alert.objects.bulk_create(bulk_alerts) User.objects.filter(id__in=alerted_users).update(alerts=F('alerts') + 1) # Hop back return redirect(reverse('usercp_username')) message = Message(form.non_field_errors()[0], 'error') else: form = UsernameChangeForm(request=request) return request.theme.render_to_response('usercp/username.html', context_instance=RequestContext(request, { 'message': message, 'changes_left': changes_left, 'form': FormLayout(form), 'next_change': next_change, 'changes_history': request.user.namechanges.order_by('-date')[:10], 'tab': 'username', }));
def username(request): if not request.acl.usercp.show_username_change(): return error404(request) changes_left = request.acl.usercp.changes_left(request.user) next_change = None if request.acl.usercp.changes_expire() and not changes_left: next_change = request.user.namechanges.filter( date__gte=timezone.now() - timedelta(days=request.acl.usercp.acl['changes_expire']), ).order_by('-date')[0] next_change = next_change.date + timedelta(days=request.acl.usercp.acl['changes_expire']) message = request.messages.get_message('usercp_username') if request.method == 'POST': if not changes_left: message = Message(_("You have exceeded the maximum number of name changes."), messages.ERROR) form = UsernameChangeForm(request=request) else: org_username = request.user.username form = UsernameChangeForm(request.POST, request=request) if form.is_valid(): request.user.set_username(form.cleaned_data['username']) request.user.save(force_update=True) request.user.sync_username() request.user.namechanges.create(date=timezone.now(), old_username=org_username) messages.success(request, _("Your username has been changed."), 'usercp_username') # Alert followers of namechange alert_time = timezone.now() bulk_alerts = [] alerted_users = [] for follower in request.user.follows_set.iterator(): alerted_users.append(follower.pk) alert = Alert(user=follower, message=ugettext_lazy("User that you are following, %(username)s, has changed his name to %(newname)s").message, date=alert_time) alert.strong('username', org_username) alert.profile('newname', request.user) alert.hydrate() bulk_alerts.append(alert) if bulk_alerts: Alert.objects.bulk_create(bulk_alerts) User.objects.filter(id__in=alerted_users).update(alerts=F('alerts') + 1) # Hop back return redirect(reverse('usercp_username')) message = Message(form.non_field_errors()[0], messages.ERROR) else: form = UsernameChangeForm(request=request) return render_to_response('usercp/username.html', context_instance=RequestContext(request, { 'message': message, 'changes_left': changes_left, 'form': form, 'next_change': next_change, 'changes_history': request.user.namechanges.order_by('-date')[:10], 'tab': 'username'}));
def invite_users(self, users): for user in users: if not user in self.thread.participants.all(): self.thread.participants.add(user) user.email_user(self.request, 'private_thread_invite', _("You've been invited to private thread \"%(thread)s\" by %(user)s") % {'thread': self.thread.name, 'user': self.request.user.username}, {'author': self.request.user, 'thread': self.thread}) alert = user.alert(ugettext_lazy("%(username)s has invited you to the %(thread)s private thread").message) alert.profile('username', self.request.user) alert.post('thread', self.type_prefix, self.thread, self.post) alert.save_all() self.post.mentions.add(user) if self.action == 'new_reply': self.thread.set_checkpoint(self.request, 'invited', user)
def follow(request, user): if request.user.pk == user.pk: return error404(request) if not request.user.is_following(user): request.messages.set_flash(Message(_("You are now following %(username)s") % {'username': user.username}), 'success') request.user.follows.add(user) request.user.following += 1 request.user.save(force_update=True) user.followers += 1 if not user.is_ignoring(request.user): alert = user.alert(ugettext_lazy("%(username)s is now following you").message) alert.profile('username', request.user) alert.save_all() else: user.save(force_update=True) return fallback(request)
def follow(request, user): if request.user.pk == user.pk: return error404(request) if not request.user.is_following(user): messages.success(request, _("You are now following %(username)s") % {'username': user.username}) request.user.follows.add(user) request.user.following += 1 request.user.save(force_update=True) user.followers += 1 if not user.is_ignoring(request.user): alert = user.alert(ugettext_lazy("%(username)s is now following you").message) alert.profile('username', request.user) alert.save_all() else: user.save(force_update=True) return fallback(request)
def notify_mentioned(self, request, thread_type, users): from misago.acl.exceptions import ACLError403, ACLError404 mentioned = self.mentions.all() for slug, user in users.items(): if user.pk != request.user.pk and user not in mentioned: self.mentions.add(user) try: user_acl = user.acl() user_acl.forums.allow_forum_view(self.forum) user_acl.threads.allow_thread_view(user, self.thread) user_acl.threads.allow_post_view(user, self.thread, self) if not user.is_ignoring(request.user): alert = user.alert(ugettext_lazy("%(username)s has mentioned you in his reply in thread %(thread)s").message) alert.profile('username', request.user) alert.post('thread', thread_type, self.thread, self) alert.save_all() except (ACLError403, ACLError404): pass
def notify_mentioned(self, request, thread_type, users): from misago.acl.exceptions import ACLError403, ACLError404 mentioned = self.mentions.all() for slug, user in users.items(): if user.pk != request.user.pk and user not in mentioned: self.mentions.add(user) try: user_acl = user.acl() user_acl.forums.allow_forum_view(self.forum) user_acl.threads.allow_thread_view(user, self.thread) user_acl.threads.allow_post_view(user, self.thread, self) if not user.is_ignoring(request.user): alert = user.alert( ugettext_lazy( "%(username)s has mentioned you in his reply in thread %(thread)s" ).message) alert.profile('username', request.user) alert.post('thread', thread_type, self.thread, self) alert.save_all() except (ACLError403, ACLError404): pass
def invite_users(self, users): for user in users: if not user in self.thread.participants.all(): self.thread.participants.add(user) user.email_user( self.request, 'private_thread_invite', _("You've been invited to private thread \"%(thread)s\" by %(user)s" ) % { 'thread': self.thread.name, 'user': self.request.user.username }, { 'author': self.request.user, 'thread': self.thread }) alert = user.alert( ugettext_lazy( "%(username)s has invited you to the %(thread)s private thread" ).message) alert.profile('username', self.request.user) alert.post('thread', self.type_prefix, self.thread, self.post) alert.save_all() self.post.mentions.add(user) if self.action == 'new_reply': self.thread.set_checkpoint(self.request, 'invited', user)
def you_have_been_warned(giver, receiver, warning): alert = receiver.alert( ugettext_lazy( "%(username)s has increased your warning level.").message) alert.profile('username', giver) alert.save_all()
def your_warn_has_been_canceled(canceler, receiver): alert = receiver.alert(ugettext_lazy("%(username)s has lowered your warning level.").message) alert.profile('username', canceler) alert.save_all()
def you_have_been_warned(giver, receiver, warning): alert = receiver.alert(ugettext_lazy("%(username)s has increased your warning level.").message) alert.profile('username', giver) alert.save_all()
def your_warn_has_been_canceled(canceler, receiver): alert = receiver.alert( ugettext_lazy("%(username)s has lowered your warning level.").message) alert.profile('username', canceler) alert.save_all()
def post_form(self, form): now = timezone.now() moderation = (not self.request.acl.threads.acl[self.forum.pk]['can_approve'] and self.request.acl.threads.acl[self.forum.pk]['can_start_threads'] == 1) self.thread.previous_last = self.thread.last_post self.md, post_preparsed = post_markdown(self.request, form.cleaned_data['post']) # Count merge diff and see if we are merging merge_diff = (now - self.thread.last) merge_diff = (merge_diff.days * 86400) + merge_diff.seconds if (self.request.settings.post_merge_time and merge_diff < (self.request.settings.post_merge_time * 60) and self.thread.last_poster_id == self.request.user.id and self.thread.last_post.moderated == moderation): merged = True self.post = self.thread.last_post self.post.date = now self.post.post = '%s\n\n- - -\n**%s**\n%s' % (self.post.post, _("Added on %(date)s:") % {'date': date(now, 'SHORT_DATETIME_FORMAT')}, form.cleaned_data['post']) self.md, self.post.post_preparsed = post_markdown(self.request, self.post.post) self.post.save(force_update=True) else: # Create new post merged = False self.post = Post.objects.create( forum=self.forum, thread=self.thread, user=self.request.user, user_name=self.request.user.username, ip=self.request.session.get_ip(self.request), agent=self.request.META.get('HTTP_USER_AGENT'), post=form.cleaned_data['post'], post_preparsed=post_preparsed, date=now, merge=self.thread.merges, moderated=moderation, ) # Update thread data and score? if not moderation: self.thread.new_last_post(self.post) if not merged: if not moderation: self.thread.replies += 1 else: self.thread.replies_moderated += 1 # Increase thread score if self.thread.last_poster_id != self.request.user.pk: self.thread.score += self.request.settings['thread_ranking_reply_score'] # Save updated thread self.thread.save(force_update=True) # Update forum and monitor if not moderation and not merged: self.request.monitor['posts'] = int(self.request.monitor['posts']) + 1 self.forum.posts += 1 self.forum.new_last_thread(self.thread) self.forum.save(force_update=True) # Reward user for posting new reply? if not moderation and not merged and (not self.request.user.last_post or self.request.user.last_post < timezone.now() - timedelta(seconds=self.request.settings['score_reward_new_post_cooldown'])): self.request.user.score += self.request.settings['score_reward_new_post'] # Update user if not moderation and not merged: self.request.user.threads += 1 self.request.user.posts += 1 self.request.user.last_post = now self.request.user.save(force_update=True) # Set thread weight if 'thread_weight' in form.cleaned_data: self.thread.weight = form.cleaned_data['thread_weight'] # Set "closed" checkpoint, either due to thread limit or posters wish if (self.request.settings.thread_length > 0 and not merged and not moderation and not self.thread.closed and self.thread.replies >= self.request.settings.thread_length): self.thread.closed = True self.post.set_checkpoint(self.request, 'limit') self.post.save(force_update=True) self.thread.save(force_update=True) elif 'close_thread' in form.cleaned_data and form.cleaned_data['close_thread']: self.thread.closed = not self.thread.closed if merged: checkpoint_post = self.post else: checkpoint_post = self.thread.previous_last if self.thread.closed: checkpoint_post.set_checkpoint(self.request, 'closed') else: checkpoint_post.set_checkpoint(self.request, 'opened') checkpoint_post.save(force_update=True) self.thread.save(force_update=True) # Notify user we quoted? if (self.quote and self.quote.user_id and not merged and self.quote.user_id != self.request.user.pk and not self.quote.user.is_ignoring(self.request.user)): alert = self.quote.user.alert(ugettext_lazy("%(username)s has replied to your post in thread %(thread)s").message) alert.profile('username', self.request.user) alert.post('thread', self.type_prefix, self.thread, self.post) alert.save_all() # E-mail users about new response self.thread.email_watchers(self.request, self.type_prefix, self.post)