Пример #1
0
    def index(self):
        """
        fan_list = memcache.get("500fans")
        # TODO we are relying on index.py resetting memcache on day change here
        if fan_list:
            return fan_list
        """
        
        fans = db.Query(storage.ShopFans).filter("shop =", self.shop) \
                                 .order("-favored_on").fetch(limit=500)
        
        fan_counter = storage.Counters.get_by_key_name("%d:fans" % self.shop.id)
        if not fan_counter or not fans or fans[0].favored_on > fan_counter.timestamp:
            fan_counter = self._count_fans(fan_counter)        
        
        fan_count = fan_counter.count
    
        dates = []
        for group in itertools.groupby(fans, lambda entry: appetsy.to_local_time(self.shop, entry.favored_on).date()):
            fans = list(group[1])
            fans.reverse()
            dates.append ({"cur_date": group[0],
                           "fans": fans 
                           })

        #filter out expired and sold listings
    
        data = {
            "dates": dates,
            "now": appetsy.time(self.shop),
            "fan_count": fan_count,
            "today": appetsy.today(self.shop),
            "yesterday": appetsy.today(self.shop) - dt.timedelta(days=1),            
        }
        
        fan_list = appetsy.get_template("etsy.html").render(**data)
        memcache.set("500fans", fan_list)
        return fan_list
Пример #2
0
    def recent_views(self):
        recent_views_json = memcache.get("recent_views_json", namespace = str(self.shop.id))
        if recent_views_json: #got memcache? return that
            return recent_views_json

        one_day = appetsy.strip_minutes(dt.datetime.now() - dt.timedelta(hours=23))
        views = storage.Counters.all().filter("name =", "last_24") \
                              .filter("shop =", self.shop) \
                              .filter("timestamp >=", one_day) \
                              .order("timestamp") \
                              .fetch(1000)

        for view in views:
            view.timestamp = appetsy.strip_minutes(view.timestamp)

        max_views = max([view.count for view in views] or [0])

        events = storage.Events.all().filter("created >", one_day) \
                             .filter("shop =", self.shop) \
                             .order("created").fetch(500)

        literal_events = dict(
            sold_out = "Sold",
            active = "Posted new",
            renewed = "Renewed",
            uknown = "Removed"
        )
        for event in events:
            event.event = literal_events.get(event.event) or event.event
            event.created = appetsy.strip_minutes(event.created)

        stat_events = {}
        for date_hour, event_group in groupby(events, lambda x: x.created):
            stat_events[date_hour] = list(event_group)


        faves = storage.ItemFans.all().filter("favored_on >", one_day) \
                             .filter("shop =", self.shop) \
                             .order("favored_on").fetch(500)
        faves.extend(storage.ShopFans.all().filter("favored_on >", one_day) \
                             .filter("shop =", self.shop) \
                             .order("favored_on").fetch(500))

        stat_faves = {}
        for date_hour, fave_group in groupby(faves, lambda x: x.favored_on.combine(x.favored_on.date(), dt.time(hour = x.favored_on.hour))):
            stat_faves[date_hour] = len(list(fave_group))


        views = dict([(view.timestamp, view) for view in views])

        stats = []
        for i in range(24):
            view_time = appetsy.strip_minutes(one_day + dt.timedelta(hours=i))

            stats.append(
                {'time': appetsy.to_local_time(self.shop, view_time),
                 'events': [event.event for event in stat_events[view_time]] if view_time in stat_events else [],
                 'faves': stat_faves[view_time] if view_time in stat_faves else 0,
                 'views': views[view_time].count if view_time in views else 0,
                 'rel_views': views[view_time].count / float(max_views) if float(max_views) and view_time in views else 0,
                }
            )



        recent_views_json = appetsy.json({"max_views": max_views, "views": stats})
        memcache.set("recent_views_json", recent_views_json, namespace =  str(self.shop.id))

        return recent_views_json