Skip to content

[WIP] use Flask 3.0.0 . deprecate using flask-validator #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ logs/
migrations/versions/*

diagrams/*.png

test_donate
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:3.8
FROM python:3.11
COPY . /app
WORKDIR /app
RUN mkdir /var/log/donate && \
Expand Down
4 changes: 2 additions & 2 deletions autoapp.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from dotenv import load_dotenv
import os
load_dotenv(os.environ['DONATE_DOTENV'])
#load_dotenv(os.environ['DONATE_DOTENV'])

from donate.app import create_app
from donate.database import db
from donate.extensions import db
from donate.log_utils import start_timer, log_request
from donate.models import DonateConfiguration
from donate.settings import DevConfig, ProdConfig, TestConfig
Expand Down
7 changes: 7 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
services:
web:
build: .
ports:
- "8000:5000"
redis:
image: "redis:alpine"
101 changes: 91 additions & 10 deletions donate/models.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from donate.extensions import db
from datetime import datetime
from flask_validator import (
ValidateInteger,
ValidateString,
ValidateNumeric,
)

from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.orm import validates
## replaced by ripping off
# https://github.com/learn-co-curriculum/python-p4-validation-flask-sqlalchemy-validations
#from flask_validator import (
# ValidateInteger,
# ValidateString,
# ValidateNumeric,
#)

class TimestampMixin():
''' Most objects (but not all) need a creation and updated timestamp '''
Expand Down Expand Up @@ -41,13 +44,28 @@ class User(db.Model):
# donations = db.relationship('Donation')
# subscriptions = db.relationship('StripeSubscription')

## replaced by ripping off
# https://github.com/learn-co-curriculum/python-p4-validation-flask-sqlalchemy-validations
"""
@classmethod
def __declare_last__(cls):
ValidateString(User.username, False, True)
ValidateString(User.email,
False,
True,
True,sqlalchemy.orm
"not a valid email address")
"""
@validates('username', 'name_first', 'name_last')
def validate_string(self, key, s):
if not isinstance(s, str):
raise ValueError("Failed string test")
return s

@validates('email')
def validate_email(self, key, address):
if '@' not in address:
raise ValueError("Failed simple email test")
return address


class Currency(db.Model, TimestampMixin):
Expand All @@ -61,10 +79,17 @@ class Currency(db.Model, TimestampMixin):
unique=True,
nullable=False)

"""
@classmethod
def __declare_last__(cls):
ValidateString(Currency.name, False, True)
ValidateString(Currency.code, False, True)
"""
@validates('name', 'code')
def validate_string(self, key, s):
if not isinstance(s, str):
raise ValueError("Failed string test")
return s


class Account(db.Model, TimestampMixin):
Expand All @@ -87,10 +112,24 @@ class Account(db.Model, TimestampMixin):
ccy_id = db.Column(db.Integer, db.ForeignKey('currency.id'))
ccy = db.relationship('Currency')

"""
@classmethod
def __declare_last__(cls):
ValidateString(Account.name, False, True)
ValidateInteger(Account.ccy_id, False, True)
"""
@validates('name')
def validate_string(self, key, s):
if not isinstance(s, str):
raise ValueError("Failed string test")
return s

@validates('ccy_id')
def validate_integer(self, key, i):
if not isinstance(i, int):
raise ValueError("Failed integer test")
return i



class Project(db.Model, TimestampMixin):
Expand All @@ -117,11 +156,23 @@ class Project(db.Model, TimestampMixin):

accounts = db.relationship('Account')

"""
@classmethod
def __declare_last__(cls):
ValidateString(Project.name, False, True)
ValidateNumeric(Project.goal, False, True)

"""
@validates('name')
def validate_string(self, key, s):
if not isinstance(s, str):
raise ValueError("Failed string test")
return s

@validates('goal')
def validate_number(self, key, g):
if (type(g) != int) and (type(g) != long) and (type(g) != float) and (type(g) != complex):
raise ValueError("Failed numeric test")
return g

class Donation(db.Model, TimestampMixin):
''' An amount of currency donated by a user, possibly anonymous.
Expand Down Expand Up @@ -219,6 +270,7 @@ class Transaction(db.Model, TimestampMixin):
# approver = db.relationship("User",
# foreign_keys=[approver_id])

"""
@classmethod
def __declare_last__(cls):
ValidateInteger(Transaction.ccy_id, False, True)
Expand All @@ -229,6 +281,18 @@ def __declare_last__(cls):
# ValidateInteger(Transaction.approver_id, False, True)
# validate that the transaction is between two accounts with the
# same ccy
"""
@validates('ccy_id','payer_id','recvr_id')
def validate_integer(self, key, i):
if not isinstance(i, int):
raise ValueError("Failed integer test")
return i

@validates('amount')
def validate_number(self, key, g):
if (type(g) != int) and (type(g) != long) and (type(g) != float) and (type(g) != complex):
raise ValueError("Failed numeric test")
return g


class StripeSubscription(db.Model, TimestampMixin):
Expand Down Expand Up @@ -273,14 +337,31 @@ class StripePlan(db.Model):
desc = db.Column(db.String(64),
nullable=False)
subscriptions = db.relationship('StripeSubscription')

"""
@classmethod
def __declare_last__(cls):
ValidateNumeric(StripePlan.amount, False, True)
ValidateString(StripePlan.name, False, True)
ValidateInteger(StripePlan.ccy_id, False, True)
ValidateString(StripePlan.interval, False, True)

"""
@validates('name','interval')
def validate_string(self, key, s):
if not isinstance(s, str):
raise ValueError("Failed string test")
return s

@validates('ccy_id')
def validate_integer(self, key, i):
if not isinstance(i, int):
raise ValueError("Failed integer test")
return i

@validates('amount')
def validate_number(self, key, g):
if (type(g) != int) and (type(g) != long) and (type(g) != float) and (type(g) != complex):
raise ValueError("Failed numeric test")
return g

class DonateConfiguration(db.Model, TimestampMixin):
__tablename__ = 'donate_configuration'
Expand Down
4 changes: 2 additions & 2 deletions donate/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
from flask import (
current_app as app,
flash,
Markup,
redirect,
render_template,
request,
Blueprint,
)
from markupsafe import Markup
from sqlalchemy.orm.exc import (
NoResultFound,
)
from donate.util import get_one
from donate.database import db
from donate.extensions import db
from donate.models import (
Account,
Project,
Expand Down
3 changes: 2 additions & 1 deletion donate/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
)

from flask import current_app as app
from donate.database import db
from donate.extensions import db



def get_one(cls, criteria):
Expand Down
54 changes: 33 additions & 21 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
Babel==2.6.0
Flask==1.1.4
Flask-Migrate==2.3.1
Flask-SQLAlchemy==2.5.1
SQLAlchemy==1.4.50
GitPython==3.0.9
git+https://github.com/xeBuz/[email protected]#egg=Flask-Validator
pdoc==0.3.2
pytest==5.3.5
pytest-cov==2.7.1
python-dotenv==0.10.1
sadisplay==0.4.9
Werkzeug==1.0.1
stripe==2.17.0
rfc3339==6.0
alembic==1.13.0
ansicolors==1.1.8
mysqlclient==1.4.2
prometheus_client==0.7.1
requests==2.24.0
Jinja2==2.11.3
itsdangerous==1.1.0
MarkupSafe==1.1.1
blinker==1.7.0
certifi==2023.11.17
charset-normalizer==3.3.2
click==8.1.7
coverage==7.3.2
Flask==3.0.0
Flask-Migrate==4.0.5
Flask-SQLAlchemy==3.1.1
greenlet==3.0.1
idna==3.6
iniconfig==2.0.0
itsdangerous==2.1.2
Jinja2==3.1.2
Mako==1.3.0
MarkupSafe==2.1.3
mysqlclient==2.2.0
packaging==23.2
pdoc==14.1.0
pluggy==1.3.0
prometheus-client==0.19.0
Pygments==2.17.2
pytest==7.4.3
pytest-cov==4.1.0
python-dotenv==1.0.0
requests==2.31.0
rfc3339==6.2
sadisplay==0.4.9
SQLAlchemy==2.0.23
stripe==7.7.0
typing_extensions==4.8.0
urllib3==2.1.0
Werkzeug==3.0.1