def new_game(): """Creates a new game entry in the database. Returns: Rendered html template or redirect url """ form = CreateForm() if 'username' not in login_session: return redirect('/login') if request.method == 'POST': if form.validate() is False: flash('All fields are required.', 'warning') name = normalize(form.name.data) game = session.query(Games).filter_by(name=name).first() if game is not None: flash('Game already exists in the database', 'warning') return render_template('new_game.html', form=form) description = normalize(form.description.data) image_path = form.image.data if image_path and allowed_file(image_path.filename): image_file = secure_filename(image_path.filename) image_path.save(os.path.join( app.config['UPLOAD_IMAGES_FOLDER'], image_file )) banner_path = form.banner.data if banner_path and allowed_file(banner_path.filename): banner_file = secure_filename(banner_path.filename) banner_path.save(os.path.join( app.config['UPLOAD_IMAGES_FOLDER'], banner_file )) platform = normalize(form.platform.data) creators = normalize(form.creators.data) release_date = form.release_date.data video_path = normalize(form.youtubeVideoURL.data) category_id = normalize(form.category.data) new_game = Games(name=name, description=description, image_path=normalize(image_path.filename), banner_path=normalize(banner_path.filename), platform=platform, creators=creators, release_date=release_date, video_path=video_path, category_id=category_id, user_id=login_session['user_id']) session.add(new_game) session.commit() flash('New game data for ' + new_game.name + ' created!!', 'success') return redirect(url_for('rest.show_games')) else: return render_template('new_game.html', form=form)
def actualizarFoto(self, documento, content, file): consulta = personas.ConsultaId(documento) if (len(consulta) < 1): return False correo = content['correo'] verificarCorreo = personas.VerificarCorreo(correo) if (verificarCorreo): status = int(1) return status if allowed_file(file.filename): nombres = content['nombres'] apellidos = content['apellidos'] telefono = content['telefono'] edad = content['edad'] filename = secure_filename(file.filename) dia = datetime.datetime.utcnow() salt = bcrypt.gensalt() hash = bcrypt.hashpw(bytes(str(dia), encoding='utf-8'), salt) h = str(hash).split('/') if (len(h) > 2): t = h[1] + h[2] else: t = h[0] filename = h[0] filename = str(t) cloudinary.uploader.upload(file, public_id=filename) url = cloudinary.utils.cloudinary_url(filename) actualizar = personas.actualizarPersona(documento, correo, nombres, apellidos, telefono, edad, url[0]) if (actualizar): return url[0] else: status = int(0) return status
def user_register(): form = RegistrationForm() if request.method == "POST": if form.validate() is False: flash("Invalid", 'warning') return render_template('register.html', form=form) username = form.username.data email = form.email.data password = form.password.data accept_tos = form.accept_tos.data # check if fields are empty if username == "" or password == "" or email == "": flash( "Arguments Missing. Make sure to enter \ username and password to signup.", 'danger') return render_template('register.html', form=form) if (session.query(User).filter_by(name=username).first() is not None) or (session.query(User).filter_by( email=email).first() is not None): flash( "That username or email is \ already taken, please try another", 'warning') return render_template('register.html', form=form) picture = form.picture.data if picture and allowed_file(picture.filename): image_file = secure_filename(picture.filename) picture.save( os.path.join(app.config['UPLOAD_USERIMAGES_FOLDER'], image_file)) login_session['username'] = username login_session['email'] = email login_session['picture'] = normalize(picture.filename) user_id = get_user_id(login_session['email']) if not user_id and accept_tos: user_id = create_user(login_session) user = get_user_info(user_id) user.hash_password(password) session.add(user) session.commit() login_session['user_id'] = user_id flash("You are successfully logged in as %s" % username, 'success') return redirect(url_for('rest.show_categories')) else: return render_template("register.html", form=form)
def editarConFoto(self, id, content, file): anuncio = anuncios.ConsultaId(id) if (len(anuncio) < 1): return False titulo = content["titulo"] verificarNombre = anuncios.consultarAnuncios(titulo) if (verificarNombre): return 'NO' if allowed_file(file.filename): descripcion = content["descripcion"] filename = secure_filename(file.filename) dia = datetime.datetime.utcnow() salt = bcrypt.gensalt() hash = bcrypt.hashpw(bytes(str(dia), encoding='utf-8'), salt) h = str(hash).split('/') if len(h) > 2: t = h[1] + h[2] else: t = h[0] filename = str(t) cloudinary.uploader.upload(file, public_id=filename) url = cloudinary.utils.cloudinary_url(filename) actualizacion = anuncios.actualizacion(id, titulo, descripcion, url[0]) if actualizacion: return actualizacion else: status = int(0) return status
def generar(self, content, file): try: if allowed_file(file.filename): filename = secure_filename(file.filename) titulo = content["titulo"] descripcion = content["descripcion"] dia = datetime.datetime.utcnow() salt = bcrypt.gensalt() hash = bcrypt.hashpw(bytes(str(dia), encoding='utf-8'), salt) h = str(hash).split('/') if len(h) > 2: t = h[1] + h[2] else: t = h[0] filename = str(t) cloudinary.uploader.upload(file, public_id=filename) url = cloudinary.utils.cloudinary_url(filename) consulta = anuncios.consultar() if consulta: for i in consulta: id_bd = i.get('id') + 1 # si no hay ejercicios registrados, este toma el valor de 1 else: id_bd = 1 registrar = anuncios.insert(id_bd, titulo, descripcion, url[0]) return registrar except Exception as error: status = False return status
def edit_game(game_id): """Allows user to edit a game. Args: game_id(int) Returns: Rendered html template or redirect url """ if 'username' not in login_session: return redirect('/login') edited_game = session.query(Games).filter_by(id=game_id).first() if (edited_game is not None and (edited_game.user_id == login_session['user_id'] ) or check_admin( login_session['user_id'])): date_to_edit = edited_game.release_date.date().strftime("%d/%m/%y") form = CreateForm(category=edited_game.category_id, platform=edited_game.platform) if request.method == 'POST': if form.name.data: edited_game.name = normalize(form.name.data) if form.description.data: edited_game.description = normalize(form.description.data) if form.image.data: image_path = form.image.data if image_path and allowed_file(image_path.filename): image_file = secure_filename(image_path.filename) os.remove(os.path.join( app.config['UPLOAD_IMAGES_FOLDER'], edited_game.image_path )) image_path.save(os.path.join( app.config['UPLOAD_IMAGES_FOLDER'], image_file )) edited_game.image_path = normalize(image_path.filename) if form.banner.data: banner_path = form.banner.data if banner_path and allowed_file(banner_path.filename): banner_file = secure_filename(banner_path.filename) os.remove(os.path.join( app.config['UPLOAD_IMAGES_FOLDER'], edited_game.banner_path )) banner_path.save(os.path.join( app.config['UPLOAD_IMAGES_FOLDER'], banner_file )) edited_game.banner_path = normalize(banner_path.filename) if form.youtubeVideoURL.data: edited_game.video_path = normalize(form.youtubeVideoURL.data) if form.category.data: edited_game.category_id = normalize(form.category.data) if form.platform.data: edited_game.platform = normalize(form.platform.data) if form.creators.data: edited_game.creators = normalize(form.creators.data) if form.release_date.data: edited_game.release_date = normalize(form.release_date.data) print(edited_game.name) session.add(edited_game) session.commit() flash('Game data for ' + edited_game.name + ' edited and saved successfully!!', 'success') return redirect(url_for('rest.show_games')) else: return render_template('edit_game.html', form=form, date_to_edit=date_to_edit, game=edited_game) else: response = make_response(json.dumps('You are not authorized \ to perform the operation'), 401) abort(response)