Skip to content
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

Add Methods to ease database additions , query etc #43

Open
DiptoChakrabarty opened this issue Sep 30, 2021 · 0 comments
Open

Add Methods to ease database additions , query etc #43

DiptoChakrabarty opened this issue Sep 30, 2021 · 0 comments

Comments

@DiptoChakrabarty
Copy link
Owner

DiptoChakrabarty commented Sep 30, 2021

Currently for the different operations we are querying , adding to database and deleting to database directly in the implementation of the routes .

The aim is to add a couple of database methods to add , delete or query the database for the different models.

The different models present are

  • userdetails
  • education
  • experience
  • projects
  • skills
  • achievements

Send your PR with modifications for any one model and also replace with these methods in the routes , do not send for multiple models at a time.

Check Usermodel which has these methods implemented

class UserModel(db.Model,UserMixin):
__tablename__= "user"
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(20),unique=True,nullable=False)
email = db.Column(db.String(120),unique=True,nullable=False)
image_file = db.Column(db.String(20),nullable=False,default='default.jpg')
password = db.Column(db.String(60),nullable=False)
#education
education = db.relationship('education',backref='edu',lazy=True)
#experience
experience = db.relationship('experience',backref='exp',lazy=True)
#projects
projects = db.relationship('projects',backref='pro',lazy=True)
#userdetails
userdetails = db.relationship('userdetails',backref='details',lazy=True)
#skills
skills = db.relationship('skills',backref='skill',lazy=True)
#achievements
achievements = db.relationship('achievements',backref='ach',lazy=True)
def __retr__(self):
return "User {} Email {} Image {}".format(self.username,self.email,self.image_file)
def add_to_database(self):
db.session.add(self)
db.session.commit()
def delete_from_database(self):
db.session.delete(self)
db.session.commit()
@classmethod
def find_by_username(cls,username):
return cls.query.filter_by(username=username).first()
@classmethod
def find_by_email(cls,email):
return cls.query.filter_by(email=email).first()
def reset_token(self,expires_sec=1800):
s = serializer(app.config['SECRET_KEY'],expires_sec)
return s.dumps({"user_id": self.id}).decode("utf-8")
@staticmethod
def verify_token(token):
s = serializer(app.config['SECRET_KEY'])
try:
user_id = s.loads(token)['user_id']
except:
return None
return UserModel.query.get(user_id)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant