generated from TempeHS/Secure_Flask_PWA_Template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiary_management.py
184 lines (174 loc) · 5.38 KB
/
diary_management.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
from flask import jsonify
import sqlite3 as sql
from jsonschema import validate
from flask import current_app
from datetime import datetime
import logging
schema = {
"type": "object",
"validationLevel": "strict",
"required": [
"devtag",
"project",
"start_time",
"end_time",
"time_worked",
"repo",
"developer_notes",
"code_additions",
"diary_entry",
],
"properties": {
"devtag": {"type": "string"},
"project": {"type": "string"},
"start_time": {"type": "string"},
"end_time": {"type": "string"},
"diary_entry": {"type": "string"},
"time_worked": {"type": "string"},
"repo": {"type": "string"},
"developer_notes": {"type": "string"},
"code_additions": {"type": "string"},
},
}
def diary_add(entry):
if validate_json(entry):
con = sql.connect(".databaseFiles/database.db")
cur = con.cursor()
cur.execute(
"INSERT INTO diary_entries (devtag, project, start_time, end_time, diary_entry, time_worked, repo, developer_notes, code_additions) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);",
[
entry["devtag"],
entry["project"],
entry["start_time"],
entry["end_time"],
entry["diary_entry"],
entry["time_worked"],
entry["repo"],
entry["developer_notes"],
entry["code_additions"],
],
)
con.commit()
con.close()
return {"message": "Extension added successfully"}, 201
else:
return {"error": "Invalid JSON"}, 400
def diary_get():
con = sql.connect(".databaseFiles/database.db")
cur = con.cursor()
cur.execute("SELECT * FROM diary_entries")
migrate_data = [
dict(
id=row[0],
devtag=row[1],
project=row[2],
start_time=row[3],
end_time=row[4],
time_worked=row[5],
repo=row[6],
developer_notes=row[7],
code_additions=row[8],
diary_entry=row[9],
)
for row in cur.fetchall()
]
return jsonify(migrate_data)
def get_entry(entry_id):
con = sql.connect(".databaseFiles/database.db")
cur = con.cursor()
cur.execute("SELECT * FROM diary_entries WHERE id=?", [entry_id])
row = cur.fetchone()
con.close()
if row:
entry = {
"id": row[0],
"devtag": row[1],
"project": row[2],
"start_time": row[3],
"end_time": row[4],
"time_worked": row[5],
"repo": row[6],
"developer_notes": row[7],
"code_additions": row[8],
"diary_entry": row[9],
}
return jsonify(entry)
else:
return jsonify({"error": "Entry not found"}), 404
def diary_search(filters=None):
con = sql.connect(".databaseFiles/database.db")
cur = con.cursor()
query = "SELECT * FROM diary_entries"
params = []
if filters:
conditions = []
if "devtag" in filters and filters["devtag"]:
conditions.append("devtag LIKE ?")
params.append(f"%{filters['devtag']}%")
if "project" in filters and filters["project"]:
conditions.append("project LIKE ?")
params.append(f"%{filters['project']}%")
if "repo" in filters and filters["repo"]:
conditions.append("repo LIKE ?")
params.append(f"%{filters['repo']}%")
if "diary_entry" in filters and filters["diary_entry"]:
conditions.append("diary_entry LIKE ?")
params.append(f"%{filters['diary_entry']}%")
if conditions:
query += " WHERE " + " AND ".join(conditions)
logging.debug(f"Executing query: {query} with params: {params}")
cur.execute(query, params)
rows = cur.fetchall()
con.close()
migrate_data = [
dict(
id=row[0],
devtag=row[1],
project=row[2],
start_time=row[3],
end_time=row[4],
time_worked= row[5],
repo=row[6],
developer_notes=row[7],
code_additions=row[8],
diary_entry=row[9],
)
for row in rows
]
return jsonify(migrate_data)
def download(devtag):
con = sql.connect(".databaseFiles/database.db")
cur = con.cursor()
cur.execute("SELECT * FROM diary_entries WHERE devtag=?", [devtag])
rows = cur.fetchall()
con.close()
migrate_data = [
dict(
id=row[0],
devtag=row[1],
project=row[2],
start_time=row[3],
end_time=row[4],
time_worked=row[5],
repo=row[6],
developer_notes=row[7],
code_additions=row[8],
diary_entry=row[9],
)
for row in rows
]
return jsonify(migrate_data)
def delete_user(devtag):
con = sql.connect(".databaseFiles/database.db")
cur = con.cursor()
cur.execute("DELETE FROM diary_entries WHERE devtag=?", [devtag])
cur.execute("DELETE FROM users WHERE devtag=?", [devtag])
con.commit()
con.close()
return {"message": "User and Data deleted successfully"}
def validate_json(json_data):
try:
validate(instance=json_data, schema=schema)
return True
except:
return False