def update_playlist(playlist): #TODO Update method in models.playlis associated with user, it should allow for adding songs / remove, these are part of the resource as a method and should not be in delete user_id = current_user.get_id(token=False) song = request.args.get('add_song') if song: return add_song_to_playlist(song, playlist) else: song = request.args.get('delete_song') if song: return del_song_in_playlist(playlist, song) if (request.is_json): request_data = request.get_json(force=True) if 'name' in request_data: playlist_name = request_data['name'] if 'description' in request_data: playlist_description = request_data['description'] else: return make_response("There is no name or description in request", 400) # Else, we assume the request is a form else: _playlist = session.query(Playlist).filter( Playlist.id == playlist, Playlist.user_id == current_user.get_id(token=False)).first() if _playlist: new_value = request.form['value'] item = request.form['name'] user_obj = session.query(Playlist).filter_by(id=playlist).update( {item: new_value}) return make_response("%s updated" % (item), 200) else: return make_response("Unauthorized", 401)
def db_get_places(chat_id, how_much): how_much = int(how_much) if how_much > 0: return session.query(Place).filter_by(chat_id=chat_id)\ .order_by(Place.id).limit(how_much) elif how_much < 0: how_much = how_much * -1 return session.query(Place).filter_by(chat_id=chat_id)\ .order_by(Place.id.desc()).limit(how_much) else: return session.query(Place).filter_by(chat_id=chat_id)
async def emoji_add(self, ctx: Context): """ Command to create or update an emoji :param ctx: :return: """ # list of emojis for reaction emojis = [] # action for every mentioned member async with ctx.typing(): for member in ctx.message.mentions: profile_picture_entry = session.query(AvatarEmoji)\ .filter(AvatarEmoji.user_id == member.id and AvatarEmoji.guild_id == member.guild.id)\ .first() if profile_picture_entry: await self.delete_emoji_by_member(member) emoji = await self.create_emoji_for_member(member) emojis.append(emoji) # create success message msg = discord.Embed( type="rich", title="Sucess", color=colors.COLOR_SUCCESS, description="Emoji has been successfully created") success_msg = await ctx.send(embed=msg) for e in emojis: if e: await success_msg.add_reaction(e)
def get_songs(): query = session.query(Song).order_by(Song.name) songs = [row.as_dict() for row in query.all()] if request.is_json: return jsonify(songs) else: user = current_user.as_dict() return render_template("songs.html", user=user, songs=songs)
def del_song_in_playlist(playlist_id, song_id): try: playlist = session.query(Playlist).filter( Playlist.id == playlist_id, Playlist.user_id == current_user.get_id(token=False)).first() song = session.query(Song).filter(Song.id == song_id).first() except Exception as e: print e return make_response("Unknown error", 500) if playlist is None: return make_response("Not your playlist", 401) elif song is None: return make_response("Song not in playlist", 400) if song in playlist.songs: playlist.songs.remove(song) session.commit() return make_response("Song removed from playlist with success", 200) else: return make_response("Song does not belong to plalyist", 400)
async def delete_text_channel(self, voice_channel): """ Deletes the temporary text channel to the voice channel :param voice_channel: :return: """ vc = session.query(VoiceChannel).filter(VoiceChannel.discord_id == voice_channel.id).first() text_channel = voice_channel.guild.get_channel(int(vc.text_id)) await text_channel.delete() vc.text_id = "" session.commit() return
def add_song_to_playlist(song, playlist): user_id = current_user.get_id(token=False) try: user_song = session.query(Song).filter(Song.id == song, Song.hidden == False).first() if user_song is None: return make_response("Song does not exist", 401) user_playlist = session.query(Playlist).filter( Playlist.id == playlist, Playlist.user_id == user_id).first() if user_playlist is None: return make_response("Not your playlist", 401) except Exception as e: print(e) return make_response("Unknown error", 500) if user_song in user_playlist.songs: return make_response("Song already on playlist", 401) user_playlist.songs.append(user_song) session.commit() return make_response("Song added to playlist", 200)
def update_song(): pk = request.form['pk'] new_value = request.form['value'] item = request.form['name'] if request.is_xhr: if session.query(Song).filter( Song.id == pk, Song.user_id == current_user.get_id(token=False)).first(): session.query(Song).filter_by(id=pk).update({item: new_value}) session.commit() return make_response("Song edited", 200) else: return make_response( "You cannot edit a music that doesn't belong to you", 401) if request.is_json: if _song: return make_response("Song edited", 200) else: return make_response( "You cannot edit a music that doesn't belong to you", 401)
def edit_songs_in_playlist(playlist): _playlist = session.query(Playlist).filter( Playlist.id == playlist, Playlist.user_id == current_user.get_id(token=False)).first() if _playlist: try: _clearSongs(_playlist) songs_ids = ast.literal_eval(request.data) _songs = list() for id in songs_ids: song = session.query(Song).filter_by(id=int(id)).first() if song: _playlist.songs.append(song) session.commit() print _playlist.songs return make_response("Songs updated", 200) except Exception as e: print e session.rollback() else: return make_response("No such playlist", 400)
def get_song(song): _song = session.query(Song).filter( Song.id == song, Song.user_id == current_user.get_id(token=False)).first() if request.is_xhr: if _song: return make_response(render_template("song.html", song=_song), 200) else: return make_response("Unauthorized", 401) if request.is_json: if _song: return make_response(jsonify(_song), 200) else: return make_response("Unauthorized", 401)
def get_playlist(playlist): _playlist = session.query(Playlist).filter( Playlist.id == playlist, Playlist.user_id == current_user.get_id(token=False)).first() if request.is_xhr: if _playlist: return make_response( render_template("playlist.html", playlist=_playlist), 200) else: return make_response("Unauthorized", 401) if request.is_json: if _playlist: return make_response(jsonify(_playlist), 200) else: return make_response("Unauthorized", 401)
async def voice_join(self, voice_channel: discord.VoiceChannel, member: discord.Member): """ Managing when member joins a voice channel :param voice_channel: channel member joined :param member: member joining channel :return: """ vc = session.query(VoiceChannel).filter(VoiceChannel.discord_id == voice_channel.id).first() if not vc: return if not vc.text_id: text_channel = await self.create_text_channel(voice_channel, member) else: text_channel = voice_channel.guild.get_channel(int(vc.text_id)) await self.give_text_channel_permission(member, text_channel)
def get_songs_in_playlist(playlist): try: my_playlist = session.query(Playlist).filter( Playlist.id == playlist).first() except Exception as e: print(e) return make_response("Unknown error", 500) if my_playlist is None: return make_response("Not your playlist", 401) songs = [row.__dict__ for row in my_playlist.songs] for song in songs: del (song['_sa_instance_state']) return jsonify(songs)
def delete_playlist(playlist): #TODO Delete method in playlists song, only the relation should be eliminated, songs are to be stored try: to_delete = session.query(Playlist).filter( Playlist.user_id == current_user.get_id(token=False), Playlist.id == playlist).first() except Exception as e: print e return make_response("Unknown error", 500) if to_delete is None: return make_response("Not your playlist", 401) to_delete.songs = [] session.delete(to_delete) session.commit() return make_response("Playlist deleted with success", 200)
def get_playlists(): #TODO Get method in models.playlis associated with user according to request.args order by Name | Creation Date | Size #Should default to ascending order by name A to Z # To order in sql alchemy: query = session.query(Playlist).filter(Playlist.user_id == current_user.get_id(token = False).ordery_by('Playlist.name')) #TODO Playlists only of user id query = session.query(Playlist).filter( Playlist.user_id == current_user.get_id(token=False)).order_by( Playlist.name) playlists = [row.__dict__ for row in query.all()] if request.is_json: return jsonify(playlists) else: user = current_user.as_dict() return render_template("playlists.html", user=user, playlists=playlists)
def delete_song(song): #TODO Delete method in models.song, only if user in session is the creator # To 'delete' a song, we change the attribute 'hidden' to True try: to_delete = session.query(Song).filter( Song.id == song, Song.user_id == current_user.get_id(token=False)).first() except Exception as e: print(e) return make_response("Unknown error", 500) if to_delete is None: return make_response("Not your song", 401) to_delete.hidden = True session.commit() return make_response("Song deleted with success", 200)
def prepare(self): ip = self.get_argument("ip", None) extension = self.get_argument("extension", False) user_agent = self.request.headers.get('User-Agent') splitted_agent = user_agent.split() model = splitted_agent[0] mac = splitted_agent[1].split(":")[1] if extension: #first check if the extension already exist query = session.query(Phones).filter(Phones.extension==extension) try: phone = query.one() phone.extension = extension phone.mac = mac except NoResultFound: phone = Phones(mac,ip,extension) session.add(phone) session.commit() self.phone = phone
async def voice_leave(self, voice_channel: discord.VoiceChannel, member: discord.Member): """ Managing when member leaves a channel :param voice_channel: channel member joined left :param member: member leaving channel :return: """ vc = session.query(VoiceChannel)\ .filter( VoiceChannel.discord_id == voice_channel.id and VoiceChannel.guild_id == voice_channel.guild.id )\ .first() if not vc: return if not vc.text_id: return if len(voice_channel.members) == 0: await self.delete_text_channel(voice_channel) if len(voice_channel.members) != 0: text_channel = voice_channel.guild.get_channel(int(vc.text_id)) await self.revoke_text_channel_permission(member, text_channel)
async def create_text_channel(self, voice_channel, member) -> discord.TextChannel: """ Creates a temporary text channel :param voice_channel: voice channel text channel should be created for :param member: member giving permission to for voice channel :return: The created text channel """ vc = session.query(VoiceChannel).filter(VoiceChannel.discord_id == voice_channel.id).first() guild = voice_channel.guild permissions = { guild.default_role: discord.PermissionOverwrite(read_messages=False) } text_channel = await voice_channel.guild.create_text_channel( voice_channel.name, postion=voice_channel.position, category=voice_channel.category, overwrites=permissions ) vc.text_id = text_channel.id session.commit() return text_channel
async def delete_emoji_by_member(self, member: discord.Member): """ deletes an Emoji for given member :param member: :return: """ # get emoji from database emoji_entry = session.query(AvatarEmoji)\ .filter(AvatarEmoji.user_id == member.id and AvatarEmoji.guild_id == member.guild.id)\ .first() # get emoji from discord emoji = discord.utils.get(member.guild.emojis, id=int(emoji_entry.discord_id)) if not emoji: return False # delete emoji on discord await emoji.delete() # delete in database session.delete(emoji_entry) session.commit()
def update_user(user): # We get the user sesion_token from the session session_token = current_user.get_id() user_id = current_user.get_id(token=False) # Handles a json request if the request is json if (request.is_json): request_data = request.get_json(force=True) if 'new_password' in request_data and 'password' in request_data and 'confirm_password' in request_data: user_password = request_data['password'] new_password = request_data['new_password'] confirm_password = request_data['confirm_password'] else: return make_response( "There is no new_password, password or password confirmation in request", 400) else: if user_id == user: new_value = request.form['value'] item = request.form['name'] user_obj = session.query(User).filter_by(id=user_id).update( {item: new_value}) return make_response("%s updated" % (item), 200)
def db_update_places(message, bot, place_id=None): if bot and message.from_user.id != bot.get_me().id: if place_id: place = session.query(Place).filter_by(id=place_id).first() if message.photo and message.photo[0]: place.photo_id = message.photo[0].file_id elif message.text: place.comment = message.text elif message.location: place.longitude = message.location.longitude place.latitude = message.location.latitude else: return None elif message.text: place = Place(chat_id=message.chat.id, name=message.text) session.add(place) else: return None else: return None session.commit() return place
async def vc_list(self, ctx: Context): """ Prints a list with all voice channels being managed :param ctx: context of event. :return: """ msg = "" # entries from database vcs_entries = session.query(VoiceChannel)\ .filter(VoiceChannel.guild_id == ctx.guild.id).all() for vc_entry in vcs_entries: # voice channel object by discord vc = ctx.guild.get_channel(int(vc_entry.discord_id)) msg += "**" + vc.name + ": ** \n" msg += "Id: *" + str(vc.id) + "*" msg += "\n\n" embed = discord.Embed( type="rich", title="Voice channels", description=msg, color=colors.COLOR_SUCCESS ) await ctx.send(embed=embed, reference=ctx.message)
async def vc_delete(self, ctx: Context, voice_id: str): """ Command to delete the temporary text channel :param ctx: :param voice_id: discords id of voice channel :return: """ # entry from database vc_entry = session.query(VoiceChannel).filter( VoiceChannel.discord_id == voice_id and VoiceChannel.guild_id == ctx.guild.id ).first() # if text channel exists delete it from discord if vc_entry.text_id: await ctx.guild.get_channel(int(vc_entry.text_id)).delete() session.delete(vc_entry) session.commit() embed = discord.Embed( type="rich", title="Success", description="Temporary text channel has been deleted", color=colors.COLOR_SUCCESS ) await ctx.send(embed=embed, reference=ctx.message)
def delete_user(user): # We get the user sesion_token from the session session_token = current_user.get_id() try: # We search for a user with the specified ID and with the same session_token as the one that made the request to_delete = session.query(User).filter( User.session_token == session_token, User.id == user).first() # Unexpected exception handling... except Exception as e: message = "Error... Try again" if request.is_json: return jsonify({"message": message}) else: return render_template('index.html', message=message, user=current_user.as_dict()) # If to_delete is None, the user does not exist or the user that made the request is not the same that we want to delete if to_delete is None: message = "User does not exist or you are not the user" if request.is_json: return jsonify({"message": message}) else: return render_template('index.html', message=message, user=current_user.as_dict()) # We logout the user from the session logout_user() # We delete the user from the database session.delete(to_delete) # And we commit the change to the db session.commit() message = "user deleted with success" if request.is_json: return jsonify({"message": message}) else: return render_template("login.html", message=message)
def get_all() -> List[UserInterface]: return [user.serializer() for user in session.query(User)]
def get_by_id(user_id: int) -> UserInterface: return session.query(User).filter(user_id == User.id).first().serializer()
def db_get_chat(chat_id): return session.query(Chat).filter_by(id=chat_id).first()
def place_exists(message, place_id): place = session.query(Place).filter_by(id=place_id).first() if place and place.name: return True return False
def load_user(token): user = session.query(User).filter(User.session_token == token).first() return user
def user_exists_email(email): user = session.query(User).filter(User.email == email).first() return user