def register(request,edit): from social.user_history import History try: user_id = request.session.get('fb_id', False) user = User.objects.get(id=user_id) if user.meta != None and not edit: return HttpResponseRedirect('/') elif user.meta == None and edit: return HttpResponseRedirect('/registrar/') except: return HttpResponseRedirect('/login/') if request.POST: getdict = request.POST goal_number = int(getdict.get('goal-num'),10) goal_time = getdict.get('goal-time') if goal_time == 'week': goal = goal_number * 7 elif goal_time == 'month': goal = goal_number * 30 elif goal_time == 'year': goal = goal_number * 365 else: goal = goal_number #só em dias por enquanto goal_date = datetime.datetime.now() + datetime.timedelta(days=+goal) user.privacy = getdict.get('privacy') user.meta = goal_date user.save() amount = int(getdict.get('amount')) if amount > 200: amount = 200 user_data = {'user': user, 'amount': amount, 'cigarette': getdict.get('cigarette'), 'method': 'post' } history = History(user_data) history_id = history.save() return HttpResponseRedirect('/') from social.models import UserHistory from cigarette.models import Cigarette from django.core.cache import cache savings = cache.get('cache_saved') if not savings: from calc.stats import StatsView def return_saving_per_user(u): history = UserHistory.objects.filter(user=u).order_by('date') stats = StatsView.Stats(u, history) return stats.meta_sum_saved() savings = float(sum( [return_saving_per_user(u) for u in User.objects.all()])) cache.set('cache_saved', savings, 3600) users = User.objects.filter(privacy=2).exclude(meta=None).order_by('-created')[:7] total_users = User.objects.count() cigarettes = Cigarette.objects.all() total_stop_users = UserHistory.objects.values('user').distinct().filter(amount=0).count() RES = { 'EDIT': edit, 'USER': user, 'USERS': users, 'TOTAL_USERS': total_users, 'TOTAL_STOP_USERS': total_stop_users, 'SAVINGS': "%.02f" % savings, 'CIGARETTE': cigarettes } if edit: RES['goal_time'] = 'day' RES['goal_date'] = (user.meta - datetime.date.today()).days if RES['goal_date'] % 7 == 0: RES['goal_date'] = RES['goal_date']/7 RES['goal_time'] = 'week' if RES['goal_date'] % 30 == 0: RES['goal_date'] = RES['goal_date']/30 RES['goal_time'] = 'month' if RES['goal_date'] % 365 == 0: RES['goal_date'] = RES['goal_date']/365 RES['goal_time'] = 'year' history = History({'user': user, 'method':'get'}) last_history = history.get_last_history() RES['amount'] = last_history.amount RES['privacy'] = user.privacy RES['cigarette_id'] = last_history.cigarette.id return render_to_response( 'register.tpl', RES, context_instance = RequestContext(request), )