Exemplo n.º 1
0
def get_votes(instance):
    def _getvalue():
        if isinstance(instance, Thread):
            filters = {"email__thread_id": instance.id}
        elif isinstance(instance, Email):
            filters = {"email_id": instance.id}
        else:
            ValueError("The 'get_votes' function only accepts 'Email' "
                       "and 'Thread' instance")
        votes = list(Vote.objects.filter(**filters).values_list(
            "value", flat=True))
        return (
                len([ v for v in votes if v == 1 ]),
                len([ v for v in votes if v == -1 ]),
            )
    cache_key = "%s:%s:votes" % (instance.__class__.__name__, instance.id)
    votes = cache.get_or_set(cache_key, _getvalue, None)
    likes, dislikes = votes
    # XXX: use an Enum?
    if likes - dislikes >= 10:
        status = "likealot"
    elif likes - dislikes > 0:
        status = "like"
    else:
        status = "neutral"
    return {"likes": likes, "dislikes": dislikes, "status": status}
Exemplo n.º 2
0
 def recent_participants_count(self):
     begin_date, end_date = self.get_recent_dates()
     return cache.get_or_set(
         "MailingList:%s:recent_participants_count" % self.name,
         lambda: self.get_participants_count_between(begin_date, end_date),
         3600 * 6,
     )  # 6 hours
Exemplo n.º 3
0
def get_months(mlist):
    """ Return a dictionnary of years, months for which there are
    potentially archives available for a given list (based on the
    oldest post on the list).

    :arg list_name, name of the mailing list in which this email
    should be searched.
    """
    date_first = cache.get_or_set(
        "MailingList:%s:first_date" % mlist.name,
        lambda: mlist.emails.order_by("date"
            ).values_list("date", flat=True).first(),
        None)
    if not date_first:
        return {}
    archives = {}
    now = datetime.datetime.now()
    year = date_first.year
    month = date_first.month
    while year < now.year:
        archives[year] = range(1, 13)[(month -1):]
        year = year + 1
        month = 1
    archives[now.year] = range(1, 13)[:now.month]
    return archives
Exemplo n.º 4
0
    def get_participants_count_for_month(self, year, month):
        def _get_value():
            begin_date = datetime.datetime(year, month, 1, tzinfo=utc)
            end_date = begin_date + datetime.timedelta(days=32)
            end_date = end_date.replace(day=1)
            return self.get_participants_count_between(begin_date, end_date)

        return cache.get_or_set("MailingList:%s:p_count_for:%s:%s" % (self.name, year, month), _get_value, None)
Exemplo n.º 5
0
 def get_participants_count_for_month(self, year, month):
     def _get_value():
         begin_date = datetime.datetime(year, month, 1, tzinfo=utc)
         end_date = begin_date + datetime.timedelta(days=32)
         end_date = end_date.replace(day=1)
         return self.get_participants_count_between(begin_date, end_date)
     return cache.get_or_set(
         "MailingList:%s:p_count_for:%s:%s" % (self.name, year, month),
         _get_value, None)
Exemplo n.º 6
0
 def top_posters(self):
     begin_date, end_date = self.get_recent_dates()
     query = (
         Sender.objects.filter(emails__mailinglist=self, emails__date__gte=begin_date, emails__date__lt=end_date)
         .annotate(count=models.Count("emails"))
         .order_by("-count")
     )
     # Because of South, ResultSets are not pickleizable directly, they must
     # be converted to lists (there's an extra field without the _deferred
     # attribute that causes tracebacks)
     return cache.get_or_set("MailingList:%s:top_posters" % self.name, lambda: list(query[:5]), 3600 * 6)  # 6 hours
Exemplo n.º 7
0
    def get_subscriptions(self):
        def _get_value():
            mm_user = self.get_mailman_user()
            if mm_user is None:
                return {}
            subscriptions = dict([(member.list_id, member.address) for member in mm_user.subscriptions])
            return subscriptions

        # TODO: how should this be invalidated? Subscribe to a signal in
        # mailman when a new subscription occurs? Or store in the session?
        return cache.get_or_set("User:%s:subscriptions" % self.id, _get_value, 60, version=2)  # 1 minute
Exemplo n.º 8
0
 def top_posters(self):
     begin_date, end_date = self.get_recent_dates()
     query = Sender.objects.filter(
         emails__mailinglist=self,
         emails__date__gte=begin_date,
         emails__date__lt=end_date,
         ).annotate(count=models.Count("emails")
         ).order_by("-count")
     # Because of South, ResultSets are not pickleizable directly, they must
     # be converted to lists (there's an extra field without the _deferred
     # attribute that causes tracebacks)
     return cache.get_or_set(
         "MailingList:%s:top_posters" % self.name,
         lambda: list(query[:5]),
         3600 * 6) # 6 hours
Exemplo n.º 9
0
 def get_subscriptions(self):
     def _get_value():
         mm_user = self.get_mailman_user()
         if mm_user is None:
             return []
         mm_client = get_mailman_client()
         subscriptions = {}
         for member in mm_user.subscriptions:
             mlist_name = mm_client.get_list(member.list_id).fqdn_listname
             ## de-duplicate subscriptions
             #if mlist_name in [ s["list_name"] for s in sub_names ]:
             #    continue
             subscriptions[mlist_name] = member.address
         return subscriptions
     # TODO: how should this be invalidated? Subscribe to a signal in
     # mailman when a new subscription occurs? Or store in the session?
     return cache.get_or_set(
         "User:%s:subscriptions" % self.id,
         _get_value, 60 * 10) # 10 minutes
Exemplo n.º 10
0
 def subject(self):
     return cache.get_or_set(
         "Thread:%s:subject" % self.id,
         lambda: self.starting_email.subject,
         None)
Exemplo n.º 11
0
 def emails_count(self):
     return cache.get_or_set(
         "Thread:%s:emails_count" % self.id,
         lambda: self.emails.count(),
         None)
Exemplo n.º 12
0
 def participants_count(self):
     return cache.get_or_set(
         "Thread:%s:participants_count" % self.id,
         lambda: self.participants.count(),
         None)
Exemplo n.º 13
0
 def recent_participants_count(self):
     begin_date, end_date = self.get_recent_dates()
     return cache.get_or_set(
         "MailingList:%s:recent_participants_count" % self.name,
         lambda: self.get_participants_count_between(begin_date, end_date),
         3600 * 6) # 6 hours
Exemplo n.º 14
0
 def recent_threads(self):
     begin_date, end_date = self.get_recent_dates()
     return cache.get_or_set(
         "MailingList:%s:recent_threads" % self.name,
         lambda: self.get_threads_between(begin_date, end_date),
         3600 * 6) # 6 hours