def home(request): if request.method == 'GET': form = UserForm() return render(request, 'home.html', {'form': form}) else: #function = Function() function = DishFunction() data = request.POST user = User(data) dishes = function.get_menu(user=user) form = UserForm(data) return render(request, 'home.html', { 'form': form, 'user': user, 'dishes': dishes })
class Helper(Test): """Class to help testing the web interface""" user = User() def html_title(self, title=None): """Helper function to create an HTML title""" if title is None: return "<title>PyBossa</title>" else: return "<title>PyBossa · %s</title>" % title @patch('pybossa.view.account.signer') def register(self, mock, fullname="John Doe", name="johndoe", password="******", email=None): """Helper function to register and sign in a user""" if email is None: email = name + '@example.com' userdict = {'fullname': fullname, 'name': name, 'email_addr': email, 'password': password} mock.loads.return_value = userdict return self.app.get('/account/register/confirmation?key=fake-key', follow_redirects=True) def signin(self, method="POST", email="*****@*****.**", password="******", next=None): """Helper function to sign in current user""" url = '/account/signin' if next is not None: url = url + '?next=' + next if method == "POST": return self.app.post(url, data={'email': email, 'password': password}, follow_redirects=True) else: return self.app.get(url, follow_redirects=True) def profile(self, name="johndoe"): """Helper function to check profile of signed in user""" url = "/account/%s" % name return self.app.get(url, follow_redirects=True) def update_profile(self, method="POST", id=1, fullname="John Doe", name="johndoe", locale="es", email_addr="*****@*****.**", new_name=None, btn='Profile'): """Helper function to update the profile of users""" url = "/account/%s/update" % name if new_name: name = new_name if (method == "POST"): return self.app.post(url, data={'id': id, 'fullname': fullname, 'name': name, 'locale': locale, 'email_addr': email_addr, 'btn': btn}, follow_redirects=True) else: return self.app.get(url, follow_redirects=True) def signout(self): """Helper function to sign out current user""" return self.app.get('/account/signout', follow_redirects=True) def create_categories(self): with self.flask_app.app_context(): categories = db.session.query(Category).all() if len(categories) == 0: print "Categories 0" print "Creating default ones" self._create_categories() def new_application(self, method="POST", name="Sample Project", short_name="sampleapp", description="Description", long_description=u'Long Description\n================'): """Helper function to create a project""" if method == "POST": self.create_categories() return self.app.post("/app/new", data={ 'name': name, 'short_name': short_name, 'description': description, 'long_description': long_description, }, follow_redirects=True) else: return self.app.get("/app/new", follow_redirects=True) def new_task(self, appid): """Helper function to create tasks for a project""" tasks = [] for i in range(0, 10): tasks.append(Task(app_id=appid, state='0', info={})) db.session.add_all(tasks) db.session.commit() def delete_task_runs(self, app_id=1): """Deletes all TaskRuns for a given app_id""" db.session.query(TaskRun).filter_by(app_id=1).delete() db.session.commit() def task_settings_scheduler(self, method="POST", short_name='sampleapp', sched="default"): """Helper function to modify task scheduler""" url = "/app/%s/tasks/scheduler" % short_name if method == "POST": return self.app.post(url, data={ 'sched': sched, }, follow_redirects=True) else: return self.app.get(url, follow_redirects=True) def task_settings_redundancy(self, method="POST", short_name='sampleapp', n_answers=30): """Helper function to modify task redundancy""" url = "/app/%s/tasks/redundancy" % short_name if method == "POST": return self.app.post(url, data={ 'n_answers': n_answers, }, follow_redirects=True) else: return self.app.get(url, follow_redirects=True) def task_settings_priority(self, method="POST", short_name='sampleapp', task_ids="1", priority_0=0.0): """Helper function to modify task redundancy""" url = "/app/%s/tasks/priority" % short_name if method == "POST": return self.app.post(url, data={ 'task_ids': task_ids, 'priority_0': priority_0 }, follow_redirects=True) else: return self.app.get(url, follow_redirects=True) def delete_application(self, method="POST", short_name="sampleapp"): """Helper function to delete a project""" if method == "POST": return self.app.post("/app/%s/delete" % short_name, follow_redirects=True) else: return self.app.get("/app/%s/delete" % short_name, follow_redirects=True) def update_application(self, method="POST", short_name="sampleapp", id=1, new_name="Sample Project", new_short_name="sampleapp", new_description="Description", new_allow_anonymous_contributors="False", new_category_id="1", new_long_description="Long desc", new_sched="random", new_hidden=False, new_password=''): """Helper function to update a project""" if method == "POST": if new_hidden: return self.app.post("/app/%s/update" % short_name, data={ 'id': id, 'name': new_name, 'short_name': new_short_name, 'description': new_description, 'allow_anonymous_contributors': new_allow_anonymous_contributors, 'category_id': new_category_id, 'long_description': new_long_description, 'sched': new_sched, 'hidden': new_hidden, 'password': new_password, 'btn': 'Save'}, follow_redirects=True) else: return self.app.post("/app/%s/update" % short_name, data={'id': id, 'name': new_name, 'short_name': new_short_name, 'allow_anonymous_contributors': new_allow_anonymous_contributors, 'category_id': new_category_id, 'long_description': new_long_description, 'sched': new_sched, 'description': new_description, 'password': new_password, 'btn': 'Save' }, follow_redirects=True) else: return self.app.get("/app/%s/update" % short_name, follow_redirects=True)
class Helper(object): """Class to help testing the web interface""" user = User() app_short_name = "sampleapp" def setUp(self): self.app = web.app.test_client() model.rebuild_db() def tearDown(self): db.session.remove() @classmethod def teardown_class(cls): model.rebuild_db() def html_title(self, title=None): """Helper function to create an HTML title""" if title is None: return "<title>PyBossa</title>" else: return "<title>PyBossa · %s</title>" % title def register(self, method="POST", fullname="John Doe", username="******", password="******", password2=None, email=None): """Helper function to register and sign in a user""" if password2 is None: password2 = password if email is None: email = username + '@example.com' if method == "POST": return self.app.post('/account/register', data={ 'fullname': fullname, 'username': username, 'email_addr': email, 'password': password, 'confirm': password2 }, follow_redirects=True) else: return self.app.get('/account/register', follow_redirects=True) def signin(self, method="POST", email="*****@*****.**", password="******", next=None): """Helper function to sign in current user""" url = '/account/signin' if next is not None: url = url + '?next=' + next if method == "POST": return self.app.post(url, data={ 'email': email, 'password': password }, follow_redirects=True) else: return self.app.get(url, follow_redirects=True) def profile(self): """Helper function to check profile of signed in user""" return self.app.get("/account/profile", follow_redirects=True) def update_profile(self, method="POST", id=1, fullname="John Doe", name="johndoe", locale="es", email_addr="*****@*****.**"): """Helper function to update the profile of users""" if (method == "POST"): return self.app.post("/account/profile/update", data={ 'id': id, 'fullname': fullname, 'name': name, 'locale': locale, 'email_addr': email_addr }, follow_redirects=True) else: return self.app.get("/account/profile/update", follow_redirects=True) def signout(self): """Helper function to sign out current user""" return self.app.get('/account/signout', follow_redirects=True) def create_categories(self): categories = db.session.query(model.Category).all() if len(categories) == 0: print "Categories 0" print "Creating default ones" Fixtures.create_categories() def new_application( self, method="POST", name="Sample App", short_name="sampleapp", description="Description", thumbnail='An Icon link', allow_anonymous_contributors='True', category_id="1", long_description=u'<div id="long_desc">Long desc</div>', hidden=False): """Helper function to create an application""" if method == "POST": self.create_categories() if hidden: return self.app.post("/app/new", data={ 'name': name, 'short_name': short_name, 'description': description, 'thumbnail': thumbnail, 'allow_anonymou_contributors': allow_anonymous_contributors, 'category_id': category_id, 'long_description': long_description, 'hidden': hidden, }, follow_redirects=True) else: return self.app.post("/app/new", data={ 'name': name, 'short_name': short_name, 'description': description, 'thumbnail': thumbnail, 'allow_anonymous_contributors': allow_anonymous_contributors, 'category_id': category_id, 'long_description': long_description, }, follow_redirects=True) else: return self.app.get("/app/new", follow_redirects=True) def new_task(self, appid): """Helper function to create tasks for an app""" tasks = [] for i in range(0, 10): tasks.append(model.Task(app_id=appid, state='0', info={})) db.session.add_all(tasks) db.session.commit() def delTaskRuns(self, app_id=1): """Deletes all TaskRuns for a given app_id""" db.session.query(model.TaskRun).filter_by(app_id=1).delete() db.session.commit() def task_settings_scheduler(self, method="POST", short_name='sampleapp', sched="default"): """Helper function to modify task scheduler""" url = "/app/%s/tasks/scheduler" % short_name if method == "POST": return self.app.post(url, data={ 'sched': sched, }, follow_redirects=True) else: return self.app.get(url, follow_redirects=True) def task_settings_redundancy(self, method="POST", short_name='sampleapp', n_answers=30): """Helper function to modify task redundancy""" url = "/app/%s/tasks/redundancy" % short_name if method == "POST": return self.app.post(url, data={ 'n_answers': n_answers, }, follow_redirects=True) else: return self.app.get(url, follow_redirects=True) def task_settings_priority(self, method="POST", short_name='sampleapp', task_ids="1", priority_0=0.0): """Helper function to modify task redundancy""" url = "/app/%s/tasks/priority" % short_name if method == "POST": return self.app.post(url, data={ 'task_ids': task_ids, 'priority_0': priority_0 }, follow_redirects=True) else: return self.app.get(url, follow_redirects=True) def delete_application(self, method="POST", short_name="sampleapp"): """Helper function to create an application""" if method == "POST": return self.app.post("/app/%s/delete" % short_name, follow_redirects=True) else: return self.app.get("/app/%s/delete" % short_name, follow_redirects=True) def update_application(self, method="POST", short_name="sampleapp", id=1, new_name="Sample App", new_short_name="sampleapp", new_description="Description", new_thumbnail="New Icon link", new_allow_anonymous_contributors="False", new_category_id="2", new_long_description="Long desc", new_sched="random", new_hidden=False): """Helper function to create an application""" if method == "POST": if new_hidden: return self.app.post("/app/%s/update" % short_name, data={ 'id': id, 'name': new_name, 'short_name': new_short_name, 'description': new_description, 'thumbnail': new_thumbnail, 'allow_anonymous_contributors': new_allow_anonymous_contributors, 'category_id': new_category_id, 'long_description': new_long_description, 'sched': new_sched, 'hidden': new_hidden }, follow_redirects=True) else: return self.app.post("/app/%s/update" % short_name, data={ 'id': id, 'name': new_name, 'short_name': new_short_name, 'thumbnail': new_thumbnail, 'allow_anonymous_contributors': new_allow_anonymous_contributors, 'category_id': new_category_id, 'long_description': new_long_description, 'sched': new_sched, 'description': new_description }, follow_redirects=True) else: return self.app.get("/app/%s/update" % short_name, follow_redirects=True)