コード例 #1
0
 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()
コード例 #2
0
    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
コード例 #3
0
 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'
コード例 #4
0
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()
コード例 #5
0
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
        }
コード例 #6
0
 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()
コード例 #7
0
 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()