def updatecategory(category_id): if not current_user.is_authenticated: return redirect(url_for('main.home')) categories = Categories.query.get_or_404(category_id) create_category = CreateCategory() if request.method == 'POST': # here we check if category is None because we can't validate in the validation method(we fill category from routes) if create_category.validate_on_submit( ) or create_category.newCategory.data != None: categories.name = create_category.newCategory.data categories.rate = create_category.rate.data categories.user_id = current_user.id db.session.commit() next_page = request.args.get('next') log = f'Update {create_category.newCategory.data} from category list on' addLog(log) return redirect(next_page) if next_page else redirect( url_for('main.home')) return render_template('createCategory.html', title='Create', categoryForm=create_category) else: create_category.newCategory.data = categories.name create_category.rate.data = categories.rate log = f'Open {create_category.newCategory.data} to Update it on' addLog(log) return render_template('createCategory.html', title='Update', categoryForm=create_category)
def updateFavoriteThing(thing_id): if not current_user.is_authenticated: return redirect(url_for('main.home')) favoriteThing = Favorites.query.get_or_404(thing_id) # this section of getting categories to list them in the popup categories = Categories.query.filter_by(user_id=current_user.id).all() categoriesList = [] for category in categories: categoriesList.append((category.name, category.name)) form = Create() create_category = CreateCategory() requestForm = request.form metaData = {} for data in requestForm: if '-meta-' in data: typeAndName = data.split( "-meta-") #to get type and name of fileds from request metaData[typeAndName[0]] = { typeAndName[1]: request.form[data] } #store type filed, key and value we store type for viewing in front end if request.method == 'POST': form.category.choices = categoriesList if form.validate_on_submit(): category = Categories.query.filter_by( name=form.category.data).first() favoriteThing.title = form.title.data favoriteThing.description = form.description.data favoriteThing.meta_data = metaData favoriteThing.user_id = current_user.id favoriteThing.updateAt = datetime.utcnow() favoriteThing.category = category.id db.session.commit() next_page = request.args.get('next') log = f'Update {form.title.data} from favorite list on' addLog(log) return redirect(next_page) if next_page else redirect( url_for('main.home')) return render_template('create.html', title='Create', form=form, categoryForm=create_category, categories=categories) else: form.title.data = favoriteThing.title form.description.data = favoriteThing.description form.category.choices = categoriesList form.category.data = favoriteThing.group.name metadata = favoriteThing.meta_data log = f'Open {form.title.data} to Update it on' addLog(log) return render_template('create.html', title='Update', form=form, favoriteThing=favoriteThing, categoryForm=create_category, categories=categories, metadata=metadata)
def deleteFavoriteThing(thing_id): if not current_user.is_authenticated: return redirect(url_for('main.home')) favoriteThing = Favorites.query.get_or_404(thing_id) if favoriteThing.user_id != current_user.id: abort(403) db.session.delete(favoriteThing) db.session.commit() flash('Deleted Successfuly', 'success') log = f'Deleted {favoriteThing.title} from favorite list on' addLog(log) return redirect(url_for('main.home'))
def login(): if current_user.is_authenticated: return redirect(url_for('main.home')) form = LoginForm() if form.validate_on_submit(): user = User.query.filter_by(email=form.email.data).first() if user and bcrypt.check_password_hash(user.password,form.password.data): login_user(user) next_page = request.args.get('next') flash('Login Successful','success') log = 'Logged in on!' addLog(log) return redirect(next_page) if next_page else redirect(url_for('main.home')) flash('Login Unsuccessful. please check email and password','danger') return render_template('login.html',title='Login',form=form)
def deleteCategory(category_id): if not current_user.is_authenticated: return redirect(url_for('main.home')) category = Categories.query.get_or_404(category_id) favorites = Favorites.query.filter_by(category=category.id).all() if len(favorites) != 0: flash('you can\'t Deleted', 'danger') return redirect(url_for('favorites.create')) if category.user_id != current_user.id: abort(403) db.session.delete(category) db.session.commit() flash('Deleted Successfuly', 'success') log = f'Delete {category.name} from category list on' addLog(log) return redirect(url_for('favorites.create'))
def createCategory(): if not current_user.is_authenticated: return redirect(url_for('main.home')) newCategory = request.form.get('category') rate = int(float(request.form.get('rate'))) checkCategory = Categories.query.filter_by( name=newCategory, user_id=current_user.id).first() if checkCategory: create_category = CreateCategory() flash('Category already exist', 'danger') return jsonify(success=False, error='Category already exist') if newCategory: new = Categories(name=newCategory, user_id=current_user.id, rate=rate) db.session.add(new) db.session.commit() log = f'Add {newCategory} to category list on' addLog(log) return jsonify(success=True) return jsonify(success=False)
def create(): if not current_user.is_authenticated: return redirect(url_for('main.home')) form = Create() create_category = CreateCategory() # this section of getting categories to list them in categories choices categories = Categories.query.filter_by(user_id=current_user.id).all() categoriesList = [] for category in categories: categoriesList.append((category.name, category.name)) form.category.choices = categoriesList if form.validate_on_submit(): requestForm = request.form metaData = {} for data in requestForm: if '-meta-' in data: typeAndName = data.split( "-meta-") #to get type and name of fileds from request metaData[typeAndName[0]] = { typeAndName[1]: request.form[data] } #store type filed, key and value we store type for viewing in front end category = Categories.query.filter_by(name=form.category.data).first() favoriteThing = Favorites(title=form.title.data, description=form.description.data, meta_data=metaData, user_id=current_user.id, createdAt=datetime.utcnow(), group=category) db.session.add(favoriteThing) db.session.commit() next_page = request.args.get('next') log = f'Add {favoriteThing.title} to favorite list on!' addLog(log) return redirect(next_page) if next_page else redirect( url_for('favorites.create')) form.category.choices = categoriesList log = 'open create form on' addLog(log) return render_template('create.html', title='Create', form=form, categoryForm=create_category, categories=categories)
def log(): logs = AuditLog.query.filter_by(user_id=current_user.id).all() log = 'Open logs tab on' addLog(log) return render_template('log.html',logs=logs)