def categories(*args, **kwargs): if request.method == "GET": categories = Category.query.filter_by(user_id=current_user.id) error = kwargs.get("error", "") return render_template("/categories/categories.html", categories=categories, form=CategoryForm(), error=error) else: form = CategoryForm(request.form) # If the user already has a category by that name, let's not create a duplicate alreadyExists = db.session.query( db.exists().where(Category.name == form.name.data).where( Category.user_id == current_user.id)).scalar() if alreadyExists: return redirect(url_for('categories', error=1)) c = Category() c.name = form.name.data c.user_id = current_user.id db.session().add(c) db.session().commit() return redirect(url_for('categories', error=""))
def init_categories(): stmt = text('SELECT * FROM Category;') res = db.engine.execute(stmt) if (res.fetchone() != None): return category1 = Category('Sports') category2 = Category('Gaming') category3 = Category('Programming') userrole = Role('USER') adminrole = Role('ADMIN') db.session().add(userrole) db.session().add(adminrole) db.session().add(category1) db.session().add(category2) db.session().add(category3) db.session().commit() normaluser = User(' ', 'user', 'user') adminuser = User(' ', 'admin', 'admin') db.session().add(normaluser) db.session().add(adminuser) db.session().commit() db.session().add(User_Role(normaluser.id, 1)) db.session().add(User_Role(adminrole.id, 2)) db.session().commit()
def create_user(): if request.method == "GET": return render_template("/auth/newuser.html", form=CreateUserForm()) form = CreateUserForm(request.form) user = User.query.filter_by(username=form.username.data).first() if not user: username = form.username.data passwordHash = bcrypt.generate_password_hash( form.password.data).decode("utf-8") ## CHANGE TO BE BCRYPTED name = form.displayname.data # New user u = User(name, username, passwordHash) # THIS IS A HACK if u.name == "admin": u.role = "ADMIN" else: u.role = "USER" db.session().add(u) db.session().commit() Category.create_default_categories(u.id) return redirect(url_for("auth_login")) return render_template( "/auth/newuser.html", form=form, error="Username already in use! Choose another one.")
def categories_create(): form = CategoryForm(request.form) if not form.validate(): return render_template("categories/new.html", form=form) c = Category(form.name.data, form.description.data) c.account_id = current_user.id db.session().add(c) db.session().commit() return redirect(url_for("categories_index"))
def categories_create(): form = CategoryForm(request.form) if not form.validate(): return render_template("categories/new.html", form = form, categories = Category.query.order_by(Category.name)) t = Category(form.name.data) t.account_id = current_user.id db.session().add(t) db.session().commit() return redirect(url_for("categories_form"))
def categories_index(): categories = Category.query.all() category_count = Category.get_recipe_count_in_categories() return render_template("categories/categories.html", categories=categories, category_count=category_count)
def create_category(): if current_user.role != "ADMIN": abort(401) if request.method == "GET": return render_template("categories/new.html", form=AddCategoryForm()) form = AddCategoryForm(request.form) if not form.validate(): return render_template("categories/new.html", form=form) category_exists = Category.query.filter_by(name=form.name.data).first() if category_exists: form.name.errors.append("Category already exists") return render_template("categories/new.html", form=form) c = Category(form.name.data) db.session().add(c) db.session().commit() return redirect(url_for("categories_index"))
def items_all(): if current_user.role != "ADMIN": return render_template("index.html", msg=msg_admin_feature) categoryNames = Category.find_all_category_names() listnames = List.find_all_list_names() items = Item.query.all() return render_template("items/all.html", items = items, categoryNames = categoryNames, listnames = listnames)
def tasks_index(): categories = Category.get_categories() tasks = Task.query.filter_by(account_id = current_user.id) if 'category' not in session: session['category'] = "all" if request.args.get('category'): session['category'] = request.args.get('category') category = session['category'] if category != "all": if category == "none": category = None tasks = tasks.filter_by(category_id=category) tasks = tasks.order_by(Task.done, asc(Task.deadline)).all() overdue = Task.count_overdue()[0] return render_template("tasks/index.html", tasks = tasks, categories = categories, overdue = overdue)
def categories_index(): if current_user.role != "ADMIN": abort(401) categories = Category.count_threads_within_category() return render_template("categories/list.html", categories=categories)
def categories_index(): admin = False if current_user.role == "ADMIN": admin = True return render_template( "categories/list.html", categories=Category.find_items_per_category(), admin=admin, )
def list_page(list_id): list = List.query.get_or_404(list_id) if current_user.id != list.account_id: return render_template("index.html", msg=msg_other_user) categoryNames = Category.find_all_category_names() items = List.find_items_by_list(list_id) return render_template("/lists/info.html", list=List.query.get(list_id), items=items, categoryNames=categoryNames)
def item(item_id): item = Item.query.get_or_404(item_id) if current_user.id != item.account_id: return render_template("index.html", msg=msg_other_user) lists = List.find_all_lists_by_id(current_user.id) categories = Category.query.all() categoryNames = Category.find_all_category_names() listnames = List.find_all_lists_by_id_dictionary(current_user.id) return render_template("items/edit.html", item = item, lists = lists, categories = categories, categoryNames = categoryNames, listnames = listnames, aform = AmountForm())
def categories_create(): form = CategoryForm(request.form) if not form.validate(): return render_template("categories/new.html", form=form) if Category.query.filter(Category.account_id == current_user.id).filter( Category.name == form.name.data).first(): form.name.errors.append("Category named " + form.name.data + " already exists.") return render_template("categories/new.html", form=form) c = Category(form.name.data, form.description.data) c.account_id = current_user.id db.session().add(c) db.session().commit() return redirect(url_for("categories_index"))
def categories_create(): form = CategoryForm(request.form) if not form.validate(): return render_template("categories/new.html", form=form) c = Category(form.name.data) db.session().add(c) db.session().commit() return redirect(url_for("categories_index"))
def items_form(): category_list = Category.query.order_by('name').filter_by( account_id=current_user.id) c_list = [] for c in category_list: c_list.append(c) if len(c_list) == 0: c = Category("Ei kategoriaa") c.account_id = current_user.id db.session().add(c) db.session().commit() form = ItemForm() form.item_category.choices = [ (c.id, c.name) for c in Category.query.order_by('name').filter_by( account_id=current_user.id) ] return render_template('items/new.html', form=form)
def category(category_id): c = Category.query.get(category_id) if c.user_id == current_user.id: category_data = Category.get_transactions_by_account_for_category( category_id) return render_template("/categories/categorysummary.html", transactions=category_data, category=c) else: return "Unauthorized, fool!"
def categories_create(): form = NewCategoryForm(request.form) if not form.validate(): return render_template("categories/list.html", categories=Category.get_category_list(), form=form) category = Category(form.name.data) try: db.session.add(category) db.session.commit() except IntegrityError: db.session.rollback() return render_template("categories/list.html", categories=Category.get_category_list(), form=form, category_name_taken=True) return render_template("categories/list.html", categories=Category.get_category_list(), form=form)
def category_create(): form = CategoryForm() if form.validate_on_submit(): c = Category(form.name.data) db.session().add(c) db.session().commit() return redirect(url_for("category_create")) categories = Category.query.all() return render_template("categories/new.html", form = form, categories = categories)
def CategoryForm(requestform, other): class TempForm(FlaskForm): othercategory = StringField("If you selected 'other' from the preceding list, type new category here. Otherwise, leave this empty", [validators.Length(min=5, max=30)]) class Meta: csrf = False setattr(TempForm, 'categories', SelectField(u'Select Category', choices=Category.findChoices(other))) if requestform is None: return TempForm() return TempForm(requestform)
def categories_create(): if current_user.role != "ADMIN": return render_template("index.html", msg=msg_admin_feature) form = CategoryForm(request.form) if not form.validate(): return render_template("categories/new.html", form=form) c = Category(form.name.data) db.session().add(c) db.session().commit() return redirect(url_for("categories_index"))
def categories_create(): form = CategoryForm(request.form) c_test = Category.query.filter_by(name=form.name.data, account_id=current_user.id).first() if c_test: return render_template( "categories/new.html", form=form, error= "Kategoria on jo olemassa, valitse jokin toinen nimi kategorialle!" ) else: if not form.validate(): return render_template("categories/new.html", form=form) c = Category(form.name.data) c.account_id = current_user.id db.session().add(c) db.session().commit() return redirect(url_for("categories_index"))
def create_category(): form = CreateNewCategoryForm(request.form) if request.method == "GET": return render_template("categories/new.html", form=form) if request.method == "POST" and not form.validate_on_submit(): return render_template("categories/new.html", form=form) cat = Category(request.form.get("name")) db.session().add(cat) db.session().commit() return redirect(url_for("categories_index"))
def tasks_create(): form = TaskForm(request.form) form.tasks_and_categories.choices = [(category.id, category.name) for category in Category.query.all()] if not form.validate(): return render_template("tasks/new.html", form=form) t = Task(form.name.data, form.priority.data, Category.all_by_id(form.tasks_and_categories.data)) t.done = form.done.data t.account_id = current_user.id db.session().add(t) db.session().commit() return redirect(url_for("tasks_index"))
def search_from_everywhere(text): query = text.lower() messages = Message.find_by_given_text(query) topics = Topic.find_by_given_text(query) users = User.find_by_given_text(query) categories = Category.find_by_given_text(query) return render_template('search_results.html', text=text, messages=messages, topics=topics, users=users, categories=categories)
from application.categories.models import Category @login_manager.user_loader def load_user(user_id): return User.query.get(user_id) # datetime custom filter def format_datetime(value, format='%d-%m-%Y %H:%M:%S'): return value.strftime(format) app.jinja_env.filters['datetime'] = format_datetime # initiliaze database with default category and admin role try: db.create_all() if not Role.query.first(): admin_role = Role("admin") db.session.add(admin_role) db.session.commit() if not Category.query.first(): default_category = Category('default') db.session.add(default_category) db.session.commit() except: pass
def categories_index(): categories = Category.list_categories_for_user(current_user.id) return render_template("categories/list.html", categories=categories)
def categories_index(): form = NewCategoryForm() return render_template("categories/list.html", categories=Category.get_category_list(), form=form)
# login functionality part 2 from application.auth.models import User @login_manager.user_loader def load_user(user_id): return User.query.get(user_id) # database creation try: db.create_all() except: pass # If there are no accounts create admin account users_amount = User.amount_of_users() if users_amount == 0: User.create_admin_account() # If there are no categories, create default category # (default category is needed for adding items) from application.categories.models import Category categories_amount = Category.amount_of_categories() if categories_amount == 0: Category.create_default_category()
app.config["SECRET_KEY"] = urandom(32) from flask_login import LoginManager login_manager = LoginManager() login_manager.init_app(app) login_manager.login_view = "auth_login" login_manager.login_message = "Please login to use this functionality." @login_manager.user_loader def load_user(user_id): return User.query.get(user_id) try: db.create_all() # Adding the administrator account hashed_password = bcrypt.generate_password_hash("unlimitedpower").decode( 'utf-8') u = User("Administrator", "admin", hashed_password) db.session().add(u) db.session().commit() # Adding the first category c = Category("No category") c.account_id = 1 db.session.add(c) db.session.commit() except: pass