def new_category(): """ Add new category """ # Check if the user is loged in if 'username' not in login_session: return redirect('/login') if request.method == 'POST': # Get data from the front-end name = request.form['name'] description = request.form['description'] # Put the data into a model category = Category(name=name, user_id=login_session['user_id']) # Save description if there are one if description: category.description = description session.add(category) session.commit() return redirect(url_for('categories')) return render_template('categories/new_category.html')
def post(self): json = {} if not self.current_user: json = { 'error': 1, 'msg': self._('Access denied') } self.write(json) return title = self.get_argument('title', None) slug = self.get_argument('slug', None) description = self.get_argument('description', '') # valid arguments if not title: json = { 'error': 1, 'msg': self._('Title field can not be empty') } self.write(json) return if not slug: json = { 'error': 1, 'msg': self._('Slug field can not be empty') } self.write(json) return elif self.get_category_by_slug(slug): json = { 'error': 1, 'msg': self._('Slug already exists') } self.write(json) return # create category category = Category() category.title = title category.slug = slug category.description = description self.db.add(category) self.db.commit() # delete cache keys = ['CategoryList', 'SystemStatus'] self.cache.delete_multi(keys) json = { 'error': 0, 'msg': self._('Successfully created'), 'category': { 'id': category.id, 'title': category.title, 'slug': category.slug, 'description': category.description, 'permalink': category.permalink, 'post_count': category.post_count } } self.write(json)
def create_category(name, desc): category = Category.from_name(db.session, name) if not category: category = Category(name=name, description=desc) else: category.description = desc db.session.add(category) return category
def categories_post(): category = Category() category.description = request.json.get('description') db.session.add(category) db.session.commit() return jsonify(category.serialize()), 201
def save(self): category = Category() category.title = self.title.data.strip() category.slug = pretty_slug(self.title.data).strip() category.description = self.description.data if (self.parent.data.strip() is "") or (self.parent.data.strip() is u""): category.parent = None else: category.parent = ndb.Key(urlsafe=self.parent.data.strip()) category.put()
def categories(id=None): if request.method == 'GET': if id is not None: category = Category.query.get(id) if category: return jsonify(category.serialize()), 200 else: return jsonify({'category': 'not found'}), 400 else: categories = Category.query.all() categories = list( map(lambda category: category.serialize(), categories)) return jsonify(categories), 200 if request.method == 'POST': #validar la informacao category = Category() category.description = request.json.get('description') db.session.add(category) db.session.commit() return jsonify(category.serialize()), 201 if request.method == 'PUT': category = Category.query.get(id) category.description = request.json.get('description') db.session.commit() return jsonify(category.serialize()), 200 if request.method == 'DELETE': category = Category.query.get(id) db.session.delete(category) db.session.commit() return jsonify({'category': 'deleted'}), 400
def category(): loc = "nav-category" if request.method == "POST" and request.form["action"] == "insert": #insert new category description = request.form["description"] category_type = request.form["categorytype"] success = False category = Category(g.user.id, description, category_type) if not category.isExists(): db.session.add(category) db.session.commit() success = (True if category.id != -1 else False) return render_template("category.html", success = success, locid = loc) elif request.method == "POST" and request.form["action"] == "add": #show add category return render_template("category.html", action = "add", locid = loc) elif request.method == "POST" and request.form["action"] == "edit": #show edit category category_id = request.form["categoryid"] return render_template("category.html", action = "edit", category = Category.query.filter_by(id=category_id).one(), locid = loc) elif request.method == "POST" and request.form["action"] == "fin-edit": #show finalise edit category category_id = request.form["categoryid"] description = request.form["description"] category_type = request.form["categorytype"] success = False category = Category(g.user.id, description, category_type) if not category.isExists(): category = Category.query.get(category_id) category.description = description category.category_type = category_type db.session.commit() success = True return render_template("category.html", action = "edit-fin", success = success, locid = loc) elif request.method == "POST" and request.form["action"] == "delete": #delete category category_id = request.form["categoryid"] category = Category.query.get(category_id) db.session.delete(category) db.session.commit() return render_template("category.html", action = "delete-fin", locid = loc) else: #show category list rs = db.session.query(Category).filter_by(user_id=g.user.id) return render_template("category.html", action = "show", category = rs, locid = loc)
def create_category(parent_id, name, description, user_id): name = name.title() category = Category(name=name, description=description, created_by=user_id) response = category_pb2.Category(result="failed") try: with session_scope() as session: if parent_id: try: parent = session.query(Category).filter( Category.is_deleted == False, Category.id == parent_id).first() if parent: category.parent_id = parent.id else: session.commit() response.message = "Category parent does not exist!" response.status = status.STATUS_404_NOT_FOUND return response except DataError: session.commit() response.message = "Category parent does not exist!" response.status = status.STATUS_404_NOT_FOUND return response session.add(category) response = category_pb2.Category( result="success", status=status.STATUS_201_CREATED, message="Category created successfully!") session.commit() except IntegrityError as e: with session_scope() as session: try: category = session.query(Category).filter( Category.is_deleted == True, Category.name.ilike(name)).first() except DataError: category = None if category: category.name = name category.description = description category.created_at = datetime.now() category.created_by = user_id category.updated_at = None category.updated_by = None category.is_deleted = False category.deleted_at = None category.deleted_by = None if parent_id: category.parent_id = parent_id else: category.parent_id = None # result = session.query(Category).filter(Category.is_deleted==True, Category.name.ilike(name)) \ # .update({"name": name, "description": description, "created_at": datetime.now(), "created_by": user_id, # "updated_at": None, "updated_by": None, "parent_id": None, # "is_deleted": False, "deleted_at": None, "deleted_by": None}) # if result: session.commit() response = category_pb2.Category( result="success", status=status.STATUS_201_CREATED, message="Category created successfully!") return response # else: # session.commit() # response.message = "Unable to create category!" # response.status = status.STATUS_500_INTERNAL_SERVER_ERROR # return response else: session.commit() response.message = "Category already exists with same name!" response.status = status.STATUS_403_FORBIDDEN return response except Exception as e: print(e) response.message = "Unexpected error occurred!" response.status = status.STATUS_500_INTERNAL_SERVER_ERROR pass return response