def create_user(self, password): """Create user in db DONE""" password_hash = generate_password_hash(password) user_id = uuid.uuid4() query = "INSERT INTO " \ "users (id, username,email,password_hash,time_created)" \ "VALUES('%s','%s', '%s', '%s', '%s')" % ( user_id, self.username, self.email, password_hash, self.time_created) cur.execute(query) conn.commit()
def insert_question(self): """insert question to db""" self.question_id = uuid.uuid4() # question titles should not be the same if self.get_question_title(self.title) is None: query = "INSERT INTO questions (id, title, body, user_id, time_created, preferred_answer)" \ "VALUES ('%s', '%s', '%s', '%s', '%s', '%s')" % (self.question_id, self.title, self.body, self.user_id, self.time_created, self.preferred_answer) cur.execute(query) conn.commit() return True else: return False
def insert_answer(self): """insert answer to db""" question = Question.get_question(self.question_id) if question: self.answer_id = uuid.uuid4() if self.get_answer_from_body(self.body, self.question_id) is None: query = "INSERT INTO answers(id, body, user_id, question_id, preferred, time_created)" \ "VALUES ('%s', '%s', '%s', '%s', '%s', '%s')" % (self.answer_id, self.body, self.user_id, self.question_id, self.preferred, self.time_created) cur.execute(query) conn.commit() return 'success' else: return 'duplicated' else: return 'question not found'
def insert_token(token): """change the status of a request""" query = "INSERT INTO tokens (id, expired_tokens) VALUES ('%s','%s');" % ( uuid.uuid4(), token) cur.execute(query) conn.commit()
import jwt import psycopg2 from flask import jsonify, request from validate_email import validate_email from werkzeug.security import check_password_hash from project.config import Config from project.database import conn from project.models.models import User from . import users try: cur = conn.cursor() cur.execute("ROLLBACK") conn.commit() except Exception as e: print('connection exception ', e) cur = conn.cursor() cur.execute("ROLLBACK") def auth_encode(user_id): """Generate auth token""" try: payload = { 'exp': datetime.now() + timedelta(hours=1), 'iat': datetime.now(), 'sub': user_id }
def update_answer(self, body, preferred): cur.execute("UPDATE answers SET body='%s', preferred='%s' WHERE id='%s';" % (body, preferred, self.answer_id)) conn.commit()
def update_question(self): cur.execute("UPDATE questions SET title=%s, body=%s, user_id=%s, time_created=%s, preferred_answer=%s" "WHERE id=%s;", (self.title, self.body, self.user_id, self.time_created, self.preferred_answer, self.question_id)) conn.commit()