def assembleData(json=False): if settings.CACHE: ui_breakdown = memcache.get("%s-results-ui_breakdown" % settings.VERSION) overtime = memcache.get("%s-results-overtime" % settings.VERSION) else: ui_breakdown = None overtime = None if ui_breakdown is None: ui_breakdown = { "input" :{}, "select" :{}, "slider" :{}, "radio" :{}, } for m in ui_breakdown: ui_breakdown[m]["random_specified"] = { "data": [0,0,0,0,0,0,0,0,0,0] } ui_breakdown[m]["random_not_specified"] = { "data": [0,0,0,0,0,0,0,0,0,0] } stats = VoteStat.all().filter("showed_random !=", None) for stat in stats: if stat.showed_random: random_text = "random_specified" else: random_text = "random_not_specified" ui_breakdown[stat.method][random_text]["data"][stat.number-1] = stat.count ui_breakdown[stat.method][random_text]["generated"] = "%s UTC" % stat.generated.strftime("%Y-%m-%d %H:%M") if overtime is None: overtime = [ [] for n in range(0,10) ] hend = datetime(2011,3,28,17,11,56) hourcount = HourNumberCount.all().filter("hour_end <", hend) for stat in hourcount: t = time.mktime(stat.hour_start.timetuple()) c = stat.count overtime[stat.number-1].append( (t,c) ) if settings.CACHE: memcache.add("%s-results-ui_breakdown" % settings.VERSION, ui_breakdown, settings.CACHE_LIFE) memcache.add("%s-results-overtime" % settings.VERSION, overtime, settings.CACHE_LIFE) result = { "ui_breakdown" : ui_breakdown, "over_time" : overtime, } if json: return simplejson.dumps(result) else: return result
def post(self): method = self.request.get("method", None) number = self.request.get("number", None) cursor = self.request.get("cursor", None) refresh = self.request.get("refresh", None) random_text = self.request.get("random_text", "") try: number = int(number) except: number = None if random_text == "random": showed_random = True else: showed_random = False if method and number and method in OPTIONS: key_name = "%s-%s-%s" % (method, number, showed_random) stat = VoteStat.get_or_insert(key_name) q = Vote.all() q.filter("method =", method) q.filter("number =", number) q.filter("showed_random =", showed_random) if cursor: q.with_cursor(cursor) count = q.count() next_cursor = q.cursor() if count == 1000 else None if refresh or not stat.count: stat.count = 0 stat.count = stat.count + count stat.method = method stat.number = number stat.showed_random = showed_random stat.generated = datetime.now() stat.put() if next_cursor: params = {"number": number, "method": method, "cursor": next_cursor, "random_text": random_text} taskqueue.add(url="/tasks/tally-votes", params=params)
def vote(id): content = Content.query.get_or_404(id) vote = VoteStat.query.filter_by(content_id=id, user_id=current_user.id).first() vote_stat = request.form.get('vote') j = 0 if int(vote_stat) == 1: if vote: content.vote_count -= 1 db.session.delete(vote) db.session.commit() j = 2 return json.dumps({'result': 0, 'count': content.vote_count}) else: content.vote_count += 1 db.session.commit() add_vote = VoteStat(user_id=current_user.id, content_id=id) db.session.add(add_vote) db.session.commit() j = 3 return json.dumps({'result': 1, 'count': content.vote_count})
def assembleData(json=False): if settings.CACHE: ui_breakdown = memcache.get("%s-results-ui_breakdown" % settings.VERSION) overtime = memcache.get("%s-results-overtime" % settings.VERSION) else: ui_breakdown = None overtime = None if ui_breakdown is None: ui_breakdown = { "input": {}, "select": {}, "slider": {}, "radio": {}, } for m in ui_breakdown: ui_breakdown[m]["random_specified"] = { "data": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] } ui_breakdown[m]["random_not_specified"] = { "data": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] } stats = VoteStat.all().filter("showed_random !=", None) for stat in stats: if stat.showed_random: random_text = "random_specified" else: random_text = "random_not_specified" ui_breakdown[stat.method][random_text]["data"][stat.number - 1] = stat.count ui_breakdown[stat.method][random_text][ "generated"] = "%s UTC" % stat.generated.strftime( "%Y-%m-%d %H:%M") if overtime is None: overtime = [[] for n in range(0, 10)] hend = datetime(2011, 3, 28, 17, 11, 56) hourcount = HourNumberCount.all().filter("hour_end <", hend) for stat in hourcount: t = time.mktime(stat.hour_start.timetuple()) c = stat.count overtime[stat.number - 1].append((t, c)) if settings.CACHE: memcache.add("%s-results-ui_breakdown" % settings.VERSION, ui_breakdown, settings.CACHE_LIFE) memcache.add("%s-results-overtime" % settings.VERSION, overtime, settings.CACHE_LIFE) result = { "ui_breakdown": ui_breakdown, "over_time": overtime, } if json: return simplejson.dumps(result) else: return result