def money_add(request): if request.method == 'POST': form = MoneyForm(request.POST) if form.is_valid(): form.save() year = localtime(timezone.now()).year month = localtime(timezone.now()).month try: themonth = Month.objects.get(date=year*100+month) except ObjectDoesNotExist: themonth = Month(date=year*100+month) themonth.save() return redirect("/money/"+str(themonth.date)) else: form = MoneyForm() return render_to_response('form.html',{'form': form}, context_instance=RequestContext(request))
def diary_add(request): if request.method == 'POST': form = DiaryForm(request.POST) if form.is_valid(): form.save() year = localtime(timezone.now()).year month = localtime(timezone.now()).month try: themonth = Month.objects.get(date=year*100+month) except ObjectDoesNotExist: themonth = Month(date=year*100+month) themonth.save() return redirect("/diary") else: form = DiaryForm() return render_to_response('form.html',{'form': form}, context_instance=RequestContext(request))
def api(): token = request.headers.get('Authorization') content = request.json if token and token.startswith('Basic') and len(content): fetched_content = content.pop() parsed_key = base64.b64decode(token.split()[1]).decode('utf-8') user = User.get_user(parsed_key) if user and fetched_content: payload = dict() payload['user_last_name'] = user.last_name payload_for_today = Payload.check_today(user.last_name) current_month = Month.check_month(user.last_name) if payload_for_today: last_time = payload_for_today.last_time current_time = get_time(fetched_content['time']) delta = hms_to_m(make_delta(last_time, current_time)) if delta > 10: payload_for_today.last_time = current_time db.session.commit() return Response(status='200') current_day = Day.get_current_day(user.last_name) payload['date'] = get_today() payload['running_min'] = delta if current_day: current_day_running_min = current_day.running_min current_day.running_min = current_day_running_min + delta else: db.session.add(Day(**payload)) if current_month: current_month_running_min = current_month.running_min current_month.running_min = current_month_running_min + delta else: db.session.add(Month(**payload)) payload_for_today.last_time = current_time db.session.commit() else: payload['last_time'] = get_time(fetched_content['time']) db.session.add(Payload(**payload)) db.session.commit() return Response(status='200')
def user(): content = request.json user = User.query.filter_by(last_name=content.get('last_name')).first() if user: response = jsonify({'status': 302, 'message': f"Пользователь с фамилией {user.last_name} уже существует"}) response.status_code = 302 return response db.session.add(User(**content)) db.session.commit() new_month = Month(date=get_today(), user_last_name=content['last_name']) db.session.add(new_month) db.session.commit() response = jsonify({'status': 201, 'message': "Created"}) response.status_code = 201 return response
def get(self): month = Month.get_current_month() if month: self.redirect("/month/" + month.key.urlsafe()) else: self.redirect("/")
def retrieve_month(): content = request.json if content.get('month'): return jsonify(data=[m.serialize for m in Month.get_retrieve_month(content.get('month'))]) return Response(status='404')
def check_month(): # gets the difference in months to be used in relativedelta. def diff_month(d1, d2): return (d1.year - d2.year) * 12 + d1.month - d2.month # checks if month table is empty, then adds a month base month. monthFetch = session.query(Month.month_name).order_by( db.desc(Month.month_name)).limit(1) if monthFetch.first() == None: m = Month(month_name=datetime.datetime(2020, 1, 1)) session.add(m) session.commit() # checks the current month, if not, sets up a new month to be current, resets bills back to original amount due, and updates income to new/current month. monthFetch = session.query(Month.month_name).order_by( db.desc(Month.month_name)).limit(1) if monthFetch.first()[0] == str(this_month): print( f"Financial App!!\n\nWe are now in the month of {date_today.strftime('%B').upper()}.\n" ) else: print("New month started! Initializing...\n\n\n") m = Month(month_name=this_month) session.add(m) session.commit() # loops through all entries and sets the actual amount due back to the base amount, and increases the due date to the current month. for id, base_amount_due, due_date in session.query( Bills.id, Bills.base_amount_due, Bills.due_date): i = diff_month(date_today, due_date) newDate = due_date + relativedelta(months=+i) session.query(Bills).filter(Bills.id == id).update({ Bills.due_date: newDate, Bills.actual_amount_due: base_amount_due }) # loops through income table and updates/increases the pay day to the current month. a = session.query(Income.user, Income.base_income_amount, func.max(Income.pay_day), Income.pay_day_frequency).group_by(Income.user).\ filter(or_(Income.pay_day_frequency=='biweekly', Income.pay_day_frequency=='weekly')) b = session.query(Income.user, Income.base_income_amount, Income.pay_day, Income.pay_day_frequency).\ filter(or_(Income.pay_day_frequency=='bimonthly', Income.pay_day_frequency=='monthly')) for user, base_income_amount, pay_day, pay_day_frequency in a.union_all( b): if pay_day_frequency == 'weekly': rrule_date = str(pay_day).replace('-', '')[:-9] rr_pay_days(user, base_income_amount, rrule_date, pay_day_frequency) elif pay_day_frequency == 'biweekly': rrule_date = str(pay_day).replace('-', '')[:-9] rr_pay_days(user, base_income_amount, rrule_date, pay_day_frequency) else: i = diff_month(date_today, pay_day) new_pd = pay_day + relativedelta(months=+i) add_income(user, base_income_amount, new_pd, pay_day_frequency) session.commit() print( f"Financial App!!\n\nWe are now in the month of {date_today.strftime('%B').upper()}.\n" )