Example #1
0
 def __init__(self, user1_id, user2_id):
     select_sql = "SELECT id FROM chats WHERE user1_id = %s AND user2_id = %s"
     user_pair = (user1_id, user2_id)
     response = db.get_row(select_sql, values=user_pair) or \
          db.get_row(select_sql, values=reversed(user_pair))
     if response is None:
         insert_sql = "INSERT INTO chats (user1_id, user2_id) VALUE (%s, %s)"
         db.query(insert_sql, values=user_pair, commit=True)
         response = db.get_row(select_sql, values=user_pair)
     self.id, = response
     self.sender_id = user1_id
     self.recipient_id = user2_id
Example #2
0
 def from_email(cls, email):
     user = db.get_row("SELECT id FROM users WHERE email = %s",
                       values=(email, ))
     if user is None:
         return None
     user_id, = user
     return cls(user_id)
Example #3
0
 def __init__(self, user_id=None, *, extended=True):
     if user_id is None:
         raise ValueError("No user with this id")
     sql = "SELECT * FROM `users` WHERE id = %s"
     user = db.get_row(sql, values=(user_id, ), cur_class=DictCursor)
     assert user is not None, "No user with this id"
     self.id = user['id']
     self.login = user['login']
     self.name = user['name']
     self.surname = user['surname']
     self.email = user['email']
     self._password = user['password']
     self.token = user['token']
     self._confirmed = user['confirmed']
     self.gender = user['gender']
     self.preferences = user['preferences']
     self.biography = user['biography']
     self._avatar = user['avatar']
     self._photos = user['photos']
     self.age = user['age']
     self._online = user['online']
     if user['last_login']:
         local_dt = user['last_login'].replace(tzinfo=pytz.utc).astimezone(
             self.tz)
         self.tz.normalize(local_dt)
         self.last_login = local_dt.strftime("%H:%M %d.%m.%Y")
     self.city = user['city']
     self.fame = self.get_fame_rating(self.id) if extended else None
     self.tags = self.get_tags(self.id) if extended else None
Example #4
0
    def __init__(self, notification_id):
        sql = "SELECT * FROM notifications WHERE id = %s"
        notification = db.get_row(sql, values=(notification_id, ))
        assert notification is not None, "No notifications with this id"
        self.id, self.user_id, self.message, self.link, self.date_created = notification

        local_dt = self.date_created.replace(tzinfo=pytz.utc).astimezone(
            self.tz)
        self.tz.normalize(local_dt)
        self.date_created = local_dt.strftime(self.timestamp_format)
Example #5
0
 def login(cls, data):
     sql = "SELECT id, password, confirmed FROM `users` WHERE login = %s"
     response = db.get_row(sql, values=(data['login'], ))
     assert response is not None, "Cannot find user with this login"
     user_id, password, confirmed = response
     assert check_password_hash(password, data['pass']), "Wrong password"
     assert confirmed, "You should confirm your E-mail first!"
     user = cls(user_id)
     user.online = 1
     return user
Example #6
0
    def send(cls, sender, recipient, notification_type):
        if sender.id in recipient.blocked:
            return
        link = url_for(
            'chat_page' if notification_type == 'message' else 'profile',
            user_id=sender.id)
        notification_message = cls.notifications[notification_type].format(
            sender.login)

        sql = "INSERT INTO notifications (user_id, message, link) VALUES (%s, %s, %s)"
        db.query(sql,
                 values=(recipient.id, notification_message, link),
                 commit=True)
        sql = "SELECT id FROM notifications WHERE user_id = %s ORDER BY date_created DESC"
        notification_id = db.get_row(sql, values=(recipient.id, ))
        notification = cls(notification_id)
        from .sockets import connected
        if recipient.id in connected:
            data = {'notification': notification.to_json()}
            socketio.emit('notification received',
                          data,
                          room=connected[recipient.id])
Example #7
0
 def email_exist(email):
     sql = "SELECT COUNT(email) as row_num FROM `users` WHERE email = %s"
     row_num, = db.get_row(sql, values=(email, ))
     return row_num > 0
Example #8
0
 def login_exist(login):
     sql = "SELECT COUNT(login) as row_num FROM `users` WHERE login = %s"
     row_num, = db.get_row(sql, values=(login, ))
     return row_num > 0