def call_api(bdd): """ Request Open Food Facts API Save the results in the database param bdd: database type bdd: database.Database return: None """ headers = {"User-Agent": HEADERS} code_set = set() print("Mise à jour de la base de données...") params = PAYLOAD.copy() for index, category in enumerate(CATEGORIES): params["tag_0"] = category req = requests.get(URL_SEARCH, params=params, headers=headers) results_json = req.json() cat = Category(bdd, category, index) cat.add() for product_info in results_json["products"]: if not product_info["code"] in code_set: code_set.add(product_info["code"]) pro = Product(bdd) pro.cleaner(product_info) pro.add() bdd.add_assoc_pro_cat(pro, index)
def add_news(): form = NewsForm() if form.validate_on_submit(): session = db_session.create_session() news = News() news.title = form.title.data news.content = form.content.data f = form.photo.data if f: filename = secure_filename(f.filename) f.save(os.path.join(app.static_folder, 'img', filename)) news.photo = f.filename news.is_private = form.is_private.data lst = [] tags = [] for e in session.query(Category.name): lst.append(*e) for i in form.tag.data.split(): if i not in lst: tag = Category(name=i) session.add(tag) tags.append(i) news.tag_id = ' '.join(tags) current_user.news.append(news) session.merge(current_user) session.commit() return redirect('/') return render_template('news.html', title='Добавление новости', form=form)
def post(self): args = parser.parse_args() session = db_session.create_session() item = Category(name=args['name']) session.add(item) session.commit() return jsonify({'success': 'OK'})
def add_job(): form = JobForm() if form.validate_on_submit(): session = db_session.create_session() if str(current_user.id) == str(form.team_leader.data): job = Jobs() job.job = form.job.data job.team_leader = form.team_leader.data job.work_size = form.work_size.data job.collaborators = form.collaborators.data job.is_finished = form.is_finished.data job.start_date = datetime.datetime.now() job.end_date = datetime.datetime.now() job.categories.append(Category(name=form.categories.data)) print(job.categories, job.categories[0].name) session.add(job) session.commit() return redirect("/") return render_template('add_job.html', message="нет доступа", form=form, name='Добавление') return render_template('add_job.html', title='Добавление работы', form=form, text='Наше приложение', name='Добавление')
def add_job(): form = JobsForm() if form.validate_on_submit(): session = db_session.create_session() job = Jobs(team_leader=form.team_leader.data, job=form.job.data, work_size=form.work_size.data, collaborators=form.collaborators.data, is_finished=form.is_finished.data, start_date=datetime.now()) categ = Category() categ.name = form.category.data job.category.append(categ) session.add(job) session.commit() return redirect('/works/') return render_template('addjob.html', title='Добавление работы', form=form)
def add_cat(): form = CreateCategory() db_sess = db_session.create_session() if form.validate_on_submit(): cat = Category() cat.name = form.name.data if form.anecdots_id.data: cat.anecdoties = form.anecdots_id.data else: cat.anecdoties = ' ' db_sess.add(cat) db_sess.commit() id = db_sess.query(Category).filter( Category.name == form.name.data).first().id return redirect(f'/viewcategory/{id}') return render_template('addcategory.html', title='Добавление категории', form=form)
def edit_job(id): form = JobsForm() if request.method == "GET": session = db_session.create_session() job = session.query(Jobs).filter(Jobs.id == id).first() if job and (current_user.id == job.creator or current_user.id == 1): form.job.data = job.job form.team_leader.data = job.team_leader form.work_size.data = job.work_size form.collaborators.data = job.collaborators form.start_date.data = job.start_date form.end_date.data = job.end_date form.is_finished.data = job.is_finished for i in job.categories: form.categories.data += i.name + ", " form.categories.data = str(form.categories.data)[:-1] else: abort(404) if form.validate_on_submit(): session = db_session.create_session() job = session.query(Jobs).filter(Jobs.id == id).first() if job: session.delete(job) job = Jobs() job.team_leader = form.team_leader.data job.job = form.job.data job.work_size = form.work_size.data job.collaborators = form.collaborators.data job.start_date = form.start_date.data job.end_date = form.end_date.data job.is_finished = form.is_finished.data for i in job.categories: job.categories.remove(Category(name=i.name)) categories = Category(name=form.categories.data) job.categories.append(categories) session.add(job) session.commit() return redirect('/page') else: abort(404) return render_template('edit_job.html', title='Редактирование работы', form=form)
def edit_job(id): form = JobForm() if request.method == "GET": db_sess = db_session.create_session() job = db_sess.query(Jobs).filter(Jobs.id == id, (Jobs.user == current_user) | ( current_user.id == 1)).first() if job: form.team_leader.data = job.user.id form.job.data = job.job form.work_size.data = job.work_size form.collaborators.data = job.collaborators form.is_finished.data = job.is_finished form.categories.data = job.categories[0].name else: abort(404) if form.validate_on_submit(): db_sess = db_session.create_session() job = db_sess.query(Jobs).filter(Jobs.id == id, (Jobs.user == current_user) | ( current_user.id == 1)).first() if job: cats = form.categories.data cat = db_sess.query(Category).filter(Category.name == cats).first() if not cat: cat = Category() cat.name = cats db_sess.add(cat) db_sess.commit() cat = db_sess.query(Category).filter( Category.name == cats).first() job.team_leader = form.team_leader.data job.collaborators = form.collaborators.data job.work_size = form.work_size.data job.is_finished = form.is_finished.data job.job = form.job.data job.categories = [cat] db_sess.commit() return redirect('/') else: abort(404) return render_template('job.html', title='Edit Job', form=form)
def put(self, jobs_id): abort_if_job_not_found(jobs_id) args = edit_pasres.parse_args() db_sess = db_session.create_session() cats = args['categories'] cat = db_sess.query(Category).filter(Category.name == cats).first() if not cat: cat = Category() cat.name = cats db_sess.add(cat) db_sess.commit() cat = db_sess.query(Category).filter(Category.name == cats).first() job = db_sess.query(Jobs).get(jobs_id) job.categories = [cat] job.team_leader = args['team_leader'] job.collaborators = args['collaborators'] job.work_size = args['work_size'] job.is_finished = args['is_finished'] job.job = args['job'] db_sess.commit() return jsonify({'success': 'OK'})
def add_job(): form = JobForm() if form.validate_on_submit(): db_sess = db_session.create_session() job = Jobs() cats = form.categories.data cat = db_sess.query(Category).filter(Category.name == cats).first() if not cat: cat = Category() cat.name = cats db_sess.add(cat) db_sess.commit() cat = db_sess.query(Category).filter(Category.name == cats).first() job.team_leader = form.team_leader.data job.collaborators = form.collaborators.data job.work_size = form.work_size.data job.is_finished = form.is_finished.data job.job = form.job.data job.categories = [cat] db_sess.add(job) db_sess.commit() return redirect('/') return render_template('job.html', title='Adding a Job', form=form)
def post(self): args = add_parser.parse_args() db_sess = db_session.create_session() cats = args['categories'] cat = db_sess.query(Category).filter(Category.name == cats).first() if not cat: cat = Category() cat.name = cats db_sess.add(cat) db_sess.commit() cat = db_sess.query(Category).filter(Category.name == cats).first() job = Jobs( id=args['id'], team_leader=args['team_leader'], job=args['job'], work_size=args['work_size'], collaborators=args['collaborators'], is_finished=args['is_finished'], ) job.categories.append(cat) db_sess.add(job) db_sess.commit() return jsonify({'success': 'OK'})
def edit_jobs(id): form = JobsForm() if request.method == "GET": session = db_session.create_session() job = session.query(Jobs).filter(Jobs.id == id, (Jobs.user == current_user) | (current_user.id == 1)).first() if job: form.team_leader.data = job.team_leader form.job.data = job.job form.work_size.data = job.work_size form.collaborators.data = job.collaborators form.category.data = job.category form.is_finished.data = job.is_finished else: abort() if form.validate_on_submit(): session = db_session.create_session() job = session.query(Jobs).filter(Jobs.id == id, (Jobs.user == current_user) | (current_user.id == 1)).first() if job: job.team_leader = form.team_leader.data job.job = form.job.data job.work_size = form.work_size.data job.collaborators = form.collaborators.data categ = Category() categ.name = form.category.data job.category.append(categ) job.is_finished = form.is_finished.data session.commit() return redirect('/works/') else: abort() return render_template('addjob.html', title='Редактирование работы', form=form)
def reqister(): form = RegisterForm() if form.validate_on_submit(): if form.password.data != form.password_again.data: return render_template('register.html', title='Регистрация', form=form, message="Пароли не совпадают") session = create_session() if session.query(User).filter(User.email == form.email.data).first(): return render_template('register.html', title='Регистрация', form=form, message="Такой пользователь уже есть") user_id = random_id() category_id = random_id() user = User(id=user_id, surname=form.surname.data, name=form.name.data, category=category_id, age=form.age.data, description=form.description.data, email=form.email.data, worker=form.worker.data) UPLOAD_DIR: Path = Path(__file__).parent / 'static/users_img' UPLOAD_DIR.mkdir(parents=True, exist_ok=True) for upload in request.files.getlist('images'): filename = secure_filename(upload.filename) if filename != '': save_path = str(UPLOAD_DIR / filename) save_path2 = save_path.split('users_img') filename = random_name() save_path = save_path2[0] + 'users_img\\' + filename + '.jpg' upload.save(save_path) user.img = '\\static\\users_img\\{}.jpg'.format(filename) category = Category(id=category_id, prog=form.category_prog.data, des=form.category_des.data, av=form.category_av.data, mrk=form.category_mrk.data, tw=form.category_tw.data) pas = form.password.data user.hassed_password = User.set_password(pas) session.add(user) session.add(category) session.commit() return redirect('/login') return render_template('register.html', title='Регистрация', form=form)
def init_jobs(): session = db_session.create_session() session.query(Jobs).delete() session.query(Category).delete() job = Jobs( team_leader=1, job="deployment of residential modules 1 and 2", work_size=15, collaborators="2, 3", start_date=datetime.now(), is_finished=False, ) session.add(job) cat = Category(name="Important") cat.jobs.append(job) job2 = Jobs( team_leader=1, job="test", work_size=15, collaborators="2, 3", start_date=datetime.now(), is_finished=False, ) cat.jobs.append(job2) job3 = Jobs( team_leader=1, job="test2", work_size=15, collaborators="2, 3", start_date=datetime.now(), is_finished=True, ) cat2 = Category(name="Something") cat2.jobs.append(job3) session.add_all((job, job2, job3, cat, cat2)) session.commit()
def edit_news(id): form = NewsForm() if request.method == "GET": session = db_session.create_session() news = session.query(News).filter(News.id == id, News.users == current_user).first() if news: form.title.data = news.title form.content.data = news.content form.is_private.data = news.is_private form.tag.data = news.tag_id else: abort(404) if form.validate_on_submit(): session = db_session.create_session() news = session.query(News).filter(News.id == id, News.users == current_user).first() if news: news.title = form.title.data news.content = form.content.data news.is_private = form.is_private.data lst = [] tags = [] for e in session.query(Category.name): lst.append(*e) for i in form.tag.data.split(): if i not in lst: tag = Category(name=i) session.add(tag) tags.append(i) news.tag_id = ' '.join(tags) f = form.photo.data if f: filename = secure_filename(f.filename) f.save(os.path.join(app.static_folder, 'img', filename)) news.photo = f.filename session.commit() return redirect('/') else: abort(404) return render_template('news.html', title='Редактирование новости', form=form)
def add_job(): form = JobsForm() if request.method == 'POST' and form.validate_on_submit(): s = db_session.create_session() job = Jobs() job.team_leader = form.team_leader.data job.job = form.job.data job.work_size = form.work_size.data job.collaborators = form.collaborators.data job.start_date = form.start_date.data job.end_date = form.end_date.data job.is_finished = form.is_finished.data job.creator = current_user.id categories = Category(name=form.categories.data) job.categories.append(categories) s.add(job) s.commit() return redirect('/page') return render_template('add_job.html', title='Добавление работы', form=form)
def add_job(): form = JobsForm() if form.validate_on_submit(): session = db_session.create_session() if not session.query(User).filter( User.id == form.team_leader.data).first(): return render_template('job.html', title='Добавление работы', form=form, message="Id ТимЛида не существует") job = Jobs(job=form.job.data, team_leader=form.team_leader.data, work_size=form.work_size.data, collaborators=form.collaborators.data, is_finished=form.is_finished.data) job.categories.append(Category(form.categories.data)) current_user.jobs.append(job) session.merge(current_user) session.commit() return redirect('/') return render_template('job.html', title='Добавление работы', form=form)
def main(): db_session.global_init("db/astro.db") capitan = User() capitan.surname = "Scott" capitan.name = "Ridley" capitan.age = 21 capitan.position = "captain" capitan.speciality = "research engineer" capitan.address = "module_1" capitan.email = "*****@*****.**" capitan.set_password('password') capitan.city_from = 'Berlin' colon1 = User() colon1.surname = "Weir" colon1.name = "Andy" colon1.age = 18 colon1.position = "common" colon1.speciality = "worker" colon1.address = "module_1" colon1.email = "*****@*****.**" colon1.city_from = 'New York' colon2 = User() colon2.surname = "Sanders" colon2.name = "Teddy" colon2.age = 19 colon2.position = "common" colon2.speciality = "worker" colon2.address = "module_2" colon2.email = "*****@*****.**" colon2.city_from = 'Paris' category1 = Category() category1.name = '1' category2 = Category() category2.name = '2' category3 = Category() category3.name = '3' job1 = Jobs() job1.team_leader = 1 job1.job = "deployment of residential modules 1 and 2" job1.work_size = 15 job1.collaborators = "2, 3" job1.start_date = datetime.datetime.now() job1.is_finished = False job1.categories.append(category1) job2 = Jobs() job2.team_leader = 2 job2.job = "Exploration of mineral resourses" job2.work_size = 15 job2.collaborators = "4, 3" job2.start_date = datetime.datetime.now() job2.is_finished = False job2.categories.append(category2) job3 = Jobs() job3.team_leader = 3 job3.job = "Development of management system" job3.work_size = 25 job3.collaborators = "5" job3.start_date = datetime.datetime.now() job3.is_finished = True job3.categories.append(category3) department1 = Department() department1.title = 'Department of geological exploration' department1.chief = 2 department1.members = '1, 2, 3' department1.email = '*****@*****.**' department2 = Department() department2.title = 'Department of biological research' department2.chief = 3 department2.members = '2, 3' department2.email = '*****@*****.**' department3 = Department() department3.title = 'Department of construction' department3.chief = 1 department3.members = '1, 3' department3.email = '*****@*****.**' db_sess = db_session.create_session() db_sess.add(capitan) db_sess.add(colon1) db_sess.add(colon2) db_sess.add(job1) db_sess.add(job2) db_sess.add(job3) db_sess.add(department1) db_sess.add(department2) db_sess.add(department3) db_sess.add(category1) db_sess.add(category2) db_sess.add(category3) db_sess.commit()