async def create_emoji_for_member(self, member: discord.Member) -> discord.Emoji: """ Creates the emoji for given member :param member: member emoji should be created for :return: [discord.Emoji] created Emoji """ # generate name name = member.name + member.discriminator # download avatar profile_picture = await member.avatar_url_as(format="png", size=64).read() # create avatar and yield emoji = await member.guild.create_custom_emoji(name=name, image=profile_picture) # if failed return if not emoji: return None # create entry for database emoji_entry = AvatarEmoji(name=emoji.name, discord_id=emoji.id, user_id=str(member.id), guild_id=str(member.guild.id)) session.add(emoji_entry) session.commit() return emoji
def create_playlist(): #TODO Create method in models.playlist - Attribute Name # Handles a json request if the request is json if (request.is_json): request_data = request.get_json(force=True) if 'name' in request_data and 'description' in request_data: playlist_name = request_data['name'] 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_name = request.form.get('name') playlist_description = request.form.get('description') if playlist_name is None or playlist_description is None: return make_response("There is no name or description in request", 400) user_id = current_user.get_id(token=False) new_playlist = Playlist(name=playlist_name, description=playlist_description, user_id=user_id) session.add(new_playlist) session.commit() return make_response(str(new_playlist.id), 200)
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 db_update_chat(chat_id, step, place_id=None): chat = db_get_chat(chat_id) if chat: chat.step = step if place_id: chat.place_id = place_id else: chat = Chat(id=chat_id, step=step) session.add(chat) session.commit() return chat
def create_new_user(): # Handles a json request if the request is json if (request.is_json): request_data = request.get_json(force=True) if 'email' in request_data and 'password' in request_data and 'confirm_password' in request_data: user_password = request_data['password'] user_email = request_data['email'] user_name = request_data['name'] if (user_password != request_data['confirm_password']): return make_response("Passwords don't match", 400) else: return make_response( "There is no email, password or password confirmation in request", 400) # Else, we assume the request is a form else: user_password = request.form.get('password') user_email = request.form.get('email') user_name = request.form.get("name") if (user_password != request.form.get('confirm_password')): return make_response("Passwords don't match", 400) if (user_email == None or user_password == None): return make_response( "There is no password or password confirmation in request", 400) # Create a random session token user_token = unicode(os.urandom(24).encode('hex')) # Saves new user in the database try: new_user = User(email=user_email, password=user_password, session_token=user_token, name=user_name) # We hash the password new_user.set_password() session.add(new_user) session.commit() # Handles any unexpected exception.... except Exception as e: message = jsonify({"status": "failed", "message": str(e)}) return render_template('register.html', message=message) # Success! message = jsonify({"status": "ok", "message": "user created with success"}) return redirect("/")
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 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)
async def vc_add(self, ctx: Context, voice_id: str): """ Adds a voice channel to managing the temporary text channel :param ctx: context of event. :param voice_id: the id of the voice channel in discord. :return: """ vc = VoiceChannel(discord_id=voice_id, guild_id=ctx.guild.id) session.add(vc) session.commit() embed = discord.Embed( type="rich", title="Success", description="Temporary text channel has been created", color=colors.COLOR_SUCCESS ) await ctx.send(embed=embed, reference=ctx.message)
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 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
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_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 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 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
def create_song(): # Get the name of the uploaded file #TODO !Implement song upload! #TODO Create method in models.playlist - Attribute Name # Handles a json request if the request is json if request.is_json: request_data = request.get_json(force=True) if 'name' in request_data and 'album' in request_data and 'artist' in request_data: song_name = request_data['name'] song_album = request_data['album'] song_artist = request_data['artist'] else: return make_response( "There is no name, album or artist in request", 400) # Else, we assume the request is a form else: file = request.files['file'] file.save( os.path.join(application.config['SONG_FOLDER'], file.filename)) song_name = request.form.get('name') song_path = application.config['SONG_FOLDER'] + "/%s" % (file.filename) song_album = request.form.get('album') song_artist = request.form.get('artist') if song_name is None or song_album is None or song_artist is None: return make_response("There is no name or description in request", 400) user_id = current_user.get_id(token=False) new_song = Song(name=song_name, album=song_album, artist=song_artist, user_id=user_id, path=song_path) session.add(new_song) session.commit() return make_response(str(new_song.id), 200)
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 _clearSongs(playlist): for song in playlist.songs: playlist.songs.remove(song) session.commit()
def db_remove_places(message): db_get_places(message.chat.id, 0).delete() session.commit()