Ejemplo n.º 1
0
    def update(self):
        errors = self.validate()

        if errors:
            return {'errors': errors, 'title': self.title}

        tag_with_same_title = Tag.find_by_title(self.title)

        if tag_with_same_title is not None:
            if tag_with_same_title.uid == self.uid:
                return {
                    'warnings': ["The title is unchanged."],
                    'title': self.title
                }
            else:
                return {
                    'errors': ["A tag with that title already exists."],
                    'title': self.title
                }

        if Tag.find_by_id(self.uid) is None:
            return {'errors': ["Tag doesn't exist."], 'title': self.title}

        db.execute_update(
            """
            UPDATE Tag SET title = %(title)s 
            WHERE id = %(id)s;
            """, {
                'id': self.uid,
                'title': self.title
            })

        # Success.
        return {}
Ejemplo n.º 2
0
    def update(self):
        errors = []

        if Account.find_by_id(self.uid) is None:
            errors.append("Account doesn't exist.")

        # Remove whitespace.
        self.display_name = self.display_name.strip()

        if not (len(self.display_name) <= 50):
            errors.append("Display name must be shorter than 50 characters.")

        if errors:
            return {'errors': errors, 'display_name': self.display_name}

        db.execute_update(
            """
            UPDATE Account SET display_name = %(display_name)s 
            WHERE id = %(id)s;
            """, {
                'id':
                self.uid,
                'display_name':
                None if not self.display_name else self.display_name
            })

        # Success.
        return {}
Ejemplo n.º 3
0
    def create(author_id: int,
               thread_id: int,
               content: str,
               created: datetime = None):
        # Remove whitespace.
        content = content.strip()

        errors = Response.validate(content)

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

        if created is None:
            created = db.get_timestamp()

        db.execute_update(
            """
            INSERT INTO Response (author_id, thread_id, content, created)
            VALUES (%(author_id)s, %(thread_id)s, %(content)s, %(created)s);
            """, {
                'author_id': author_id,
                'thread_id': thread_id,
                'content': content,
                'created': created
            })

        # Success.
        return {}
Ejemplo n.º 4
0
    def create(author_id: int, title: str, content: str, tag_ids: List[int]):
        # Remove whitespace.
        title = title.strip()
        content = content.strip()

        errors = Thread.validate(title, tag_ids)

        # Convert tag_ids to integers.
        try:
            tag_ids = list(int(tag_id) for tag_id in tag_ids)
        except:
            errors.append("Invalid formatting on tags.")

        response_errors = Response.validate(content)
        errors.extend(response_errors)

        if errors:
            return {
                'errors': errors,
                'title': title,
                'content': content,
                'tag_ids': tag_ids
            }

        result = db.execute_update(
            """
            INSERT INTO Thread (author_id, title, created)
            VALUES (%(author_id)s, %(title)s, NOW() AT TIME ZONE 'UTC')
            RETURNING id, created;
            """, {
                'author_id': author_id,
                'title': title
            })

        if not ('id' in result and 'created' in result):
            return {
                'errors': ["Something went wrong."],
                'title': title,
                'content': content,
                'tag_ids': tag_ids
            }

        thread_id = result['id']
        created = result['created']

        Response.create(author_id, thread_id, content, created)

        for tag_id in tag_ids:
            db.execute_update(
                """
                INSERT INTO ThreadTag (tag_id, thread_id)
                VALUES (%(tag_id)s, %(thread_id)s);
                """, {
                    'tag_id': tag_id,
                    'thread_id': thread_id
                })

        # Success.
        return {'thread_id': thread_id}
Ejemplo n.º 5
0
    def delete(self):
        if Tag.find_by_id(self.uid) is None:
            return {'errors': ["Tag doesn't exist."]}

        db.execute_update(
            """
            DELETE FROM Tag 
            WHERE id = %(id)s;
            """, {'id': self.uid})

        # Success.
        return {}
Ejemplo n.º 6
0
    def create(username: str, password: str, repeated_password: str):
        # Remove whitespace and lowercase.
        username = username.strip().lower()

        errors = Account.validate(username, password)

        if Account.username_exists(username):
            errors.append("That username is already taken.")

        if password != repeated_password:
            errors.append("Password entries don't match.")

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

        hashed_bytes = pw.generate_hash(password)
        password_hash = pw.decode_string(hashed_bytes)

        result = db.execute_update(
            """
            INSERT INTO Account (username, password, admin)
            VALUES (%(username)s, %(password_hash)s, FALSE)
            RETURNING id;
            """, {
                'username': username,
                'password_hash': password_hash
            })

        if 'id' not in result:
            return {'errors': ["Something went wrong."], 'username': username}

        account = Account(result['id'], username)

        # Success.
        return {'user': account}
Ejemplo n.º 7
0
    def create(self):
        errors = self.validate()

        if errors:
            return {'errors': errors, 'title': self.title}

        if Tag.find_by_title(self.title) is not None:
            return {
                'errors': ["A tag with that title already exists."],
                'title': self.title
            }

        db.execute_update(
            """
            INSERT INTO Tag (title) VALUES (%(title)s);
            """, {'title': self.title})

        # Success.
        return {}
Ejemplo n.º 8
0
    def update(uid: int, title: str, tag_ids: List[int]):
        # Remove whitespace.
        title = title.strip()

        errors = Thread.validate(title, tag_ids)

        # Convert tag_ids to integers.
        try:
            tag_ids = list(int(tag_id) for tag_id in tag_ids)
        except:
            errors.append("Invalid formatting on tags.")

        if errors:
            return {'errors': errors, 'title': title, 'tag_ids': tag_ids}

        db.execute_update(
            """
            UPDATE Thread SET title = %(title)s
            WHERE id = %(id)s;
            """, {
                'id': uid,
                'title': title
            })

        existing_tags = Tag.find_by_thread_id(uid)
        existing_tag_ids = list(
            t.uid for t in existing_tags) if existing_tags else []

        for tag_id in tag_ids:
            if tag_id not in existing_tag_ids:
                db.execute_update(
                    """
                    INSERT INTO ThreadTag (tag_id, thread_id)
                    VALUES (%(tag_id)s, %(thread_id)s);
                    """, {
                        'tag_id': tag_id,
                        'thread_id': uid
                    })

        for existing_tag_id in existing_tag_ids:
            if existing_tag_id not in tag_ids:
                db.execute_update(
                    """
                    DELETE FROM ThreadTag
                    WHERE tag_id = %(tag_id)s
                    AND thread_id = %(thread_id)s;
                    """, {
                        'tag_id': existing_tag_id,
                        'thread_id': uid
                    })

        # Success.
        return {}
Ejemplo n.º 9
0
 def delete(uid: int):
     db.execute_update(
         """
         DELETE FROM Thread
         WHERE id = %(id)s;
         """, {'id': uid})