Beispiel #1
0
    def on_message(self, data_json):
        '''
        Runs when socket send some message.
        Validating the data, save data and render new message html template

        :param data_json: form from socket in json
        :raise HTTPError: if user is not joined to chat or form data is not valid
        :return: None
        '''
        data = tornado.escape.json_decode(data_json)

        #if not in chat - raise HTTPError
        if not ChatUser.has_access(self.user_id, data['chat_id']):
            raise tornado.web.HTTPError(400)

        form = MessageForm(self._format_data_to_form(data))

        #if bad data - raise HTTPError
        if not form.validate():
            raise tornado.web.HTTPError(400)

        #adding message to DB
        message = Message()
        form.populate_obj(message)
        message.user_id = self.user_id
        db.add(message)
        db.commit()

        current_user = User(name=self.user_name, id=self.user_id)

        data_to_response = {'id': message.id,
                            'message': self.render_to_response("chat/message.html", return_data=True,
                                                   message=message, user=current_user)}

        self.__class__.send_updates(data_to_response)
Beispiel #2
0
def add_to_db(line):
    line = line.split('?!WTF?!')
    m = Movie()
    m.title = line[0]
    m.year = line[1]
    if m.year.isnumeric():
        db.add(m)
Beispiel #3
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)
Beispiel #4
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()
Beispiel #5
0
def create_game(sid, gameName, mapId, gameDescription=None, playersNum=None):
    dbi.checkSid(sid)

    newGame = Game(gameName, mapId, gameDescription)
    dbi.add(newGame)

    try:
        join_game(sid, newGame.id)
    except (RequestException):
        dbi.rm(newGame)
        raise AlreadyInGame()
    return responded_ok({"gameId": newGame.id})
Beispiel #6
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()
Beispiel #7
0
    def post(self, form):
        #redirect to main if already logined
        if self.user_id:
            self.redirect("/")

        #if have some errors - again render registration form with errors
        if form.errors:
            self.render_to_response("registration.html", form=form)
            return

        #if all are OK - add user
        user = User()
        form.populate_obj(user)
        db.add(user)
        db.commit()

        self.render_to_response("registration_end.html", name=self.get_argument("name"))
Beispiel #8
0
def upload_map(mapName, playersNum, turnsNum, thumbnail=None, picture=None, regions=None):
    newMap = Map(mapName, playersNum, turnsNum, thumbnail, picture)
    dbi.add(newMap)
    return responded_ok({"mapId": newMap.id})
Beispiel #9
0
def send_message(sid, text):
    user = dbi.getUser(sid)
    dbi.add(Chat(sid, text))
    return responded_ok()