Exemplo n.º 1
0
 def delete(self, name):
     if name != g.user.username:
         return "Url header mismatch", BAD_REQUEST
     doomed_user = g.user
     shutil.rmtree('dynamic/u/%s' % name)
     session.delete(doomed_user)
     session.commit()
     return "Success", SUCCESS
Exemplo n.º 2
0
 def put(self, name):
     if name != g.user.username:
         return "Url header mismatch", BAD_REQUEST
     for filename in self.FILES:
         if filename in request.form:
             g.user.set_dynamic_file(filename, request.form[filename])
             if filename == 'entry':
                 arena_room.emit_entry_update(g.user,
                                              request.form[filename])
     session.commit()
Exemplo n.º 3
0
 def post(self, pk=None):
     form = self.form_class(self.request.arguments)
     if form.validate():
         self.object = self.get_object(pk)
         self.bind_data(self.object, form.data)
         session.add(self.object)
         session.commit()
         self.redirect(self.get_success_url())
     else:
         print("invalid form")
     self.get(form=form, pk=pk)
Exemplo n.º 4
0
 def post(self, pk=None):
     form = self.form_class(self.request.arguments)
     if form.validate():
         self.object = self.get_object(pk)
         self.bind_data(self.object, form.data)
         session.add(self.object)
         session.commit()
         self.redirect(self.get_success_url())
     else:
         print("invalid form")
     self.get(form=form, pk=pk)
Exemplo n.º 5
0
 def put(self, id):  # Updates votes only, set images in Player.update()
     if g.user.arena_id != id:
         return 'You are not in that Arena!', UNAUTHORIZED
     arena = session.query(Arena).filter_by(id=id).first()
     votes = request.get_json()
     print(votes)
     for user in arena.players:
         if user.username in votes:
             g.user.toggle_vote(user)
             session.commit()
             arena_room.emit_votes_changed(user)
Exemplo n.º 6
0
    def post(self, room_id):
        try:
            data_dict = json_decode(self.request.body)
        except Exception as e:
            self.set_status(400)
            self.json({"details": "Некорректные данные"})
        form = UserReservationForm(data=data_dict)
        if not form.validate():
            self.set_status(400)
            self.json({"details": "Некорректные данные", "error": form.errors})
            return
        print(form.data)
        try:
            room = session.query(Room).filter(Room.id == int(room_id)).one()
        except:
            self.set_status(404)
            self.finish("not found")
            return
        try:
            start_date = datetime.datetime.strptime(
                form.data['start_date_time'], "%Y-%m-%d")
            end_date = datetime.datetime.strptime(form.data["end_date_time"],
                                                  "%Y-%m-%d")
        except:
            self.set_status(400)
            self.finish({"details": "Некорректная дата"})
            return
        else:

            s_date, e_date = room.is_busy(start_date, end_date)

            if s_date is None:
                data = form.data.copy()
                data.update({
                    "start_date_time": start_date,
                    "end_date_time": end_date,
                    "room_id": int(room_id),
                    "price": room.price()
                })
                reservation = Reservation(**data)
                session.add(reservation)
                try:
                    session.commit()
                except:
                    session.rollback()
                self.json({"details": "Номер свободен"})
                return
            self.set_status(400)
            self.json({"details": "Номер занят"})
Exemplo n.º 7
0
 def post(self, name):  #Create user
     if not request.authorization:
         return "No credentials sent", BAD_REQUEST
     name = request.authorization.username
     password = request.authorization.password
     if not self._valid_password(password):
         return "Password needs to be longer than 5 characters", BAD_REQUEST
     if not self._valid_username(name):
         return "Username invalid", BAD_REQUEST
     if session.query(exists().where(User.username == name)).scalar():
         return "Username %s in use" % name, CONFLICT
     u = User(username=name)
     u.hash_password(password)
     session.add(u)
     session.commit()
     return self.get(name)
Exemplo n.º 8
0
    def post(self, room_id):
        try:
            data_dict = json_decode(self.request.body)
        except Exception as e:
            self.set_status(400)
            self.json({"details": "Некорректные данные"})
        form = UserReservationForm(data=data_dict)
        if not form.validate():
            self.set_status(400)
            self.json({"details": "Некорректные данные", "error": form.errors})
            return
        print(form.data)
        try:
            room = session.query(Room).filter(Room.id == int(room_id)).one()
        except:
            self.set_status(404)
            self.finish("not found")
            return
        try:
            start_date = datetime.datetime.strptime(form.data['start_date_time'], "%Y-%m-%d")
            end_date = datetime.datetime.strptime(form.data["end_date_time"], "%Y-%m-%d")
        except:
            self.set_status(400)
            self.finish({"details": "Некорректная дата"})
            return
        else:

            s_date, e_date = room.is_busy(start_date, end_date)

            if s_date is None:
                data = form.data.copy()
                data.update({
                    "start_date_time": start_date,
                    "end_date_time": end_date,
                    "room_id": int(room_id),
                    "price": room.price()
                })
                reservation = Reservation(**data)
                session.add(reservation)
                try:
                    session.commit()
                except:
                    session.rollback()
                self.json({"details": "Номер свободен"})
                return
            self.set_status(400)
            self.json({"details": "Номер занят"})
Exemplo n.º 9
0
    def post(self):
        user = g.user

        if not user.arena_id:  # in battle
            min_range, max_range = 50, 1000
            interval = 10
            start = time.time()
            progress = 0
            while progress < 1:
                closest = session.query(Arena).filter(
                    Arena.available).order_by(Arena.difference(user)).first()
                if closest is None:
                    progress = 1
                    continue
                # See if skill is a match, with larger tolerance over a minute of searching
                progress = (time.time() - start) / interval
                # skill_difference < time_scalar * min_to_max_skill
                if closest.difference(
                        user) < progress * (max_range - min_range) + min_range:
                    user.join_arena(closest)
                    session.commit()
                    break
            else:
                arena = user.create_arena()
                options = request.get_json() or dict()
                if 'prompt' in options:
                    arena.prompt = options['prompt']
                if 'max_players' in options \
                        and options['max_players'] \
                        and self.MIN_PLAYERS <= options['max_players'] <= self.MAX_PLAYERS :
                    arena.max_players = options['max_players']
                if 'timeout_delta' in options \
                        and options['timeout_delta'] \
                        and options['timeout_delta'] >= self.MIN_TIME:
                    arena.timeout_delta = options['timeout_delta']
                session.add(arena)
                session.commit()  #commit inits defaults
                user.join_arena(arena)
                session.commit()
        timeout = None
        if hasattr(user.arena.timeout, 'isoformat'):
            timeout = user.arena.timeout.isoformat()

        arena_room.emit_player_join(user)
        return {
            'id': user.arena_id,
            'start': user.arena.closed,
            'votes': user.votes_pouch,
            'timeout': timeout
        }, SUCCESS
Exemplo n.º 10
0
def get_question_information(rows):
    # Questions
    post_id = 0
    title = ''
    view_count = 0
    answer_count = 0
    favorite_count = 0
    accepted_answer_id = 0

    # Answers
    answer_id = 0
    parent_id = 0
    code = ''

    # Common
    score = 0
    comment_count = 0
    owner_user_id = 0
    last_editor_user_id = 0
    last_editor_display_name = ''
    last_edit_date = ''  #datetime
    last_activity_date = ''  #datetime
    creation_date = ''  #datetime
    community_owned_date = ''  #datetime
    body = ''
    tags = ''

    #Database objects
    new_question = None
    new_answer = None
    #  Iterate over the entire metadata dump file to produce the
    #  results for the data base. NOTE: the tags are not capital
    #  specific
    for row in rows:
        #Check for post to exist based on post type id
        if row.get('posttypeid'):
            post_type_id = int(row.get('posttypeid'))

            #Check for tags for the given post
            if row.get('tags'):
                tags = row.get('tags')
            else:
                continue

            print tags
            #Make sure tags match either C or C++
            if (tags.find('<c>') and tags.find('<c++>')) != -1:
                #Generic types that should be pulled for either question or answer
                if row.get('score'): score = int(row.get('score'))
                if row.get('commentcount'):
                    comment_count = int(row.get('commentcount'))
                if row.get('owneruserid'):
                    owner_user_id = int(row.get('owneruserid'))
                if row.get('lasteditoruserid'):
                    last_editor_user_id = row.get('lasteditoruserid')
                if row.get('lasteditordisplayname'):
                    last_editor_display_name = row.get('lasteditordisplayname')
                if row.get('lasteditdate'):
                    last_edit_date = datetime.strptime(row.get('lasteditdate'),
                                                       "%Y-%m-%dT%H:%M:%S.%f")
                if row.get('lastactivitydate'):
                    last_activity_date = datetime.strptime(
                        row.get('lasta:tivitydate'), "%Y-%m-%dT%H:%M:%S.%f")
                if row.get('creationdate'):
                    creation_date = datetime.strptime(row.get('creationdate'),
                                                      "%Y-%m-%dT%H:%M:%S.%f")
                if row.get('communityowneddate'):
                    community_owned_date = datetime.strptime(
                        row.get('communityowneddate'), "%Y-%m-%dT%H:%M:%S.%f")
                if row.get('body'): body = row.get('body')

                #The post type id is a question
                if post_type_id == 1:
                    print 'it is a question'
                    if row.get('id'): post_id = int(row.get('id'))
                    if row.get('title'): title = row.get('title')
                    if row.get('viewcount'):
                        view_count = int(row.get('viewcount'))
                    if row.get('answercount'):
                        answer_count = int(row.get('answercount'))
                    if row.get('favoritecount'):
                        favorite_count = int(row.get('favoritecount'))
                    if row.get('acceptedanswerid'):
                        accepted_answer_id = int(row.get('acceptedanswerid'))
                    print title
                    print creation_date
                    new_question = Questions(post_id = post_id, creation_date = creation_date, title = title, tags = tags, body = body, score = score, view_count = view_count, \
                                        answer_count = answer_count, comment_count = comment_count, favorite_count = favorite_count, owner_user_id = owner_user_id, \
                                        accepted_answer_id = accepted_answer_id, last_editor_user_id = last_editor_user_id, last_editor_display_name = last_editor_display_name, \
                                        last_edit_date = last_edit_date, last_activity_date = last_activity_date, community_owned_date = community_owned_date)
                #This type of post is an answer
                elif post_type_id == 2:
                    if row.get('answerid'):
                        answer_id = int(row.get('answerid'))
                    if row.get('parentid'):
                        parent_id = int(row.get('parentid'))
                    if row.get('code'): code = int(row.get('code'))

                    new_answer = Answers(answer_id, parent_id = parent_id, owner_user_id = owner_user_id, tags = tags, code = code, body = body, score = score, \
                                         comment_count = comment_count, last_editor_user_id = last_editor_user_id, last_editor_display_name = last_editor_display_name, \
                                         last_edit_date = last_edit_date, last_activity_date = last_activity_date, creation_date = creation_date, community_owned_date = community_owned_date)
                else:
                    print 'Not a question or answer skipping'
                    continue
                #END Post Type CHECK
                try:
                    if new_question:
                        print 'there is a question to add'
                        session.add(new_question)
                    elif new_answer:
                        print 'there is a new answer'
                        session.add(new_answer)
                    else:
                        print 'there is nothing'

                except:
                    print 'something went wrong with db session'
            else:
                print 'Not the tag we are looking for'
                #END TAG TYPE CHECK
        else:
            print 'There is no post type id'
            continue

    #Commit all the values we found
    session.commit()
Exemplo n.º 11
0
 def delete(self):
     user = g.user
     arena_room.emit_player_leave(user)
     user.leave_arena()
     session.commit()
     return "Success", SUCCESS