def confirmcreatepage(): product_name = request.form['product_name'] category_id = request.form['category_id'] description = request.form['description'] product_image = request.form['product_image'] maximumId = db.session.query(func.max( Product.id)).scalar() # Get Max Product id if not maximumId: maximumId = 0 new_product_id = maximumId + 1 # Set new product id as max product id + 1 product = Product(id=new_product_id, product_name=product_name, description=description, product_image=product_image, want_count=0) # Create New Product Instance maximumId = db.session.query(func.max( Classification.id)).scalar() # Get Max Classification id if not maximumId: maximumId = 0 new_classification_id = maximumId + 1 # Set new Classification id as max Classification id + 1 classification = Classification(id=new_classification_id, product_id=new_product_id, category_id=category_id) db.session.add(product) ## Insert into DB db.session.add(classification) ## Insert db.session.commit() ## commit transaction category = db.session.query(Product_Category).filter( Product_Category.id == category_id).first() return render_template('Confirmcreatepage.html', product=product, category=category)
def post(self): self.resource_fields['data'] = fields.Nested(self.album_fields) args = self.parser.parse_args() name = args.get('name', '') artist = args.get('artist', '') is_hot = args.get('is_hot', 0) classification = args.get('classification', []) cl_obj_list = Classification.select().where( Classification.id << classification) album = Album(name=name, artist=artist, is_hot=is_hot) album.save() cl_json_list = [] if cl_obj_list.exists(): for item in cl_obj_list: album.classification.add(item) cl_json_list.append(model_to_dict(item)) album.save() album = model_to_dict(album) album['classification'] = cl_json_list resp = {'error_code': 0, 'data': album} return make_response(json.dumps(marshal(resp, self.resource_fields)), 200)
def put(self, album_id): album = Album.select().where(Album.id == album_id).first() if album: status_code = 200 args = self.parser.parse_args() classification = args.get('classification', []) cl_json_list = [] """ Return album data without update if error classification data""" if type(classification) == type([]) and len(classification) != 0: cl_obj_list = Classification.select().where( Classification.id << classification) for item in cl_obj_list: if item in album.classification: continue album.classification.add(item) album.save() for item in album.classification: cl_json_list.append(model_to_dict(item)) album = model_to_dict(album) album['classification'] = cl_json_list resp = {'error_code': 0, 'data': album} else: status_code = 400 self.resource_fields['message'] = fields.String( attribute='message') resp = {'error_code': 40021, 'message': 'Album dose not exist.'} return make_response(json.dumps(marshal(resp, self.resource_fields)), status_code)
def get(self): resource_fields = { 'error_code': fields.Integer(attribute='error_code'), 'data': fields.List(fields.Nested(self.classification_fields)) } cl_list = Classification.select() data = [model_to_dict(cl) for cl in cl_list] resp = {'error_code': 0, 'data': data} return make_response(json.dumps(marshal(resp, resource_fields)), 200)
def update_classifications(categories): for category in categories.keys(): if not Category.query.filter_by(category=category).first(): new_cat = Category(category=category) db.session.add(new_cat) db.session.commit() for category in categories: for article_id, confidence in categories[category]: cat = Category.query.filter_by(category=category).first() article = Article.query.filter_by(id=article_id).first() cls = Classification(confidence=confidence) cls.article = article cls.category = cat cat.articles.append(cls) article.categories.append(cls) db.session.add(article) db.session.add(cat) db.session.add(cls) db.session.commit() print("Database updated")
def post(self): resource_fields = { 'error_code': fields.Integer(attribute='error_code'), 'message': fields.String(attribute='message'), 'data': fields.Nested(self.classification_fields) } args = self.parser.parse_args() name = args.get('name') parent_id = args.get('parent', None) if not (parent_id and Classification.select().where( Classification.id == parent_id).exists()): self.classification_fields.pop('parent') try: cl = Classification.create(name=name, parent=parent_id) status_code = 201 resp = {'error_code': 0, 'data': model_to_dict(cl)} resource_fields.pop('message') except IntegrityError: resp = {'error_code': 40020, 'message': 'Duplicate name.'} resource_fields.pop('data') status_code = 400 return make_response(json.dumps(marshal(resp, resource_fields)), status_code)
def insert(self, data): session = self.__thread_safe_session_factory() i_game = 1 i_genres = 1 for title, infos in data.items(): if len(title) > 128: continue # Adding game session.add( Game(id=i_game, title=title, publication_year=infos["publication_year"], min_players=infos["min_players"], max_players=infos["max_players"], min_playtime=infos["min_playtime"], image=infos["images"]["original"])) # Adding note session.add( Note(note=round(infos["average_rating"]), message=self.__default_message, user_id=1, game_id=i_game)) for genre in infos["type"]: # Adding genre if genre not in self.__genres_dict: self.__genres_dict[genre] = i_genres session.add(Genre(id=i_genres, name=genre)) i_genres += 1 # Adding classification session.add( Classification(game_id=i_game, genre_id=self.__genres_dict[genre])) i_game += 1 session.commit() self.__thread_safe_session_factory.remove()
def avaliar_motorista(request, pk_pedido, nota): pedido = Pedido.objects.get(id=pk_pedido) motorista = Motorista.objects.get(user=pedido.motorista) new_class = Classification(user=motorista.user, pedido=pedido, nota=nota) new_class.save() return redirect('/app/pedidos/loja')