From dd22c46950aaae908129c6b4fccd2a87d6054bfa Mon Sep 17 00:00:00 2001 From: Statz Tatsumi Saines Date: Mon, 22 Jan 2024 11:54:55 -0500 Subject: [PATCH] validations --- server/models.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/server/models.py b/server/models.py index 9152b83..03f1c10 100644 --- a/server/models.py +++ b/server/models.py @@ -1,5 +1,6 @@ from sqlalchemy_serializer import SerializerMixin from sqlalchemy.ext.associationproxy import association_proxy +from sqlalchemy.orm import validates from config import db @@ -14,6 +15,28 @@ class Users(db.Model,SerializerMixin): #__RELATIONSHIPS swipes = db.relationship("Swipes", back_populates="user") + #_SERIALIZATION + serialize_rules = ('-swipes.user', ) + + #_VALIDATIONS + @validates('username') + def validate_username(self, key, value): + if 0 < len(value) <= 25: + return value + else: + raise ValueError + + @validates('passwordhash') + def validate_passwordhash(self, key, value): + if 0 < len(value) <= 25: + return value + else: + raise ValueError + + #validation for @ (valid email) in user_email + + def __repr__(self): + return f'' class Recipes(db.Model,SerializerMixin): __tablename__ = "recipe" @@ -26,6 +49,14 @@ class Recipes(db.Model,SerializerMixin): #__RELATIONSHIPS swipes = db.relationship("Swipes", back_populates="recipe") + #_SERIALIZATION + serialize_rules = ('-swipes.recipe', ) + + #_VALIDATIONS + + def __repr__(self): + return f'' + class Swipes(db.Model, SerializerMixin): __tablename__ = "swipes" @@ -39,3 +70,11 @@ class Swipes(db.Model, SerializerMixin): #__RELATIONSHIPS user = db.relationship("Users", back_populates = "swipes") recipe = db.relationship("Recipes", back_populates = "swipes") + + #_SERIALIZATIONS + serialize_rules = ('-user.swipes', '-recipe.swipes') + + #_VALIDATIONS + + def __repr__(self): + return f''