-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Piglet v1.1.0: - added complete new "future spends" feature - Bugfixes - Removed old code
- Loading branch information
k3nd0x
committed
Jun 28, 2023
1 parent
fd93ec7
commit f5b1825
Showing
17 changed files
with
484 additions
and
224 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
# vim: showmatch ts=4 sts=4 sw=4 autoindent smartindent smarttab expandtab | ||
|
||
from fastapi import APIRouter, Path, Depends,HTTPException | ||
from enum import Enum | ||
import json | ||
from pydantic import BaseModel | ||
from typing import Optional | ||
from datetime import datetime | ||
|
||
from .mysql import sql | ||
from .functs import get_budgetid,check,_get_uids,get_notisettings | ||
from .sendmail import mail | ||
|
||
from .admin import oauth2_scheme,get_current_user | ||
|
||
futurespends = APIRouter() | ||
|
||
@futurespends.get("/", summary="Get all future spends by userid or budget_id") | ||
async def spends(budget_id: int, max_entries: Optional[int]=30, current_user = Depends(get_current_user)): | ||
mysql = sql() | ||
check(mysql,current_user["bid_mapping"], budget_id) | ||
|
||
query = '''select (select name from registered_user where id=user_id) as user, (select name from pig_category where id=category_id) as category, CONCAT(value,' ',currency) AS value, id, DATE_FORMAT(timestamp, '%Y-%m-%d') as timestamp,description FROM pig_futurespends where budget_id={} order by timestamp DESC'''.format(budget_id) | ||
print(query,flush=True) | ||
response = mysql.get(query) | ||
|
||
max_count = 1 | ||
return_list = [] | ||
|
||
for i in response: | ||
if max_count <= max_entries: | ||
return_list.append(i) | ||
max_count += 1 | ||
|
||
graphquery = f'''select MONTH(TIMESTAMP) as monthnumber, MONTHNAME(TIMESTAMP) as month, sum(value) as value, currency from pig_futurespends where budget_id={budget_id} group by month;''' | ||
response = mysql.get(graphquery) | ||
|
||
currentMonth = int(datetime.now().month) | ||
|
||
return_dict = { "orders": return_list, "monthlist": [], "valuelist": [], "colorlist": [] } | ||
|
||
months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] | ||
updated_data = [] | ||
|
||
if len(response) == 0: | ||
for month_number, month_name in enumerate(months, start=1): | ||
updated_data.append({'monthnumber': month_number, 'month': month_name,'value': 0.0, 'currency': 'EUR'}) | ||
else: | ||
existing_data = {entry['monthnumber']: entry for entry in response} | ||
|
||
for month_number, month_name in enumerate(months, start=1): | ||
if month_number in existing_data: | ||
updated_data.append(existing_data[month_number]) | ||
else: | ||
updated_data.append({'monthnumber': month_number, 'month': month_name, | ||
'value': 0.0, 'currency': response[0]["currency"]}) | ||
|
||
print(updated_data,flush=True) | ||
|
||
for i in updated_data: | ||
if i["monthnumber"] >= currentMonth: | ||
value = float(i['value']) | ||
|
||
return_dict["monthlist"].append(i["month"]) | ||
return_dict["colorlist"].append('#2739ff') | ||
return_dict["valuelist"].append(value) | ||
|
||
|
||
mysql.close() | ||
return return_dict | ||
|
||
class newSpend(BaseModel): | ||
value: str | ||
userid: str | ||
category: str | ||
description: Optional[str] = None | ||
budget_id: str | ||
timestamp: str | ||
class Config: | ||
schema_extra = { | ||
"example": { | ||
"userid": "1", | ||
"category": "2", | ||
"value": "123.02", | ||
"budget_id": "151", | ||
"description": "Einkaufen Kupsch (ptional)", | ||
"timestamp": "2023-06-28 10:00:00" | ||
} | ||
} | ||
|
||
@futurespends.post("/new", summary="Place new order with payload") | ||
async def spends(newSpend: newSpend,current_user = Depends(get_current_user)): | ||
try: | ||
value = float(newSpend.value) | ||
category = int(newSpend.category) | ||
userid = int(newSpend.userid) | ||
budget_id = int(newSpend.budget_id) | ||
description = newSpend.description | ||
timestamp = newSpend.timestamp | ||
except: | ||
return "Variables not valid" | ||
mysql = sql() | ||
|
||
if userid != current_user["id"]: | ||
raise HTTPException(status_code=403, detail="Forbidden") | ||
|
||
check(mysql,current_user["bid_mapping"], budget_id) | ||
|
||
currency_query = f"select currency from pig_budgets where id={budget_id}" | ||
curr = mysql.get(currency_query)[0]["currency"] | ||
|
||
query = '''insert into pig_futurespends(value,currency,user_id,category_id,budget_id,description,timestamp) VALUES ({},"{}",{},{},{},"{}","{}")'''.format(value,curr,userid,category,budget_id,description,timestamp) | ||
print(query) | ||
insert = mysql.post(query) | ||
if insert == True: | ||
output = "Order added".format(userid,value, curr, category,description) | ||
|
||
|
||
mysql.close() | ||
return output | ||
@futurespends.delete("/{id}", summary="Delete order by timestamp") | ||
async def spends(id: str, budget_id: str,current_user = Depends(get_current_user)): | ||
mysql = sql() | ||
|
||
userid = current_user["id"] | ||
check(mysql,current_user["bid_mapping"], budget_id) | ||
|
||
query = '''delete from pig_futurespends where id="{}" and budget_id={}'''.format(id,budget_id) | ||
|
||
response = mysql.delete(query) | ||
|
||
mysql.close() | ||
|
||
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from flask import Flask, render_template, url_for, flash, redirect, request, session, send_from_directory | ||
from source.app import app | ||
|
||
from .api_func import get_data_api, post_data_api, del_data_api | ||
def allowed_exts(ext): | ||
allowed = [ 'jpeg', 'jpg', 'png'] | ||
ext = ext.lower() | ||
if ext in allowed: | ||
return True | ||
else: | ||
return False | ||
def auth(): | ||
auth = session["authorization"] | ||
|
||
return auth | ||
|
||
def get_notis(): | ||
userid = session["userid"] | ||
noticount, notilist, notifications = get_data_api("notis", data={ "uid": userid, "show_all": False},auth=auth()) | ||
|
||
return noticount, notilist, notifications |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
from flask import Flask, render_template, url_for, flash, redirect, request, session, send_from_directory | ||
from source.app import app | ||
from werkzeug.utils import secure_filename | ||
from hashlib import sha256 | ||
import os | ||
import json | ||
|
||
from .api_func import get_data_api, post_data_api, del_data_api | ||
from .funcs import get_notis, auth | ||
|
||
@app.route('/futurespends', methods=["GET"]) | ||
def futurespends(): | ||
if session: | ||
session["title"] = "futurespends" | ||
budget_id = session["budget_id"] | ||
data = { "budget_id": budget_id} | ||
if request.method == "GET": | ||
noticount, notilist, notifications = get_notis() | ||
session["title"] = "futurespends" | ||
apidata = get_data_api("futurespends", data=data,auth=auth()) | ||
orderlist = apidata["orders"] | ||
|
||
monthlist = apidata["monthlist"] | ||
valuelist = apidata["valuelist"] | ||
colorlist = apidata["colorlist"] | ||
|
||
categorylist = get_data_api("categorylist",data=budget_id,auth=auth()) | ||
|
||
return render_template("futurespends.html",orderlist=orderlist,notifications=notifications, notilist=notilist, noticount=noticount, monthlist=monthlist, valuelist=valuelist, colorlist=colorlist, categorylist=categorylist ) | ||
else: | ||
return redirect(url_for('login')) | ||
else: | ||
return redirect(url_for('login')) | ||
|
||
@app.route('/fdelete', methods=["GET","POST"]) | ||
def fdelete(): | ||
if session: | ||
if request.args['name'] == "futurespends": | ||
id = request.args['id'] | ||
data = { "id": id, "budget_id": session["budget_id"] } | ||
return_value = del_data_api("futurespends",data,auth=auth()) | ||
if return_value =="Entity deleted": | ||
flash_message = {"Entity deleted": "success"} | ||
else: | ||
flash_message = {"Error at delete": "danger"} | ||
|
||
flash(flash_message) | ||
return futurespends() | ||
|
||
@app.route('/new-futurespend', methods=["GET", "POST"]) | ||
def new_futurespend(): | ||
if session: | ||
budget_id = session["budget_id"] | ||
userid = session["userid"] | ||
if request.method == "POST": | ||
data = request.form.to_dict() | ||
data["userid"] = userid | ||
data["budget_id"] = budget_id | ||
|
||
response = post_data_api("futurespends", data,auth=auth()) | ||
|
||
if response == "Future spend added!": | ||
flash_message = {response: "danger"} | ||
else: | ||
flash_message = {response: "success"} | ||
|
||
flash(flash_message) | ||
|
||
return redirect(url_for('futurespends')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.