Exemple #1
0
    def authenticate(username: str, password: str):
        # Remove whitespace and lowercase.
        username = username.strip().lower()

        errors = Account.validate(username, password)

        if errors:
            return {'errors': errors, 'username': username}

        rows = db.execute_query(
            """
            SELECT * FROM Account WHERE username = %(username)s;
            """, {'username': username})

        try:
            row = rows.pop(0)
            stored_hash = row['password']

            if pw.matches(password, stored_hash):
                # Successful authentication.
                account = Account(row['id'], row['username'],
                                  row['display_name'], row['admin'])
                return {'user': account}

        except:
            pass

        return {
            'errors': ["Incorrect username or password."],
            'username': username
        }
Exemple #2
0
    def find_by_id(uid: int) -> 'Thread':
        rows = db.execute_query(
            """
            SELECT
                Thread.*,
                Account.username, Account.display_name
            FROM Thread
            INNER JOIN Account ON Account.id = Thread.author_id
            AND Thread.id = %(id)s;
            """, {'id': uid})

        try:
            row = rows.pop(0)
            account = Account(row['author_id'], row['username'],
                              row['display_name'])
            tags = Tag.find_by_thread_id(row['id'])
            responses = Response.find_by_thread_id(row['id'])
            thread = Thread(row['id'],
                            account,
                            tags,
                            row['title'],
                            row['created'],
                            responses=responses)
            return thread

        except:
            return None
Exemple #3
0
    def find_all() -> List['Thread']:
        rows = db.execute_query("""
            SELECT 
                Thread.*, 
                Account.username, Account.display_name, 
                COUNT(Response.id) as response_count,
                MAX(Response.created) as last_active
            FROM Thread
            INNER JOIN Account ON Account.id = Thread.author_id
            LEFT JOIN Response ON Response.thread_id = Thread.id
            GROUP BY Thread.id, Account.username, Account.display_name
            ORDER BY last_active DESC;
            """)

        result = []

        for row in rows:
            account = Account(row['author_id'], row['username'],
                              row['display_name'])
            tags = Tag.find_by_thread_id(row['id'])
            thread = Thread(row['id'], account, tags, row['title'],
                            row['created'], row['last_active'],
                            row['response_count'])
            result.append(thread)

        return result
Exemple #4
0
    def find_by_author_id(author_id: int) -> List['Thread']:
        rows = db.execute_query(
            """
            SELECT
                Thread.id, Thread.title, Thread.created,
                COUNT(Response.id) as response_count,
                MAX(Response.created) as last_active
            FROM Thread
            INNER JOIN Account ON Account.id = Thread.author_id
            AND Thread.author_id = %(author_id)s
            LEFT JOIN Response ON Response.thread_id = Thread.id
            GROUP BY Thread.id
            ORDER BY Thread.created DESC
            LIMIT 5;
            """, {'author_id': author_id})

        result = []

        for row in rows:
            tags = Tag.find_by_thread_id(row['id'])
            thread = Thread(row['id'], None, tags, row['title'],
                            row['created'], row['last_active'],
                            row['response_count'])
            result.append(thread)

        return result
Exemple #5
0
    def username_exists(username: str) -> bool:
        rows = db.execute_query(
            """
            SELECT id FROM Account WHERE username = %(username)s;
            """, {'username': username})

        try:
            row = rows.pop(0)
            return 'id' in row

        except:
            return False
Exemple #6
0
    def author_for_thread(uid: int) -> int:
        rows = db.execute_query(
            """
            SELECT author_id FROM Thread
            WHERE id = %(id)s;
            """, {'id': uid})

        try:
            row = rows.pop(0)
            return row['author_id']
        except:
            return None
Exemple #7
0
    def find_all() -> List['Tag']:
        rows = db.execute_query("""
            SELECT Tag.*, COUNT(ThreadTag.tag_id) as count FROM Tag
            LEFT JOIN ThreadTag ON ThreadTag.tag_id = Tag.id
            GROUP BY Tag.id
            ORDER BY count DESC, title;
            """)

        result = []

        for row in rows:
            tag = Tag(row['id'], row['title'], row['count'])
            result.append(tag)

        return result
Exemple #8
0
    def find_by_thread_id(thread_id: int) -> List['Tag']:
        rows = db.execute_query(
            """
            SELECT Tag.* FROM Tag
            INNER JOIN ThreadTag ON ThreadTag.thread_id = %(thread_id)s
            AND ThreadTag.tag_id = Tag.id
            ORDER BY title;
            """, {'thread_id': thread_id})

        result = []

        for row in rows:
            tag = Tag(row['id'], row['title'])
            result.append(tag)

        return result
Exemple #9
0
    def find_by_title(title: str) -> 'Tag':
        rows = db.execute_query(
            """
            SELECT *, (
                SELECT COUNT(*) FROM ThreadTag
                WHERE ThreadTag.tag_id = Tag.id
            ) AS count FROM Tag
            WHERE title = %(title)s;
            """, {'title': title})

        try:
            row = rows.pop(0)
            tag = Tag(row['id'], row['title'], row['count'])
            return tag

        except:
            return None
Exemple #10
0
    def find_by_id(uid: int) -> 'Account':
        rows = db.execute_query(
            """
            SELECT id, username, display_name, admin FROM Account WHERE id = %(id)s;
            """, {'id': uid})

        try:
            row = rows.pop(0)

            # Deferred in order to prevent circular imports.
            from src.models.thread import Thread

            threads = Thread.find_by_author_id(row['id'])
            account = Account(row['id'], row['username'], row['display_name'],
                              row['admin'], threads)
            return account

        except:
            return None
Exemple #11
0
    def find_by_id(uid: int) -> 'Tag':
        rows = db.execute_query(
            """
            SELECT *, (
                SELECT COUNT(*) FROM ThreadTag
                WHERE ThreadTag.tag_id = %(id)s
            ) AS count FROM Tag
            WHERE id = %(id)s;
            """, {'id': uid})

        try:
            row = rows.pop(0)

            # Deferred in order to prevent circular imports.
            from src.models.thread import Thread

            threads = Thread.find_by_tag_id(row['id'])
            tag = Tag(row['id'], row['title'], row['count'], threads)
            return tag

        except:
            return None
Exemple #12
0
    def find_by_thread_id(thread_id: int) -> List['Response']:
        rows = db.execute_query(
            """
            SELECT 
                Response.*,
                Account.username, Account.display_name
            FROM Response
            INNER JOIN Account ON Account.id = Response.author_id
            AND Response.thread_id = %(thread_id)s
            ORDER BY created ASC;
            """, {'thread_id': thread_id})

        result = []

        for row in rows:
            account = Account(row['author_id'], row['username'],
                              row['display_name'])
            response = Response(row['id'], account, row['content'],
                                row['created'])
            result.append(response)

        return result