Skip to content

Commit

Permalink
pylint updates
Browse files Browse the repository at this point in the history
  • Loading branch information
caseybecking committed Nov 18, 2024
1 parent c7d8add commit 26ffd32
Show file tree
Hide file tree
Showing 20 changed files with 129 additions and 45 deletions.
22 changes: 22 additions & 0 deletions api/account/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,30 @@

@g.api.route('/account/signup')
class Signup(Resource):
"""
Signup Resource for creating a new user account.
This resource handles the POST request to create a new user account. It expects
a JSON payload with the user's email, username, password, first name, and last name.
It checks if the email or username already exists in the database and returns an error
message if they do. Otherwise, it creates a new user with the provided details.
Methods:
post: Handle POST requests to create a new user account.
"""
@g.api.expect(account_model)
def post(self):
"""
Handle POST requests to create a new user account.
This method extracts the user details from the request JSON payload, checks if the
email or username already exists in the database, and returns an error message if they do.
If the email and username are unique, it creates a new user with the provided details
and hashes the password using the scrypt method.
Returns:
Response: JSON response with a success or error message.
"""
data = request.json
email = data.get('email')
username = data.get('username')
Expand Down
19 changes: 17 additions & 2 deletions api/base/models.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
from app import db
import uuid
from app import db

class Base(db.Model):
"""
Base represents the base table in the database.
Attributes:
id (str): The ID of the base.
created_at (datetime): The date and time the base was created.
updated_at (datetime): The date and time the base was last updated.
"""

__abstract__ = True

id = db.Column('id', db.Text, default=lambda: str(uuid.uuid4()), primary_key=True)
created_at = db.Column(db.DateTime, default=db.func.current_timestamp())
updated_at = db.Column(db.DateTime, default=db.func.current_timestamp(), onupdate=db.func.current_timestamp())

def save(self):
"""
Save the Base instance to the database.
"""
db.session.add(self)
db.session.commit()

def delete(self):
"""
Delete the Base instance from the database.
"""
db.session.delete(self)
db.session.commit()
db.session.commit()
2 changes: 1 addition & 1 deletion api/categories/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def __repr__(self):
Returns:
str: String representation of the category.
"""
return '<Categories %r>' % self.name
return f'<Categories {self.name!r}>'

def to_dict(self):
"""
Expand Down
2 changes: 1 addition & 1 deletion api/categories_group/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def __init__(self, user_id, name):
self.name = name

def __repr__(self):
return '<CategoriesGroup %r>' % self.name
return f'<CategoriesGroup {self.name!r}>'

def to_dict(self):
return {
Expand Down
2 changes: 1 addition & 1 deletion api/categories_type/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ def post(self):
def get(self):
categories_type = CategoriesTypeModel.query.all()
_categories_type = [category_type.to_dict() for category_type in categories_type]
return make_response(jsonify({'categories_type': _categories_type}), 200)
return make_response(jsonify({'categories_type': _categories_type}), 200)
4 changes: 2 additions & 2 deletions api/categories_type/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ def __init__(self, user_id, name):
self.name = name

def __repr__(self):
return '<CategoriesType %r>' % self.name
return f'<CategoriesType {self.name!r}>'

def to_dict(self):
return {
'id': self.id,
Expand Down
2 changes: 1 addition & 1 deletion api/institution/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def __init__(self, user_id, name, location, description):
self.description = description

def __repr__(self):
return '<Institution %r>' % self.name
return f'<Institution {self.name!r}>'

def to_dict(self):
return {
Expand Down
6 changes: 2 additions & 4 deletions api/institution_account/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ def __init__(self, institution_id, user_id, name, number, status, balance):
self.balance = balance

def __repr__(self):
return '<Account %r>' % self.name
return f'<Account {self.name!r}>'

def to_dict(self):
return {
'id': self.id,
Expand All @@ -43,5 +43,3 @@ def save(self):
def delete(self):
db.session.delete(self)
db.session.commit()


7 changes: 5 additions & 2 deletions api/transaction/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
'categories_id': fields.String(required=True, description='Categories ID'),
'account_id': fields.String(required=True, description='Account ID'),
'amount': fields.Float(required=True, description='Amount'),
'transaction_type': fields.String(required=True, description='Transaction Type')
'transaction_type': fields.String(required=True, description='Transaction Type'),
'external_id': fields.String(description='External ID')
})

@g.api.route('/transaction')
Expand All @@ -20,13 +21,15 @@ def post(self):
account_id = data.get('account_id')
amount = data.get('amount')
transaction_type = data.get('transaction_type')
external_id = data.get('external_id')

new_transaction = TransactionModel(
user_id=user_id,
categories_id=categories_id,
account_id=account_id,
amount=amount,
transaction_type=transaction_type
transaction_type=transaction_type,
external_id=external_id
)
new_transaction.save()

Expand Down
53 changes: 48 additions & 5 deletions api/transaction/models.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,84 @@
from app import db
from api.base.models import Base


class TransactionModel(Base):
"""
TransactionModel represents the transaction table in the database.
Attributes:
user_id (str): The ID of the user associated with the transaction.
categories_id (str): The ID of the category associated with the transaction.
account_id (str): The ID of the account associated with the transaction.
amount (float): The amount of the transaction.
type (str): The type of the transaction.
external_id (str): The external ID of the transaction.
"""

__tablename__ = 'transaction'
user_id = db.Column('user_id', db.Text, db.ForeignKey('user.id'), nullable=False)
categories_id = db.Column('categories_id', db.Text, db.ForeignKey('categories.id'), nullable=False)
account_id = db.Column('account_id', db.Text, db.ForeignKey('account.id'), nullable=False)
amount = db.Column(db.Float, nullable=False)
transaction_type = db.Column(db.String(255), nullable=False)
external_id = db.Column(db.String(255), nullable=False)

def __init__(self, user_id, categories_id, account_id, amount, transaction_type, external_id):
"""
Initialize a TransactionModel instance.
def __init__(self, user_id, categories_id, account_id, amount, transaction_type):
Args:
user_id (str): The ID of the user associated with the transaction.
categories_id (str): The ID of the category associated with the transaction.
account_id (str): The ID of the account associated with the transaction.
amount (float): The amount of the transaction.
transaction_type (str): The transaction_type of the transaction.
external_id (str): The external ID of the transaction.
"""
self.user_id = user_id
self.categories_id = categories_id
self.account_id = account_id
self.amount = amount
self.transaction_type = transaction_type
self.external_id = external_id

def __repr__(self):
return '<Transaction %r>' % self.id

"""
Return a string representation of the TransactionModel instance.
Returns:
str: String representation of the transaction.
"""
return f'<Transaction {self.id!r}>'

def to_dict(self):
"""
Convert the TransactionModel instance to a dictionary.
Returns:
dict: Dictionary representation of the transaction.
"""
return {
'id': self.id,
'user_id': self.user_id,
'categories_id': self.categories_id,
'account_id': self.account_id,
'amount': self.amount,
'transaction_type': self.transaction_type,
'external_id': self.external_id,
'created_at': self.created_at,
'updated_at': self.updated_at
}

def save(self):
"""
Save the TransactionModel instance to the database.
"""
db.session.add(self)
db.session.commit()

def delete(self):
"""
Delete the TransactionModel instance from the database.
"""
db.session.delete(self)
db.session.commit()
db.session.commit()
1 change: 0 additions & 1 deletion api/user/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,3 @@ def get(self):
users = _user_model.query.all()
users = [user.to_dict() for user in users]
return make_response(jsonify({'users': users}), 200)

7 changes: 2 additions & 5 deletions api/user/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import secrets
from flask_login import UserMixin
from app import db
from api.base.models import Base
import secrets

class User(Base, UserMixin):
__tablename__ = 'user'
Expand All @@ -21,7 +21,7 @@ def __init__(self, email, username, password, first_name, last_name):
self.api_key = None # Initialize without an API key

def __repr__(self):
return '<User %r>' % self.username
return f'<User {self.username!r}>'

def to_dict(self):
return {
Expand All @@ -38,6 +38,3 @@ def generate_api_key(self):
"""Generate a new unique API key and save it to the database."""
self.api_key = secrets.token_hex(32) # Generate a 64-character API key
db.session.commit() # Save the new key to the database



8 changes: 4 additions & 4 deletions app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# CSV
import csv
# Flask
from flask import Flask
from flask import g
Expand All @@ -8,8 +10,7 @@
# Baseline
from app.config import Config
from app.database import db
# CSV
import csv


def create_app():
app = Flask(__name__)
Expand Down Expand Up @@ -58,10 +59,9 @@ def create_app():

#CLI
from app.cli import insert_categories
@app.cli.command('insert-categories')
@app.cli.command('insert-categories')
def insert_cat():
insert_categories()


db.create_all()

Expand Down
8 changes: 4 additions & 4 deletions app/account/controllers.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from flask import Blueprint, render_template, request, redirect, url_for
from flask import Blueprint, render_template, redirect, url_for
from flask_login import logout_user, login_required

account_blueprint = Blueprint('account', __name__)

@account_blueprint.route('/account/signup')
def signup():
@account_blueprint.route('/account/signup')
def signup():
return render_template('account/signup.html')

@account_blueprint.route('/account/login')
Expand All @@ -15,4 +15,4 @@ def login():
@login_required
def logout():
logout_user()
return redirect(url_for('account.login'))
return redirect(url_for('account.login'))
12 changes: 6 additions & 6 deletions app/cli.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# CSV
import csv
# APP
from app import db
from app.config import Config
# CSV
import csv
# MODELS
from api.categories_type.models import CategoriesTypeModel as CategoriesType
from api.categories_group.models import CategoriesGroupModel as CategoriesGroup
Expand All @@ -12,7 +12,7 @@ def insert_categories():
user_id = Config.DEFAULT_USER_ID
categories_type = []
categories_group = []
with open("data/categories_data.csv", "r") as f_in:
with open("data/categories_data.csv", "r", encoding=str) as f_in:
reader = csv.reader(f_in, quotechar="'")
next(reader) # skip header
## categories_type
Expand All @@ -35,8 +35,8 @@ def insert_categories():
cat = CategoriesGroup(name=category_group, user_id=user_id)
db.session.add(cat)
db.session.commit()
with open("data/categories_data.csv", "r") as f_in:

with open("data/categories_data.csv", "r", encoding=str) as f_in:
reader = csv.reader(f_in, quotechar="'")
next(reader) # skip header
## categories_type
Expand All @@ -50,4 +50,4 @@ def insert_categories():
if _categories is None:
cat = Categories(name=category, categories_group_id=_category_group_id, categories_type_id=_category_type_id, user_id=user_id)
db.session.add(cat)
db.session.commit()
db.session.commit()
2 changes: 1 addition & 1 deletion app/dashboard/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
@dashboards.route('/')
@login_required
def index():
return render_template('dashboards/index.html')
return render_template('dashboards/index.html')
2 changes: 1 addition & 1 deletion app/database.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()
db = SQLAlchemy()
2 changes: 1 addition & 1 deletion app/institution/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
@login_required
def institution():
api_url = url_for('institution', _external=True)
response = requests.get(api_url)
response = requests.get(api_url, timeout=15)
institutions = response.json().get('institutions', [])
user_id = session.get('_user_id')

Expand Down
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
app = create_app()

if __name__ == '__main__':
app.run(debug=True)
app.run(debug=True)
Loading

0 comments on commit 26ffd32

Please sign in to comment.