Skip to content

Commit

Permalink
Merge pull request #25 from hsuanyu414/new/formatter
Browse files Browse the repository at this point in the history
New/formatter
  • Loading branch information
hsuanyu414 authored Jun 9, 2024
2 parents 419aa6b + 463f5bd commit 36d9d05
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 101 deletions.
17 changes: 16 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
name: CI
on: [push, pull_request]
on: [push]

permissions:
contents: write
jobs:
test:
runs-on: ubuntu-latest
Expand All @@ -14,8 +17,20 @@ jobs:
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: autoformatter
run: pip install autopep8
- name: autopep8
run: autopep8 --in-place --recursive . --exclude ./test
- name: add commit
run: |
git config --global user.name 'GitHub Actions Bot'
git config --global user.email '[email protected]'
git add .
git diff --quiet && git diff --staged --quiet || git commit -m "autopep8"
- name: run tests
run: |
cd test
pytest . -v
- name: push changes
run: git push

114 changes: 64 additions & 50 deletions accounting/accounting.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
from models import Record
import sqlite3
import sys
import csv
import os
from fileio_wrapper import Fileio
sys.path.append('..')

from models import Record

class accountingFunction:
def __init__(self):
self.db_name = '../db.sqlite3'

def create_record(self, user_id, date, item, cost, category, comment):
success = False
record = None
Expand Down Expand Up @@ -58,13 +59,16 @@ def create_record(self, user_id, date, item, cost, category, comment):

try:
# insert new record
cursor.execute('INSERT INTO record (user_id, date, item, cost, category, comment) VALUES (?, ?, ?, ?, ?, ?)', (user_id, date, item, cost, category, comment))
cursor.execute('INSERT INTO record (user_id, date, item, cost, category, comment) VALUES (?, ?, ?, ?, ?, ?)',
(user_id, date, item, cost, category, comment))
new_record_id = cursor.lastrowid

# get record_id and create_date from new data
cursor.execute('SELECT * FROM record WHERE record_id = ?', (new_record_id,))
cursor.execute(
'SELECT * FROM record WHERE record_id = ?', (new_record_id,))
row = cursor.fetchone()
record = Record.Record(row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7])
record = Record.Record(
row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7])
record.date = str(record.date)
record.create_date = str(record.create_date)
success = True
Expand All @@ -77,7 +81,7 @@ def create_record(self, user_id, date, item, cost, category, comment):
# DB related end

return success, record, error_message

def show_recent_record(self, user_id, num=5, days=3, type='num'):
success = False
records = None
Expand All @@ -86,19 +90,19 @@ def show_recent_record(self, user_id, num=5, days=3, type='num'):
if not isinstance(user_id, int):
error_message = 'invalid user_id parameter'
return success, records, error_message

if not isinstance(num, int):
error_message = 'invalid num parameter'
return success, records, error_message

if not isinstance(days, int):
error_message = 'invalid day parameter'
return success, records, error_message
if type!='num' and type!='days':

if type != 'num' and type != 'days':
error_message = 'invalid type parameter'
return success, records, error_message

# DB related
conn = sqlite3.connect(self.db_name)
cursor = conn.cursor()
Expand All @@ -109,25 +113,27 @@ def show_recent_record(self, user_id, num=5, days=3, type='num'):
if row == None:
error_message = 'user_id does not exist'
return success, records, error_message

if type == 'num':
cursor.execute('SELECT * FROM record WHERE user_id = ? ORDER BY create_date DESC LIMIT ?', (user_id, num))
cursor.execute(
'SELECT * FROM record WHERE user_id = ? ORDER BY create_date DESC LIMIT ?', (user_id, num))
elif type == 'days':
cursor.execute('SELECT * FROM record WHERE user_id = ? AND create_date >= date("now", "-' + str(days) + ' day") ORDER BY create_date DESC', (user_id,))

cursor.execute('SELECT * FROM record WHERE user_id = ? AND create_date >= date("now", "-' +
str(days) + ' day") ORDER BY create_date DESC', (user_id,))

rows = cursor.fetchall()
records = []


for row in rows:
record = Record.Record(row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7])
record = Record.Record(
row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7])
record.date = str(record.date)
record.create_date = str(record.create_date)
records.append(record)
success = True

return success, records, error_message

def search_record(self, user_id, date_from, date_to=None):
success = False
records = None
Expand Down Expand Up @@ -157,21 +163,25 @@ def search_record(self, user_id, date_from, date_to=None):
return success, records, error_message

if date_to == None:
cursor.execute('SELECT * FROM record WHERE user_id = ? AND date = ?', (user_id, date_from))
cursor.execute(
'SELECT * FROM record WHERE user_id = ? AND date = ?', (user_id, date_from))
else:
cursor.execute('SELECT * FROM record WHERE user_id = ? AND date >= ? AND date <= ?', (user_id, date_from, date_to))
cursor.execute(
'SELECT * FROM record WHERE user_id = ? AND date >= ? AND date <= ?', (user_id, date_from, date_to))

rows = cursor.fetchall()
records = []

for row in rows:
record = Record.Record(row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7])
record = Record.Record(
row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7])
record.date = str(record.date)
record.create_date = str(record.create_date)
records.append(record)
success = True

return success, records, error_message

def update_record(self, user_id, record_id, date=None, item=None, cost=None, category=None, comment=None):
success = False
record = None
Expand Down Expand Up @@ -216,18 +226,22 @@ def update_record(self, user_id, record_id, date=None, item=None, cost=None, cat
cursor = conn.cursor()

# check if user_id exists
cursor.execute('SELECT * FROM record WHERE user_id = ? AND record_id = ?', (user_id, record_id))
cursor.execute(
'SELECT * FROM record WHERE user_id = ? AND record_id = ?', (user_id, record_id))
row = cursor.fetchone()
if row == None:
error_message = 'the record of this id does not exist'
return success, record, error_message
#update record
# update record
try:
cursor.execute('UPDATE record SET item = COALESCE(?, item), cost = COALESCE(?, cost), category = COALESCE(?, category), comment = COALESCE(?, comment), date = COALESCE(?, date) WHERE user_id = ? AND record_id = ?', (item, cost, category, comment, date, user_id, record_id))
cursor.execute('UPDATE record SET item = COALESCE(?, item), cost = COALESCE(?, cost), category = COALESCE(?, category), comment = COALESCE(?, comment), date = COALESCE(?, date) WHERE user_id = ? AND record_id = ?',
(item, cost, category, comment, date, user_id, record_id))
conn.commit()
cursor.execute('SELECT * FROM record WHERE user_id = ? AND record_id = ?', (user_id, record_id))
cursor.execute(
'SELECT * FROM record WHERE user_id = ? AND record_id = ?', (user_id, record_id))
row = cursor.fetchone()
record = Record.Record(row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7])
record = Record.Record(
row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7])
record.date = str(record.date)
record.create_date = str(record.create_date)
success = True
Expand All @@ -236,7 +250,7 @@ def update_record(self, user_id, record_id, date=None, item=None, cost=None, cat
conn.rollback()
conn.close()
return success, record, error_message

def delete_record(self, user_id, record_id):
success = False
error_message = None
Expand All @@ -256,14 +270,16 @@ def delete_record(self, user_id, record_id):
cursor = conn.cursor()

# check if the record of the user_id exists
cursor.execute('SELECT * FROM record WHERE user_id = ? AND record_id = ?', (user_id, record_id))
cursor.execute(
'SELECT * FROM record WHERE user_id = ? AND record_id = ?', (user_id, record_id))
row = cursor.fetchone()
if row == None:
error_message = 'the record of this id does not exist'
return success, None, error_message

try:
cursor.execute('DELETE FROM record WHERE record_id = ? AND user_id = ?', (record_id, user_id))
cursor.execute(
'DELETE FROM record WHERE record_id = ? AND user_id = ?', (record_id, user_id))
conn.commit()
success = True

Expand All @@ -273,21 +289,21 @@ def delete_record(self, user_id, record_id):

conn.close()
return success, None, error_message

def export_record(self, user_id, method='this month'):
#method: may this_month, this_year, all
#transit a csv file to the user
# method: may this_month, this_year, all
# transit a csv file to the user
success = False
error_message = None
link = None
if not isinstance(user_id, int):
error_message = 'invalid user_id parameter'
return success, link, error_message
return success, link, error_message

if method != 'this month' and method != 'this year' and method != 'all':
error_message = 'invalid method parameter'
return success, link, error_message
return success, link, error_message

# DB related
conn = sqlite3.connect(self.db_name)
cursor = conn.cursor()
Expand All @@ -298,36 +314,34 @@ def export_record(self, user_id, method='this month'):
link = row
if row == None:
error_message = 'user_id does not exist'
return success, link, error_message
return success, link, error_message

if method == 'this_month':
cursor.execute('SELECT * FROM record WHERE user_id = ? AND date >= date("now", "start of month")', (user_id,))
cursor.execute(
'SELECT * FROM record WHERE user_id = ? AND date >= date("now", "start of month")', (user_id,))
elif method == 'this_year':
cursor.execute('SELECT * FROM record WHERE user_id = ? AND date >= date("now", "start of year")', (user_id,))
cursor.execute(
'SELECT * FROM record WHERE user_id = ? AND date >= date("now", "start of year")', (user_id,))
elif method == 'all':
cursor.execute('SELECT * FROM record WHERE user_id = ?', (user_id,))
cursor.execute(
'SELECT * FROM record WHERE user_id = ?', (user_id,))

rows = cursor.fetchall()
conn.close()
# write to csv file
filepath = 'export_'+ str(user_id) +'.csv'
filepath = 'export_' + str(user_id) + '.csv'

with open(filepath, 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(['record_id', 'user_id', 'date', 'item', 'cost', 'category', 'comment', 'create_date'])
writer.writerow(['record_id', 'user_id', 'date', 'item',
'cost', 'category', 'comment', 'create_date'])
for row in rows:
writer.writerow(row)
# upload to file.io
resp = Fileio.upload(filepath, expires="5m")
success = resp['success'] # True if upload was successful
link = resp['link']
link = resp['link']
os.remove(filepath)
if not success:
error_message = 'upload failed'
return success, link, error_message






4 changes: 2 additions & 2 deletions init.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@


def init_db(db_name='db.sqlite3'):

"""
Initialize database with tables
user
- user_id (primary key) autoincrement
- line_id (unique)
Expand Down Expand Up @@ -50,5 +49,6 @@ def init_db(db_name='db.sqlite3'):
conn.commit()
conn.close()


if __name__ == '__main__':
init_db()
Loading

0 comments on commit 36d9d05

Please sign in to comment.