Example #1
0
    def test_user_unjoin(self):
        chat = self.chats[0]
        self.add_user_to_chat(chat.id, self.user.id)

        ChatUser.join_switcher(self.user.id, chat.id)

        with self.assertRaises(NoResultFound):
            db.query(ChatUser).filter(ChatUser.user_id==self.user.id, ChatUser.chat_id==chat.id).one()
Example #2
0
def get_games_list():
    try:
        print("get game list")
        print("game count - {0}".format(len(dbi.query(Game).filter(Game.gameStatus != Game.gameStatusEnd).all())))
        return responded_ok(
            {"games": [game.toList() for game in dbi.query(Game).filter(Game.gameStatus != Game.gameStatusEnd).all()]}
        )
    except:
        raise NotGames()
Example #3
0
    def get(self, chat_id):
        #if user is not joined to chat - redirct to chat main page
        if not ChatUser.has_access(self.user_id, chat_id):
            self.redirect("/chat")

        chat = db.query(Chat).filter(Chat.id == chat_id).one()
        messages = db.query(Message, User).join(User).filter(
            Message.chat_id == chat_id).order_by(Message.id.desc()).limit(50).all()

        self.render_to_response("chat/chat.html", chat=chat, messages=messages, form=MessageForm(chat_id=chat_id))
Example #4
0
    def has_access(cls, user_id, chat_id):
        '''
        Check if user has access to some chat

        :param user_id: int - id of user to check
        :param chat_id: int - id of chat to check
        :return: bool - True if user have access or False if not have
        '''
        try:
            db.query(cls.id).filter(cls.user_id == user_id, cls.chat_id == chat_id).one()
        except NoResultFound:
            return False

        return True
Example #5
0
def logout_user(sid):
    try:
        user = dbi.query(User).filter_by(sid=sid).one()
        user.sid = None
        dbi.commit()
        return responded_ok()
    except sqlalchemy.orm.exc.NoResultFound:
        raise BadUserSid()
Example #6
0
    def post(self, form):

        search_value = u'%{0}%'.format(self.get_argument("name"))
        chats = db.query(Chat).filter(Chat.name.like(search_value)).all()

        user_in_chats = ChatUser.get_user_chats(self.user_id)

        self.render_to_response("chat/chats_list.html", chats=chats, user_in_chats=user_in_chats)
Example #7
0
    def test_user_join(self):
        chat = self.chats[0]

        ChatUser.join_switcher(self.user.id, chat.id)

        get_row = db.query(ChatUser).filter(ChatUser.user_id==self.user.id, ChatUser.chat_id==chat.id).one()

        self.assertTrue(get_row)
        self.delete_objects(get_row)
Example #8
0
    def get_user_chats(cls, user_id):
        '''
        Get id of chats that user join

        :param user_id: int - id of user to get chats
        :return: list - list of chats id
        '''
        user_in_chat = db.query(cls.chat_id).filter(cls.user_id == user_id).all()

        return [item.chat_id for item in user_in_chat]
Example #9
0
    def post(self, form):
        '''Create new Chat object, populate it, save and render new chats list'''
        chat = Chat()
        form.populate_obj(chat)
        chat.user_id = self.user_id
        db.add(chat)
        db.commit()

        #re-render block with chats
        user_in_chats = ChatUser.get_user_chats(self.user_id)
        chats = db.query(Chat).all()
        self.render_to_response("chat/chats_list.html", chats=chats, user_in_chats=user_in_chats)
Example #10
0
def register_user(username, password):
    check_username(username)
    check_password(password)

    if config.DEBUG:
        print("sid before {0}".format(dbi.lastSid))

    if dbi.query(User).filter_by(name=username).count() != 0:
        raise UsernameTaken()
    newUser = User(username, password)
    dbi.add(newUser)
    return responded_ok()
Example #11
0
def login_user(username, password):
    check_username(username)
    check_password(password)

    try:  # потом могет из дб кидать
        user = dbi.query(User).filter_by(name=username).one()
        if user.password != password:
            raise BadUsernameOrPassword()

        user.setSid()
        user.setUserId()
        return responded_ok({"sid": user.sid, "userId": user.userId})
    except sqlalchemy.orm.exc.NoResultFound:
        raise BadUsernameOrPassword()
Example #12
0
    def isset_user(name, password):
        '''
        Check exist or not user in DB

        :param password: str - user name
        :param string: str - user password
        : return: dict - {} if not isset or dict with user_name and user_id keys
        '''
        md5_password = Md5Handler().process(password)

        try:
            user = db.query(User).filter_by(name=name, password=md5_password).one()
        except NoResultFound:
            return {}
        else:
            return {'user_name': name, 'user_id': user.id}
Example #13
0
    def join_switcher(cls, user_id, chat_id):
        '''
        Switch user joining in some chat. If user is already joined - unjoin him, if not - join

        :param user_id: int - id of user to switch
        :param chat_id: int - id of chat to switch in
        :return: None
        '''
        try:
            row = db.query(ChatUser).filter(cls.chat_id==chat_id, cls.user_id==user_id).one()
        except NoResultFound:
            row = cls(user_id=user_id, chat_id=chat_id)
            db.add(row)
        else:
            db.delete(row)

        db.commit()
Example #14
0
 def validate_name(self, field):
     if db.query(User).filter(User.name==field.data).first():
         raise ValidationError('Username already exists')
Example #15
0
def get_maps_list():
    try:
        return responded_ok({"maps": [map.to_json() for map in dbi.query(Map).all()]})
    except:
        raise NotMaps()
Example #16
0
    def get(self, chat_id):
        ChatUser.join_switcher(self.user_id, chat_id)

        user_in_chats = ChatUser.get_user_chats(self.user_id)
        chats = db.query(Chat).all()
        self.render_to_response("chat/chats_list.html", chats=chats, user_in_chats=user_in_chats)
Example #17
0
    def get(self):
        user_in_chats = ChatUser.get_user_chats(self.user_id)
        chats = db.query(Chat).all()

        self.render_to_response("chat/index.html", chat_add_form=ChatAddForm(),
                                chat_search_form=ChatSearchForm(), chats=chats, user_in_chats=user_in_chats)