Esempio n. 1
0
    def post(self):
        error_message = None
        try:
            # Processing input arguments
            parser.add_argument('name', type=str, required=True, trim=True)
            parsed_args = parser.parse_args()

            # Creating team instance
            team = Team(
                created=datetime.datetime.utcnow(),
                name=parsed_args['name'],
                params=parsed_args['params'],
                meta=parsed_args['meta']
            )

            # Saving to DB
            session.add(team)
            session.commit()
        except IntegrityError:
            error_message = 'Faulty or a duplicate record'
        except ValueError as e:
            error_message = str(e)
        except Exception as e:
            print(str(e))
            error_message = 'Bad request'
        finally:
            if error_message:
                session.rollback()
                abort(400, message='Unable to create team record: ' + error_message)
            else:
                return team, 201
Esempio n. 2
0
    def put(self, id):
        team = session.query(Team).filter(Team.id == id).first()
        if not team:
            abort(404, message="Team {} does not exist".format(id))
        error_message = None
        try:
            # Processing input arguments
            parser.add_argument('name', type=str, required=True, trim=True)
            parsed_args = parser.parse_args()

            # Updating team instance
            team.name = parsed_args['name'] if parsed_args['name'] else team.name

            # Saving to DB
            session.merge(team)
            session.commit()
        except IntegrityError:
            error_message = 'Faulty or a duplicate record'
        except ValueError as e:
            error_message = str(e)
        except Exception as e:
            print(str(e))
            error_message = 'Bad request'
        finally:
            if error_message:
                session.rollback()
                abort(400, message='Unable to create team record: ' + error_message)
            else:
                return team, 202
Esempio n. 3
0
 def put(self, id):
     try:
         property_contoller = PropertyController(id=id, landlord_id=self.landlord_id)
         parser.add_argument('name', type=str, trim=True, store_missing=False)
         parser.add_argument('address', type=str, trim=True, store_missing=False)
         parser.add_argument('city', type=str, trim=True, store_missing=False)
         parser.add_argument('state', type=str, trim=True, store_missing=False)
         parser.add_argument('postal_zip', type=str, trim=True, store_missing=False)
         parsed_args = parser.parse_args()
         property_contoller.update(**parsed_args)
         return property_contoller.property, 202
     except Exception:
         abort(400, message="Error editing Property")
Esempio n. 4
0
 def post(self):
     try:
         # Processing input arguments
         parser.add_argument('name', type=str, trim=True)
         parser.add_argument('address', type=str, required=True, trim=True)
         parser.add_argument('city', type=str, required=True, trim=True)
         parser.add_argument('state', type=str, trim=True)
         parser.add_argument('postal_zip', type=str, trim=True)
         parsed_args = parser.parse_args()
         parsed_args['landlord_id'] = self.landlord_id
         property = PropertyController().create(**parsed_args)
         return property, 201
     except Exception as e:
         abort(400, message=str(e))
Esempio n. 5
0
 def post(self):
     try:
         # Processing input arguments
         parser.add_argument('first_name', type=str, required=True, trim=True)
         parser.add_argument('last_name', type=str, required=True, trim=True)
         parser.add_argument('email', type=str, required=True, trim=True)
         parser.add_argument('username', type=str, required=False, trim=True)
         parser.add_argument('password', type=str, required=False)
         parsed_args = parser.parse_args()
         parsed_args['type'] = self.type
         # Creating user object
         user = UserController().create(**parsed_args)
         return user, 201
     except Exception as e:
         abort(400, message=str(e))
Esempio n. 6
0
    def get(self):
        args = parser.parse_args()
        start, end = getTimeWindow(args)
        limit = args['limit'] if args['limit'] else 30
        query = db.session.query(Track, db.func.count(Track.id).label('n'))\
            .select_from(History).join(Track, History.track_id == Track.id, isouter=True)\
            .filter(db.func.date(History.played_at) > start, db.func.date(History.played_at) < end)\
            .group_by(Track.id)\
            .order_by(db.desc('n')).limit(limit).all()

        payload = [{
            'track': track,
            'times_played': count
        } for track, count in query]
        return payload
Esempio n. 7
0
 def put(self):
     try:
         user_controller = UserController(id=self.id)
     except Exception:
         abort(404, message="User not loaded")
         return None
     try:
         # Processing input arguments
         parser.add_argument('first_name', type=str, trim=True, store_missing=False)
         parser.add_argument('last_name', type=str, trim=True, store_missing=False)
         parser.add_argument('email', type=str, trim=True, store_missing=False)
         parser.add_argument('username', type=str, trim=True, store_missing=False)
         parser.add_argument('password', type=str, store_missing=False)
         parsed_args = parser.parse_args()
         # Updating user instance
         user_controller.update(**parsed_args)
         return user_controller.user, 202
     except Exception as e:
         abort(400, message=str(e))
 def get(self):
     args = parser.parse_args()
     start, end = getTimeWindow(args)
     limit = args['limit'] if args['limit'] else 100
     query = db.session.query(History.played_at, Track)\
         .join(Track, History.track_id == Track.id, isouter=True)\
         .filter(db.func.date(History.played_at) > start, db.func.date(History.played_at) < end)\
         .order_by(db.desc(History.played_at)).paginate(page=args['page'], per_page=args['per_page'])
     # print(query.page)
     # print(query.pages)
     history_list = [{
         'played_at': played_at,
         'track': track
     } for played_at, track in query.items]
     payload = {
         'page': query.page,
         'pages': query.pages,
         'history': history_list
     }
     return payload
Esempio n. 9
0
 def get(self):
     args = parser.parse_args()
     start, end = getTimeWindow(args)
     limit = args['limit'] if args['limit'] else 30
     query = db.session.query(Track.album, Track.id, Track.artist)\
         .select_from(History)\
         .join(Track, History.track_id == Track.id, isouter=True)\
         .filter(db.func.date(History.played_at) > start, db.func.date(History.played_at) < end)\
         .order_by(History.played_at).all()
     counts = self.count_album_plays(query)
     counts = [(album, n[1], n[0]) for album, n in sorted(
         counts.items(), key=lambda item: item[1][0], reverse=True)[:limit]]
     # print(counts)
     payload = [{
         'album': album.split('---')[0],
         'artist': album.split('---')[1],
         'track_id': track_id,
         'times_played': n
     } for album, track_id, n in counts]
     return payload
Esempio n. 10
0
    def put(self, id):
        user = session.query(User).filter(User.id == id).first()
        if not user:
            abort(404, message="User {} does not exist".format(id))
        error_message = None
        try:
            # Processing input arguments
            parser.add_argument('name', type=str, trim=True)
            parser.add_argument('email', type=str, trim=True)
            parser.add_argument('password', type=str)
            parser.add_argument('user_id', type=str)
            parser.add_argument('pin', type=str)
            parser.add_argument('profile_picture', type=str)
            parsed_args = parser.parse_args()

            # Updating user instance
            user.name = parsed_args['name'] if parsed_args['name'] else user.name
            user.email = parsed_args['email'] if parsed_args['email'] else user.email
            user.password = parsed_args['password'] if parsed_args['password'] else user.password
            user.user_id = parsed_args['user_id'] if parsed_args['user_id'] else user.user_id
            user.pin = parsed_args['pin'] if parsed_args['pin'] else user.pin
            user.profile_picture = parsed_args['profile_picture'] if parsed_args[
                'profile_picture'] else user.profile_picture

            # Saving to DB
            session.merge(user)
            session.commit()
        except IntegrityError:
            error_message = 'Faulty or a duplicate record'
        except ValueError as e:
            error_message = str(e)
        except Exception as e:
            print(str(e))
            error_message = 'Bad request'
        finally:
            if error_message:
                session.rollback()
                abort(400, message='Unable to create user record: ' + error_message)
            else:
                return user, 202
Esempio n. 11
0
    def post(self):
        error_message = None
        try:
            # Processing input arguments
            parser.add_argument('name', type=str, required=True, trim=True)
            parser.add_argument('email', type=str, required=True, trim=True)
            parser.add_argument('password', type=str)
            parser.add_argument('user_id', type=str, required=True)
            parser.add_argument('pin', type=str, required=True)
            parser.add_argument('profile_picture', type=str)
            parsed_args = parser.parse_args()

            # Creating user instance
            user = User(
                created=datetime.datetime.utcnow(),
                name=parsed_args['name'],
                email=parsed_args['email'],
                user_id=parsed_args['user_id'],
                pin=parsed_args['pin'],
                profile_picture=parsed_args['profile_picture'],
            )

            # Saving to DB
            session.add(user)
            session.commit()
        except IntegrityError:
            error_message = 'Faulty or a duplicate record'
        except ValueError as e:
            error_message = str(e)
        except Exception as e:
            print(str(e))
            error_message = 'Bad request'
        finally:
            if error_message:
                session.rollback()
                abort(400, message='Unable to create user record: ' + error_message)
            else:
                return user, 201