def delete(self, idea_id): """Delete the idea with the selected idea_id""" if not idea_exists(idea_id): idea_ns.abort(404, 'Idea not found') check_for_admin_or_idea_ownership(get_idea(idea_id)) delete_idea_by_id(idea_id) return '', 204
def get(self, idea_id): """Show the idea with the selected idea_id""" queried_idea = get_idea(idea_id) if queried_idea is None: idea_ns.abort(404, 'Idea not found') check_for_idea_ownership(queried_idea) return marshal(queried_idea.as_dict(), idea), 200
def get(self, idea_id): """Show all votes that are targeted to the idea with the selected id""" if not idea_exists(idea_id): idea_ns.abort(404, 'Idea not found') queried_idea = get_idea(idea_id) check_for_admin_or_idea_ownership(queried_idea) return marshal(collection_as_dict(queried_idea.votes), vote), 200
def put(self, idea_id): """Update the idea with the selected idea_id""" if is_admin(): idea_ns.abort(403, 'Admin is not allowed to modify ideas') if not idea_exists(idea_id): idea_ns.abort(404, 'Idea not found') check_for_idea_ownership(get_idea(idea_id)) json_data = request.get_json(force=True) if idea_title_exists(json_data['title']): idea_ns.abort(409, "Idea already exists") edit_idea_by_json(idea_id, json_data) return '', 204
def delete_idea(idea_id): if not current_user.is_authenticated: return redirect(url_for('login')) if not idea_exists(idea_id): abort(404) if str(current_user.role) == 'UserRole.admin': delete_idea_by_id(idea_id) flash('The idea has been deleted!', 'info') return redirect(url_for('admin_ideas')) else: if current_user.id != get_idea(idea_id).author.id: abort(403) delete_idea_by_id(idea_id) flash('Your idea has been deleted!', 'info') return redirect(url_for('profile'))
def vote(): if not current_user.is_authenticated: return redirect(url_for('login')) queried_idea = get_idea(request.form.get('target')) if queried_idea is None: abort(409) if vote_exists(current_user.id, queried_idea.id): edit_vote( get_vote(current_user.id, queried_idea.id).id, request.form.get('value')) else: future_vote = Vote(owner=current_user, target=queried_idea, value=request.form.get('value')) save_vote(future_vote) return redirect_back()
def test_get_idea(self): self.addTestModels() self.assertEqual(self.testIdea, get_idea(self.testIdea.id)) self.assertIsNone(get_idea(1234))