def post(self, *args, **kwargs): obj = self.get_object() if "important-toggle" in self.request.POST: with search.skip_index_update(): obj.important = not bool(obj.important) obj.save(update_fields=["important"]) return HttpResponseRedirect(self.get_success_url())
def post(self, *args, **kwargs): obj = self.get_object() if "important-toggle" in self.request.POST: with search.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): kwargs["headline"] = u"{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.new: with search.skip_index_update(): self.inbox_obj.new = False self.inbox_obj.save(update_fields=["new"]) return context
def get(self, *args, **kwargs): with search.skip_index_update(): out = super(EmailView, self).get(*args, **kwargs) if "all-headers" in self.request.GET: self.object.view_all_headers = bool(int(self.request.GET["all-headers"])) self.object.read = True self.object.seen = True self.object.save(update_fields=["view_all_headers", "read", "seen"]) # pretend to be @csp_replace out._csp_replace = {"style-src": ["'self'", "'unsafe-inline'"]} if getattr(self, "_has_images", False): # if we have images to display, allow loading over https out._csp_replace["img-src"] = ["'self'", "https:"] return out
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_search.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 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_search.skip_index_update(): # update seen flags models.Email.objects.filter(id__in=email_id_list).update(seen=True) 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 inbox_new_flag(user_id, inbox_id=None): emails = models.Email.objects.order_by("-received_date") 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 = list(emails.values_list("id", flat=True)[:100]) # number of emails on page emails = models.Email.objects.filter(id__in=emails, flags=~models.Email.flags.seen) if emails.count() > 0: # if some emails haven't been seen yet, we have nothing else to do return elif inbox_id is None: profile = models.UserProfile.objects.get_or_create(user_id=user_id)[0] profile.flags.unified_has_new_messages = False profile.save(update_fields=["flags"]) else: with watson_search.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 process_message(message, inbox=None, domain=None): try: inbox = Inbox.objects.filter(inbox=inbox, domain__domain=domain) inbox = inbox.select_related("user", "user__inboxenprofile").receiving() inbox = inbox.get() make_email(message, inbox) with search.skip_index_update(): inbox.new = True inbox.save(update_fields=["new"]) if not inbox.exclude_from_unified: profile = inbox.user.inboxenprofile profile.unified_has_new_messages = True profile.save(update_fields=["unified_has_new_messages"]) except DatabaseError as e: log.exception("DB error: %s", e) raise SMTPError(451, "Error processing message, try again later.") except Inbox.DoesNotExist: raise SMTPError(550, "No such address")
def process_message(message, inbox=None, domain=None): try: inbox = Inbox.objects.filter(inbox=inbox, domain__domain=domain) inbox = inbox.select_related("user", "user__inboxenprofile").receiving() inbox = inbox.get() make_email(message, inbox) with search.skip_index_update(): inbox.flags.new = True inbox.save(update_fields=["flags"]) if not inbox.flags.exclude_from_unified: profile = inbox.user.inboxenprofile profile.flags.unified_has_new_messages = True profile.save(update_fields=["flags"]) except DatabaseError as e: log.exception("DB error: %s", e) raise SMTPError(451, "Error processing message, try again later.") except Inbox.DoesNotExist: raise SMTPError(550, "No such address")
def inbox_new_flag(user_id, inbox_id=None): emails = models.Email.objects.order_by("-received_date") emails = emails.filter(inbox__user__id=user_id, inbox__exclude_from_unified=False) if inbox_id is not None: emails = emails.filter(inbox__id=inbox_id) emails = list(emails.values_list( "id", flat=True)[:100]) # number of emails on page emails = models.Email.objects.filter(id__in=emails, seen=False) if emails.count() > 0: # if some emails haven't been seen yet, we have nothing else to do return elif inbox_id is None: profile = models.UserProfile.objects.get_or_create(user_id=user_id)[0] profile.unified_has_new_messages = False profile.save(update_fields=["unified_has_new_messages"]) else: with watson_search.skip_index_update(): inbox = models.Inbox.objects.get(user__id=user_id, id=inbox_id) inbox.new = False inbox.save(update_fields=["new"])
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)