diff options
Diffstat (limited to 'bitbake/lib/toaster/toastergui/views.py')
-rwxr-xr-x | bitbake/lib/toaster/toastergui/views.py | 48 |
1 files changed, 36 insertions, 12 deletions
diff --git a/bitbake/lib/toaster/toastergui/views.py b/bitbake/lib/toaster/toastergui/views.py index a57f001459..8fbe8a3640 100755 --- a/bitbake/lib/toaster/toastergui/views.py +++ b/bitbake/lib/toaster/toastergui/views.py | |||
@@ -22,11 +22,13 @@ | |||
22 | import operator,re | 22 | import operator,re |
23 | 23 | ||
24 | from django.db.models import Q, Sum | 24 | from django.db.models import Q, Sum |
25 | from django.db import IntegrityError | ||
25 | from django.shortcuts import render, redirect | 26 | from django.shortcuts import render, redirect |
26 | from orm.models import Build, Target, Task, Layer, Layer_Version, Recipe, LogMessage, Variable | 27 | from orm.models import Build, Target, Task, Layer, Layer_Version, Recipe, LogMessage, Variable |
27 | from orm.models import Task_Dependency, Recipe_Dependency, Package, Package_File, Package_Dependency | 28 | from orm.models import Task_Dependency, Recipe_Dependency, Package, Package_File, Package_Dependency |
28 | from orm.models import Target_Installed_Package, Target_File, Target_Image_File | 29 | from orm.models import Target_Installed_Package, Target_File, Target_Image_File |
29 | from django.views.decorators.cache import cache_control | 30 | from django.views.decorators.cache import cache_control |
31 | from django.core.urlresolvers import reverse | ||
30 | from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger | 32 | from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger |
31 | from django.http import HttpResponseBadRequest | 33 | from django.http import HttpResponseBadRequest |
32 | from django.utils import timezone | 34 | from django.utils import timezone |
@@ -68,7 +70,6 @@ def _verify_parameters(g, mandatory_parameters): | |||
68 | 70 | ||
69 | def _redirect_parameters(view, g, mandatory_parameters, *args, **kwargs): | 71 | def _redirect_parameters(view, g, mandatory_parameters, *args, **kwargs): |
70 | import urllib | 72 | import urllib |
71 | from django.core.urlresolvers import reverse | ||
72 | url = reverse(view, kwargs=kwargs) | 73 | url = reverse(view, kwargs=kwargs) |
73 | params = {} | 74 | params = {} |
74 | for i in g: | 75 | for i in g: |
@@ -1772,6 +1773,16 @@ if toastermain.settings.MANAGED: | |||
1772 | from django.contrib.auth import authenticate, login | 1773 | from django.contrib.auth import authenticate, login |
1773 | from django.contrib.auth.decorators import login_required | 1774 | from django.contrib.auth.decorators import login_required |
1774 | 1775 | ||
1776 | import traceback | ||
1777 | |||
1778 | class BadParameterException(Exception): pass # error thrown on invalid POST requests | ||
1779 | |||
1780 | # the context processor that supplies data used across all the pages | ||
1781 | def managedcontextprocessor(request): | ||
1782 | return { | ||
1783 | "projects": Project.objects.all(), | ||
1784 | "MANAGED" : toastermain.settings.MANAGED | ||
1785 | } | ||
1775 | 1786 | ||
1776 | # new project | 1787 | # new project |
1777 | def newproject(request): | 1788 | def newproject(request): |
@@ -1787,28 +1798,41 @@ if toastermain.settings.MANAGED: | |||
1787 | return render(request, template, context) | 1798 | return render(request, template, context) |
1788 | elif request.method == "POST": | 1799 | elif request.method == "POST": |
1789 | mandatory_fields = ['projectname', 'email', 'username', 'projectversion'] | 1800 | mandatory_fields = ['projectname', 'email', 'username', 'projectversion'] |
1790 | if reduce( lambda x, y: x and y, map(lambda x: x in request.POST and len(request.POST[x]) > 0, mandatory_fields)): | 1801 | try: |
1802 | # make sure we have values for all mandatory_fields | ||
1803 | if reduce( lambda x, y: x or y, map(lambda x: len(request.POST.get(x, '')) == 0, mandatory_fields)): | ||
1804 | # set alert for missing fields | ||
1805 | raise BadParameterException("Fields missing: " + | ||
1806 | ", ".join([x for x in mandatory_fields if len(request.POST.get(x, '')) == 0 ])) | ||
1807 | |||
1791 | if not request.user.is_authenticated(): | 1808 | if not request.user.is_authenticated(): |
1792 | user = authenticate(username = request.POST['username'], password = 'nopass') | 1809 | user = authenticate(username = request.POST['username'], password = 'nopass') |
1793 | if user is None: | 1810 | if user is None: |
1794 | user = User.objects.create_user(username = request.POST['username'], email = request.POST['email'], password = "nopass") | 1811 | user = User.objects.create_user(username = request.POST['username'], email = request.POST['email'], password = "nopass") |
1795 | raise Exception("User cannot be authed, creating") | 1812 | |
1796 | user = authenticate(username = request.POST['username'], password = '') | 1813 | user = authenticate(username = user.username, password = 'nopass') |
1797 | login(request, user) | 1814 | login(request, user) |
1798 | 1815 | ||
1799 | return redirect(project) | 1816 | # save the project |
1800 | else: | 1817 | prj = Project.objects.create_project(name = request.POST['projectname'], |
1801 | alerts = [] | 1818 | branch = request.POST['projectversion'].split(" ")[0], |
1802 | # set alerts for missing fields | 1819 | short_description=request.POST['projectversion'].split(" ")[1:]) |
1803 | map(lambda x: alerts.append('Field '+ x + ' not filled in') if not x in request.POST or len(request.POST[x]) == 0 else None, mandatory_fields) | 1820 | prj.user_id = request.user.pk |
1804 | # fill in new page with already submitted values | 1821 | prj.save() |
1822 | return redirect(reverse(project, args = (prj.pk,))) | ||
1823 | |||
1824 | except (IntegrityError, BadParameterException) as e: | ||
1825 | # fill in page with previously submitted values | ||
1805 | map(lambda x: context.__setitem__(x, request.POST[x]), mandatory_fields) | 1826 | map(lambda x: context.__setitem__(x, request.POST[x]), mandatory_fields) |
1806 | context['alerts'] = alerts | 1827 | if isinstance(e, IntegrityError) and "username" in str(e): |
1828 | context['alert'] = "Your chosen username is already used" | ||
1829 | else: | ||
1830 | context['alert'] = str(e) | ||
1807 | return render(request, template, context) | 1831 | return render(request, template, context) |
1808 | raise Exception("Invalid HTTP method for this page") | 1832 | raise Exception("Invalid HTTP method for this page") |
1809 | 1833 | ||
1810 | # Shows the edit project page | 1834 | # Shows the edit project page |
1811 | def project(request): | 1835 | def project(request, pid): |
1812 | template = "project.html" | 1836 | template = "project.html" |
1813 | context = {} | 1837 | context = {} |
1814 | return render(request, template, context) | 1838 | return render(request, template, context) |