def create(identity_provider, external_id, name, email, profile_pic, country): with getcursor() as cur: cur.execute( "INSERT INTO users (identity_provider, external_id, name, email, profile_pic, country, last_login, created)" " VALUES (%s, %s, %s, %s, %s, %s, current_timestamp, current_timestamp)" " RETURNING id", ( identity_provider, external_id, name, email, profile_pic, country, )) user_id = cur.fetchone()[0] user = User(user_id, identity_provider, external_id, name, email, profile_pic, country, generate_csrf()) Album.create(user, "Unsorted") return user
def on_post(self, req, resp, user): user = req.context['user'] name = req.get_param('name') public = bool(req.get_param('public')) print(name, public, user) if name: user = req.context['user'] album = Album.create(name=name, public=public, user=user) resp.status = falcon.HTTP_201 resp.body = json.dumps(album.json(), default=str) else: resp.status = falcon.HTTP_500 resp.boyd = json.dumps({"Error": "Error creating the album"})
def create_album(artist_id, request, base_url): if not verify_data(request=request): return jsonify({'message': 'input inválido'}), 400 if len(artist_id) == 0: return jsonify({'message': 'input inválido'}), 400 possible_artist = Artist.query.filter_by(id=artist_id).first() if possible_artist is None: return jsonify({'message': 'artista no existe'}), 422 artist = possible_artist json = request.get_json(force=True) if json.get('name') is None or json.get('genre') is None: return jsonify({'message': 'input inválido'}), 400 album_name = json['name'] album_id = b64encode(f'{album_name}:{artist.id}'.encode()).decode('utf-8') album_id = truncate_id(album_id) possible_album = Album.query.filter_by(id=album_id).first() if possible_album is None: album = Album.create(album_id=album_id, artist_id=artist_id, name=json['name'], genre=json['genre'], base_url=base_url) return jsonify(album.json()), 201 return jsonify(possible_album.json()), 409
def post_api_albums(): try: album = Album.create(current_user, request.json['name']) except UniqueViolation: return "Album already exists", 400 return json.dumps(album.__dict__)