def post(self, *args, **kwargs): qs = self.get_queryset() # this is kinda bad, but nested forms aren't supported in all browsers if "delete-single" in self.request.POST: email_id = int(self.request.POST["delete-single"], 16) email = qs.get(id=email_id) email.delete() return HttpResponseRedirect(self.get_success_url()) elif "important-single" in self.request.POST: with watson.skip_index_update(): email_id = int(self.request.POST["important-single"], 16) email = qs.get(id=email_id) email.flags.important = not email.flags.important email.save(update_fields=["flags"]) return HttpResponseRedirect(self.get_success_url()) emails = [] for email in self.request.POST: if self.request.POST[email] == "email": try: email_id = int(email, 16) emails.append(email_id) except ValueError: return HttpResponseRedirect(self.get_success_url()) if len(emails) == 0: # nothing was selected, return early return HttpResponseRedirect(self.get_success_url()) # Something between Bitfield and Django's ORM doesn't like out complex queries emails = qs.filter(id__in=emails).order_by("id").only("id") email_ids = list(emails.values_list('id', flat=True)) emails = self.model.objects.filter(id__in=email_ids).only("id") with watson.skip_index_update(): if "unimportant" in self.request.POST: emails.update( flags=F('flags').bitand(~self.model.flags.important)) elif "important" in self.request.POST: emails.update( flags=F('flags').bitor(self.model.flags.important)) elif "delete" in self.request.POST: emails.update(flags=F('flags').bitor(self.model.flags.deleted)) for email in emails: delete_email.delay(email.id) return HttpResponseRedirect(self.get_success_url())
def inbox_new_flag(user_id, inbox_id=None): emails = models.Email.objects.order_by("-received_date").only('id') emails = emails.filter( inbox__user__id=user_id, inbox__flags=~models.Inbox.flags.exclude_from_unified) if inbox_id is not None: emails = emails.filter(inbox__id=inbox_id) emails = [email.id for email in emails[:100]] # number of emails on page emails = models.Email.objects.filter(id__in=emails, flags=~models.Email.flags.seen) # if some emails haven't been seen yet, we have nothing else to do if emails.count() > 0: return if inbox_id is None: profile = get_user_model().objects.select_related("userprofile").get( id=user_id).userprofile profile.flags.unified_has_new_messages = False profile.save(update_fields=["flags"]) else: with watson.skip_index_update(): inbox = models.Inbox.objects.get(user__id=user_id, id=inbox_id) inbox.flags.new = False inbox.save(update_fields=["flags"])
def get(self, *args, **kwargs): with watson.skip_index_update(): out = super(EmailView, self).get(*args, **kwargs) self.object.flags.read = True self.object.flags.seen = True self.object.save(update_fields=["flags"]) return out
def post(self, *args, **kwargs): qs = self.get_queryset() # this is kinda bad, but nested forms aren't supported in all browsers if "delete-single" in self.request.POST: email_id = int(self.request.POST["delete-single"], 16) email = qs.get(id=email_id) email.delete() return HttpResponseRedirect(self.get_success_url()) elif "important-single" in self.request.POST: with watson.skip_index_update(): email_id = int(self.request.POST["important-single"], 16) email = qs.get(id=email_id) email.flags.important = not email.flags.important email.save(update_fields=["flags"]) return HttpResponseRedirect(self.get_success_url()) emails = [] for email in self.request.POST: if self.request.POST[email] == "email": try: email_id = int(email, 16) emails.append(email_id) except ValueError: return HttpResponseRedirect(self.get_success_url()) if len(emails) == 0: # nothing was selected, return early return HttpResponseRedirect(self.get_success_url()) # Something between Bitfield and Django's ORM doesn't like out complex queries emails = qs.filter(id__in=emails).order_by("id").only("id") email_ids = list(emails.values_list('id', flat=True)) emails = self.model.objects.filter(id__in=email_ids).only("id") with watson.skip_index_update(): if "unimportant" in self.request.POST: emails.update(flags=F('flags').bitand(~self.model.flags.important)) elif "important" in self.request.POST: emails.update(flags=F('flags').bitor(self.model.flags.important)) elif "delete" in self.request.POST: emails.update(flags=F('flags').bitor(self.model.flags.deleted)) for email in emails: delete_email.delay(email.id) return HttpResponseRedirect(self.get_success_url())
def post(self, *args, **kwargs): obj = self.get_object() if "important-toggle" in self.request.POST: with watson.skip_index_update(): obj.flags.important = not bool(obj.flags.important) obj.save(update_fields=["flags"]) return HttpResponseRedirect(self.get_success_url())
def testNestedUpdateInSkipContext(self): with watson.skip_index_update(): self.test21.title = "baar" self.test21.save() with watson.update_index(): self.test11.title = "fooo" self.test11.save() # We should get "fooo", but not "baar" self.assertEqual(watson.search("fooo").count(), 1) self.assertEqual(watson.search("baar").count(), 0)
def get_context_data(self, *args, **kwargs): self.headline = "{0}@{1}".format(self.kwargs["inbox"], self.kwargs["domain"]) context = super(SingleInboxView, self).get_context_data(*args, **kwargs) context.update({"inbox":self.kwargs["inbox"], "domain":self.kwargs["domain"]}) if self.inbox_obj.flags.new: with watson.skip_index_update(): self.inbox_obj.flags.new = False self.inbox_obj.save(update_fields=["flags"]) return context
def get_context_data(self, *args, **kwargs): self.headline = "{0}@{1}".format(self.kwargs["inbox"], self.kwargs["domain"]) context = super(SingleInboxView, self).get_context_data(*args, **kwargs) context.update({ "inbox": self.kwargs["inbox"], "domain": self.kwargs["domain"] }) if self.inbox_obj.flags.new: with watson.skip_index_update(): self.inbox_obj.flags.new = False self.inbox_obj.save(update_fields=["flags"]) return context
def deal_with_flags(email_id_list, user_id, inbox_id=None): """Set seen flags on a list of email IDs and then send off tasks to update "new" flags on affected Inbox objects """ with transaction.atomic(): with watson.skip_index_update(): # update seen flags models.Email.objects.filter(id__in=email_id_list).update( flags=F('flags').bitor(models.Email.flags.seen)) if inbox_id is None: # grab affected inboxes inbox_list = models.Inbox.objects.filter(user__id=user_id, email__id__in=email_id_list) inbox_list = inbox_list.distinct() for inbox in inbox_list: inbox_new_flag.delay(user_id, inbox.id) else: # we only need to update inbox_new_flag.delay(user_id)
def START(message, inbox=None, domain=None): try: inbox = Inbox.objects.select_related("user", "user__userprofile").get(inbox=inbox, domain__domain=domain) if inbox.flags.deleted: raise SMTPError(550, "No such address") make_email(message, inbox) with watson.skip_index_update(): inbox.flags.new = True inbox.save(update_fields=["flags"]) if not inbox.flags.exclude_from_unified: profile = inbox.user.userprofile profile.flags.unified_has_new_messages = True profile.save(update_fields=["flags"]) except DatabaseError, e: log.debug("DB error: %s", e) raise SMTPError(451, "Error processing message, try again later.")
def testSkipSearchIndexUpdate(self): with watson.skip_index_update(): self.test11.title = "fooo" self.test11.save() # Test a search that should get not model. self.assertEqual(watson.search("fooo").count(), 0)