Ejemplo n.º 1
0
    def _create_markups_from_this_post(self):
        connection = Connection()
        cursor = connection.start_database_connection()

        for word in self.text.split(' '):
            if '@' in word:
                user_name = re.sub('[^0-9a-zA-Z_]+', '', word)

                cursor.execute("select * from perfil where nome_usuario=%s",
                               [user_name])

                user_exist = True if cursor.fetchall() else False

                if user_exist:
                    cursor.execute(
                        "select * from marcacao_postagem where nome_perfil=%s and id_postagem=%s",
                        (user_name, self.post_id))

                    markup_already_exist = True if cursor.fetchall() else False

                    if not markup_already_exist:
                        cursor.execute(
                            "insert into marcacao_postagem(nome_perfil, id_postagem) values (%s, %s)",
                            (user_name, self.post_id))

                        connection._connection.commit()
                        Notification.create_instance(
                            user_name,
                            id_postmarkup_perfil=user_name,
                            id_postmarkup_post=self.post_id)

        connection.close_database_connection()
Ejemplo n.º 2
0
    def delete_instance(cls, post_id):
        connection = Connection()
        cursor = connection.start_database_connection()

        cursor.execute("delete from postagem where id=%s", [post_id])

        connection.close_database_connection()
    def delete_instance(cls, commentary_id):
        connection = Connection()
        cursor = connection.start_database_connection()

        cursor.execute("delete from comentario where id=%s", [commentary_id])

        connection.close_database_connection()
Ejemplo n.º 4
0
    def create_instance(cls, blocker_user_name, blocked_user_name):
        connection = Connection()
        cursor = connection.start_database_connection()

        cursor.execute(
            "insert into bloqueio(nome_bloqueador, nome_bloqueado) values (%s, %s)",
            (blocker_user_name, blocked_user_name))

        entity_instance = Block([blocker_user_name, blocked_user_name])

        cursor.execute(
            "delete from comentario where id_autor=%s and comentario.id_postagem in" \
            " (select id from postagem where postagem.id_autor=%s)",
            (blocker_user_name, blocked_user_name))

        cursor.execute(
            "delete from comentario where id_autor=%s and comentario.id_postagem in" \
            " (select id from postagem where postagem.id_autor=%s)",
            (blocked_user_name, blocker_user_name))

        cursor.execute(
            "delete from seguimento where nome_seguidor=%s and nome_seguido=%s",
            (blocker_user_name, blocked_user_name))

        cursor.execute(
            "delete from seguimento where nome_seguidor=%s and nome_seguido=%s",
            (blocked_user_name, blocker_user_name))

        connection.close_database_connection()

        return entity_instance
Ejemplo n.º 5
0
    def delete_instance(cls, follower, followed):
        connection = Connection()
        cursor = connection.start_database_connection()

        cursor.execute(
            "delete from seguimento where nome_seguidor=%s and nome_seguido=%s",
            [follower.user_name, followed.user_name])

        connection.close_database_connection()
Ejemplo n.º 6
0
    def delete_instance(cls, blocker_user_name, blocked_user_name):
        connection = Connection()
        cursor = connection.start_database_connection()

        cursor.execute(
            "delete from bloqueio where nome_bloqueador=%s and nome_bloqueado=%s",
            (blocker_user_name, blocked_user_name))

        connection.close_database_connection()
Ejemplo n.º 7
0
    def topic_exist(cls, name):
        connection = Connection()
        cursor = connection.start_database_connection()

        cursor.execute("select * from topico where nome=%s", [name])

        topic_exist = True if cursor.fetchall() else False

        connection.close_database_connection()

        return topic_exist
Ejemplo n.º 8
0
    def set_privacy(self, privacy: bool):
        connection = Connection()
        cursor = connection.start_database_connection()

        self.privacy = privacy

        cursor.execute(
            "update perfil set privacidade=%s where nome_usuario=%s",
            (privacy, self.user_name))

        connection.close_database_connection()
Ejemplo n.º 9
0
    def follow_exist(cls, follower, followed):
        connection = Connection()
        cursor = connection.start_database_connection()

        cursor.execute(
            "select * from seguimento where nome_seguidor=%s and nome_seguido=%s",
            [follower.user_name, followed.user_name])

        follow_exist = True if cursor.fetchall() else False

        connection.close_database_connection()

        return follow_exist
Ejemplo n.º 10
0
    def block_exist(cls, blocker_user_name, blocked_user_name):
        connection = Connection()
        cursor = connection.start_database_connection()

        cursor.execute(
            "select * from bloqueio where nome_bloqueador=%s and nome_bloqueado=%s",
            (blocker_user_name, blocked_user_name))

        block_exist = True if cursor.fetchall() else False

        connection.close_database_connection()

        return block_exist
Ejemplo n.º 11
0
    def create_instance(cls, name):
        connection = Connection()
        cursor = connection.start_database_connection()

        if cls.topic_exist(name):
            raise RepeatedPrimaryKeyException()

        cursor.execute("insert into topico(nome) values (%s)", [name])

        connection.close_database_connection()

        entity_instance = Topic()
        entity_instance.name = name

        return entity_instance
    def get_commentary_instance(cls, commentary_id):
        connection = Connection()
        cursor = connection.start_database_connection()

        cursor.execute("select * from comentario where id=%s", [commentary_id])

        commentary_as_list = cursor.fetchall()[0]

        if commentary_as_list:
            commentary = Commentary(commentary_as_list)
        else:
            commentary = None

        connection.close_database_connection()

        return commentary
Ejemplo n.º 13
0
    def get_post_instance(cls, post_id):
        connection = Connection()
        cursor = connection.start_database_connection()

        cursor.execute("select * from postagem where id=%s", [post_id])

        post_as_list = cursor.fetchall()[0]

        if post_as_list:
            post = Post(post_as_list)
        else:
            post = None

        connection.close_database_connection()

        return post
Ejemplo n.º 14
0
    def set_confirmation(self, confirmation: bool):
        connection = Connection()
        cursor = connection.start_database_connection()

        self.confirmation = confirmation

        cursor.execute(
            "update seguimento set confirmacao=%s where nome_seguidor=%s and nome_seguido=%s",
            (confirmation, self.follower_user_name, self.followed_user_name))

        connection.close_database_connection()

        Notification.create_instance(
            self.follower_user_name,
            notification_type='follow confirmation',
            id_follow_follower=self.follower_user_name,
            id_follow_followed=self.followed_user_name)
Ejemplo n.º 15
0
    def get_topic_instance(cls, name):
        connection = Connection()
        cursor = connection.start_database_connection()

        cursor.execute("select * from topico where nome=%s", [name])

        topics_as_lists = cursor.fetchall()

        if topics_as_lists:
            topic = Topic()
            topic.name = name
        else:
            topic = None

        connection.close_database_connection()

        return topic
Ejemplo n.º 16
0
    def get_user_notifications(self):
        connection = Connection()
        cursor = connection.start_database_connection()

        cursor.execute(
            "select * from notificacao where id_perfil=%s order by data desc",
            [self.user_name])

        notifications_as_lists = cursor.fetchall()
        notifications = []

        for notification_as_list in notifications_as_lists:
            notifications.append(Notification(notification_as_list))

        connection.close_database_connection()

        return notifications
Ejemplo n.º 17
0
    def search_users(cls, search_key, order_by=None):
        connection = Connection()
        cursor = connection.start_database_connection()

        order_by_followers = isinstance(
            order_by, str) and order_by.lower() == 'followers number'

        try:
            search_key = str(search_key)
            search_parameter = '%' + search_key + '%'

            cursor.execute(
                "select * from perfil where nome_usuario like %s or nome_real like %s or biografia like %s",
                [search_parameter, search_parameter, search_parameter])
            users_as_list = cursor.fetchall()
            all_users_informations = []

            for user_as_list in users_as_list:
                user_information = {'object': User(user_as_list)}
                all_users_informations.append(user_information)

            connection.close_database_connection()

            if order_by_followers:
                for user_information in all_users_informations:
                    user_information['followers number'] = len(
                        user_information['object'].get_user_followers())

                all_users_informations = sorted(
                    all_users_informations,
                    key=lambda k: k['followers number'],
                    reverse=True)

            return [
                user_information['object']
                for user_information in all_users_informations
            ]

        except ValueError:
            connection.close_database_connection()
            raise ValueError

        except Exception as e:
            connection.close_database_connection()
            traceback.print_exc()
            raise Exception
Ejemplo n.º 18
0
    def get_user_instance(cls, user_name):
        connection = Connection()
        cursor = connection.start_database_connection()

        cursor.execute("select * from perfil where nome_usuario=%s",
                       [user_name])

        user_as_list = cursor.fetchall()[0]

        if user_as_list:
            user = User(user_as_list)
        else:
            user = None

        connection.close_database_connection()

        return user
Ejemplo n.º 19
0
    def get_follow_instance(cls, follower_id, followed_id):
        connection = Connection()
        cursor = connection.start_database_connection()

        cursor.execute(
            "select * from seguimento where nome_seguidor=%s and nome_seguido=%s",
            (follower_id, followed_id))

        follow_as_list = cursor.fetchall()[0]

        if follow_as_list:
            follow_relationship = Follow(follow_as_list)
        else:
            follow_relationship = None

        connection.close_database_connection()

        return follow_relationship
Ejemplo n.º 20
0
    def get_block_instance(cls, blocker_user_name, blocked_user_name):
        connection = Connection()
        cursor = connection.start_database_connection()

        cursor.execute(
            "select * from bloqueio where nome_bloqueador=%s and nome_bloqueado=%s",
            (blocker_user_name, blocked_user_name))

        block_as_list = cursor.fetchall()[0]

        if block_as_list:
            block_relationship = Block(block_as_list)
        else:
            block_relationship = None

        connection.close_database_connection()

        return block_relationship
Ejemplo n.º 21
0
    def _create_topics_from_this_post(self):
        connection = Connection()
        cursor = connection.start_database_connection()

        for word in self.text.split(' '):
            if '#' in word:
                word = re.sub('[^0-9a-zA-Z]+', '', word)

                date = datetime.datetime.now()

                if not Topic.topic_exist(word):
                    cursor.execute("insert into topico(nome) values (%s)",
                                   [word])

                cursor.execute(
                    "insert into topico_postagem(data, id_topico, id_postagem) values (%s, %s, %s)",
                    (date, word, self.post_id))

        connection.close_database_connection()
    def get_commentary_from_topic(cls, topic_name):
        connection = Connection()
        cursor = connection.start_database_connection()

        cursor.execute(
            "select comentario.id, comentario.id_postagem, comentario.id_autor, comentario.data, comentario.texto from topico_comentario" \
            " inner join comentario on topico_comentario.id_comentario = comentario.id" \
            " where topico_comentario.id_topico=%s order by topico_comentario.data desc",
            [topic_name])

        commentaries_as_lists = cursor.fetchall()
        commentaries = []

        for commentary_as_list in commentaries_as_lists:
            commentaries.append(Commentary(commentary_as_list))

        connection.close_database_connection()

        return commentaries
Ejemplo n.º 23
0
    def get_posts_from_topic(cls, topic_name):
        connection = Connection()
        cursor = connection.start_database_connection()

        cursor.execute(
            "select postagem.id, postagem.data, postagem.texto, postagem.foto, postagem.id_autor from topico_postagem" \
            " left join postagem on topico_postagem.id_postagem = postagem.id" \
            " where topico_postagem.id_topico=%s order by topico_postagem.data desc",
            [topic_name])

        posts_as_lists = cursor.fetchall()
        posts = []

        for post_as_list in posts_as_lists:
            posts.append(Post(post_as_list))

        connection.close_database_connection()

        return posts.copy()
    def create_instance(cls, post, text, user_author):
        connection = Connection()
        cursor = connection.start_database_connection()

        date = datetime.datetime.now()

        cursor.execute(
            "insert into comentario(id_postagem, id_autor, data, texto) values (%s, %s, %s, %s) returning id",
            (post.post_id, user_author.user_name, date, text))

        commentary_id = cursor.fetchone()[0]

        connection.close_database_connection()

        entity_instance = Commentary(
            [commentary_id, post.post_id, user_author.user_name, date, text])
        entity_instance._create_topics_from_this_commentary()
        entity_instance._create_markups_from_this_commentary()

        return entity_instance
Ejemplo n.º 25
0
    def get_user_followeds(self):
        connection = Connection()
        cursor = connection.start_database_connection()

        user_name = str(self.user_name)

        cursor.execute(
            "select nome_usuario, nome_real, senha, biografia, privacidade" \
            " from seguimento inner join perfil on seguimento.nome_seguido = perfil.nome_usuario " \
            " where nome_seguidor=%s and confirmacao=%s",
            (user_name, True))

        followeds_as_list = cursor.fetchall()
        followeds = []

        for followed_as_list in followeds_as_list:
            followeds.append(User(followed_as_list))

        connection.close_database_connection()

        return followeds
Ejemplo n.º 26
0
    def get_user_posts(self):
        connection = Connection()
        cursor = connection.start_database_connection()

        cursor.execute(
            "select postagem.id, data, texto, foto, id_autor from postagem" \
            " left join seguimento on postagem.id_autor = seguimento.nome_seguido" \
            " where id_autor=%s or id_autor=seguimento.nome_seguido" \
            " order by data desc",
            [self.user_name]
        )

        posts_as_lists = cursor.fetchall()
        posts = []

        for post_as_list in posts_as_lists:
            posts.append(Post(post_as_list))

        connection.close_database_connection()

        return posts
Ejemplo n.º 27
0
    def create_instance(cls, text, image_path, user_author):
        connection = Connection()
        cursor = connection.start_database_connection()

        date = datetime.datetime.now()
        image_bytes = cls._image_bytes_from_path(image_path)

        cursor.execute(
            "insert into postagem(data, texto, foto, id_autor) values (%s, %s, %s, %s) returning id",
            (date, text, image_bytes, user_author.user_name))

        post_id = cursor.fetchone()[0]

        connection.close_database_connection()

        entity_instance = Post(
            [post_id, date, text, image_bytes, user_author.user_name])
        entity_instance._create_topics_from_this_post()
        entity_instance._create_markups_from_this_post()

        return entity_instance
Ejemplo n.º 28
0
    def get_post_commentaries(self, logged_user_name=None):
        connection = Connection()
        cursor = connection.start_database_connection()

        cursor.execute(
            "select * from comentario where id_postagem=%s" \
            " order by data desc",
            [self.post_id]
        )

        commentaries_as_lists = cursor.fetchall()
        commentaries = []

        for commentary_as_list in commentaries_as_lists:
            commentary = Commentary(commentary_as_list)

            if not (logged_user_name) or not (Block.block_exist(
                    logged_user_name, commentary.author_id)):
                commentaries.append(commentary)

        connection.close_database_connection()

        return commentaries
Ejemplo n.º 29
0
    def create_instance(cls, follower, followed):
        connection = Connection()
        cursor = connection.start_database_connection()

        if cls.follow_exist(follower, followed):
            raise RepeatedPrimaryKeyException()

        confirmation = True if not followed.privacy else False

        cursor.execute(
            "insert into seguimento(confirmacao, nome_seguidor, nome_seguido) values (%s, %s, %s)",
            (confirmation, follower.user_name, followed.user_name))

        connection.close_database_connection()

        Notification.create_instance(followed.user_name,
                                     id_follow_follower=follower.user_name,
                                     id_follow_followed=followed.user_name)

        entity_instance = Follow(
            [confirmation, follower.user_name, followed.user_name])

        return entity_instance