def retrieve_rants(page=1): if is_json(request): rants = Rant.get_rants(json=True) return jsonify(status="success", data={'rants': rants}, message="", code=200), 200 else: pageObj = Rant.query.paginate(page, 4, False) return render_template('view_rants.html', rants=pageObj)
def validate(self): if not Form.validate(self): return False if Rant.title_exists(self.title.data, self.id.data): self.title.errors.append("That title is already used.") return False return True
def post(self): category = self.data['category'] title = self.data['title'] content = self.data['content'] rant = Rant.create(category, title, content, self.user) return True, { 'msg': 'rant_created', 'id': rant.key.id() }
def index(): if request.method == 'GET': rants = Rant.get_rants(top=3) rant_list = rants.all() if len(rant_list) > 0: rants_found = True else: rants_found = False return render_template('index.html', rants=rant_list, rants_found=rants_found)
def get(self): rant_id = int(self.request.path.split('/')[-1]) rant = Rant.getById(rant_id, self.user) if rant is not None: return True, { 'content': rant.content, 'title': rant.title, 'category': rant.category, 'created': str(rant.created), } else: return False, {'msg': 'rant_not_found'}
def get(self): rants = Rant.getByUser(self.user) if rants is not None: return True, [{ 'content': rant.content, 'title': rant.title, 'category': rant.category, 'created': str(rant.created), } for rant in rants] else: return False, {'msg': 'rants_not_found'}
def rants(): form = RantForm(request.form) error = '' if not is_json(request): #Rant updated from browser form POST (or deleted from browser) if form.validate() == False: return render_template('write_rant.html', form=form) title_in = request.form.get('title') content_in = request.form.get('content') category_list = [] if request.form['id'] is not u'': rant = Rant.query.filter_by(id = request.form.get('id')).first() action = "updated" if rant is None: form.id.errors.append("Could not update rant with id {0}".format(id)) return render_template('write_rant.html', form=form) else: if form.delete.data == True: if rant.user_id != g.user.id: return render_template('error_page.html', error="Unauthorized to edit this rant.") rant_id = rant.id db.session.delete(rant) db.session.commit() message = "Rant(ID: {0}) successfully deleted.".format(rant_id) return render_template('rant_posted.html', form=form, message=message) rant.title = title_in rant.content = content_in else: rant = Rant(title_in, content_in, g.user.id, datetime.utcnow()) action = "created" if rant.user_id != g.user.id: return render_template('error_page.html', error="Unauthorized to edit this rant.") # Delete all rant-categories before re-adding them RantCategory.query.filter_by(rant_id = rant.id).delete() db.session.add(rant) db.session.commit() rant = Rant.query.filter_by(id = rant.id).first() for key in request.form.keys(): for value in request.form.getlist(key): if key == 'categories': category_list.append(value) save_rant_categories(category_list, rant) message="Rant(ID: {0}) successfully {1}.".format(rant.id, action) return render_template('rant_posted.html', form=form, message=message) else: # New Rant POSTed from curl category_list = [] title_in, content_in = None, None if 'title' in request.json: title_in = request.json['title'] elif error is '': error = 'Title is required.' if 'content' in request.json: content_in = request.json['content'] elif error is '': error = 'Content is required.' if 'categories' in request.json: category_list = request.json['categories'] elif error is '': error = 'A list containing at least one valid category id is required.' if Rant.title_exists(title_in, None): error = "That title is already used." if error is not '': return jsonify(status="error", data={}, message=error, code=400), 400 rant = Rant(title_in, content_in, g.user.id, datetime.utcnow()) db.session.add(rant) db.session.commit() save_rant_categories(category_list, rant) return jsonify(status="success", data={'rant': rant.to_json()}, message="Rant successfully created.", code=200), 200