def put_library(): name = request.form.get("name") address = request.form.get("address") telephone = request.form.get("telephone") email = request.form.get("email") if not name and not address and not telephone and not email: return make_response(jsonify({"code": 403, "msg": "Cannot put library. Missing mandatory fields."}), 403) library_id = request.form.get("id") if not library_id: p = models.Library(name=name, address=address, telephone=telephone, email=email) else: p = models.Library(id=library_id, name=name, address=address, telephone=telephone, email=email) models.db.session.add(p) try: models.db.session.commit() except sqlalchemy.exc.SQLAlchemyError as e: error = "Cannot put library. " print(app.config.get("DEBUG")) if app.config.get("DEBUG"): error += str(e) return make_response(jsonify({"code": 404, "msg": error}), 404) return jsonify({"code": 200, "msg": "Successfully added library."})
def get(self): google_user = users.get_current_user() if not google_user: self.redirect(users.create_login_url(self.request.uri)) else: app_user = utils.get_current_user() if google_user and not app_user: app_user = utils.init_new_user(google_user) share_hash = utils.url_to_share_key(self.request.uri) q = db.GqlQuery( "SELECT * FROM Playlist WHERE share_hash = :share_hash", share_hash=share_hash) playlist = q.get() if playlist: if not app_user.has_playlist(playlist): last_position = app_user.last_lib_position() library_item = models.Library(user=app_user, position=last_position + 1, playlist=playlist, is_owner=False) library_item.put() flash = "add_shared_playlist" else: flash = "playlist_already_in_lib" else: flash = "playlist_not_found" self.redirect("/?flash=" + flash)
def post(self): #Create new playlist current_user = utils.get_current_user() playlist = models.Playlist( name=self.request.get("name"), owner=current_user, smart=utils.convert_javascript_bool_to_python( self.request.get("smart")), share_hash=utils.generate_share_hash()) if playlist.smart: utils.parse_smart_filters(playlist, self.request) playlist.put() library_item = models.Library(user=current_user, playlist=playlist, is_owner=True, position=int( self.request.get("position"))) library_item.put() self.response.out.write(library_item.serialize())
def buildObjects(db): u1 = models.User(name="Meghana", email="*****@*****.**") u1.set_password('admin') a1 = models.Author(name='John Williams') b1 = models.Book(title="Once upon a time", description="Great book") b2 = models.Book(title="Second upon a time", description="Another classic") l = models.Library() b1.author = a1 b2.author = a1 l.user = u1 assoc = models.AssociationBookLibrary(library=l) assoc.book = b2 db.session.add(a1) db.session.add(b1) db.session.add(b2) db.session.add(u1) db.session.add(l) db.session.commit()
def init_new_user(google_user): nickname = make_pretty_nickname_from_email(google_user.email()) app_user = models.User(google_user=google_user, nickname=nickname) app_user.put() default_playlists = [] p = models.Playlist(name="Hot Tracks", smart=True, owner=app_user, order="hotness", share_hash=generate_share_hash()) default_playlists.append(p) p = models.Playlist(name="Indie", smart=True, owner=app_user, genres="indie", order="latest", share_hash=generate_share_hash()) default_playlists.append(p) p = models.Playlist(name="Deep House", smart=True, owner=app_user, genres="deep house", order="latest", share_hash=generate_share_hash()) default_playlists.append(p) p = models.Playlist(name="Rock", smart=True, owner=app_user, genres="rock", order="latest", share_hash=generate_share_hash()) default_playlists.append(p) p = models.Playlist(name="Techno", smart=True, owner=app_user, genres="techno", order="latest", share_hash=generate_share_hash()) default_playlists.append(p) p = models.Playlist(name="Spoken Word", smart=True, owner=app_user, genres="spoken+word, spokenword", order="latest", share_hash=generate_share_hash()) default_playlists.append(p) p = models.Playlist(name="Dubstep", smart=True, owner=app_user, genres="dubstep, dub+step", order="latest", share_hash=generate_share_hash()) default_playlists.append(p) db.put(default_playlists) library = [] i = 0 for p in default_playlists: library.append( models.Library(user=app_user, playlist=p, is_owner=True, position=i)) i += 1 db.put(library) return app_user