Patchwork [bitbake-devel,6/9] toaster: add automated login in new project page

login
register
mail settings
Submitter Alexandru DAMIAN
Date July 4, 2014, 12:44 p.m.
Message ID <a9062d9692525e24e59b5b2bb4dfdef90b41bf2a.1404477729.git.alexandru.damian@intel.com>
Download mbox | patch
Permalink /patch/75029/
State New
Headers show

Comments

Alexandru DAMIAN - July 4, 2014, 12:44 p.m.
From: Alexandru DAMIAN <alexandru.damian@intel.com>

Toaster uses the Django authentication system to assign
user accounts to the projects that are being created.

In the current implementation, the user accounts are
created/authenticated automatically, on the fly, based
on the fields specified in the create new project page.

Signed-off-by: Alexandru DAMIAN <alexandru.damian@intel.com>
---
 lib/toaster/toastergui/templates/newproject.html | 35 +++++++++++++-----------
 lib/toaster/toastergui/views.py                  | 29 ++++++++++++++++++--
 2 files changed, 46 insertions(+), 18 deletions(-)

Patch

diff --git a/lib/toaster/toastergui/templates/newproject.html b/lib/toaster/toastergui/templates/newproject.html
index e5a6551..ce01800 100644
--- a/lib/toaster/toastergui/templates/newproject.html
+++ b/lib/toaster/toastergui/templates/newproject.html
@@ -6,36 +6,39 @@ 
 				<div class="span6">
 				<div class="page-header">
 					<h1>Create a new project</h1>
-				</div>  
-					<form>
+				</div>
+				<div class="container-fluid">
+		{% for a in alerts %}
+			<div class="alert alert-error row-fluid" role="alert">{{a}}</div>
+		{% endfor %}
+				</div>
+					<form method="POST">{% csrf_token %}
 						<fieldset>
 							<label>Project name <span class="muted">(required)</span></label>
-							<input type="text" class="input-xlarge" required name="projectname">
+							<input type="text" class="input-xlarge" required name="projectname" value="{{projectname}}">
 							<label class="project-form">
 								Project owner
-								<i class="icon-question-sign get-help" title="The go-to person for this project"></i>
+					<i class="icon-question-sign get-help" title="The go-to person for this project"></i>
 							</label>
-				<form method="POST">
-							<input type="text">
+							<input type="text" name="username" value="{{username}}">
 							<label class="project-form">Owner's email</label>
-							<input type="email" class="input-large" name="email">
+							<input type="email" class="input-large" name="email" value="{{email}}">
 							<label class="project-form">
 								Yocto Project version
 								<i class="icon-question-sign get-help" title="This sets the branch for the Yocto Project core layers (meta, meta-yocto and meta-yocto-bsp), and for the layers you use from the OpenEmbedded Metadata Index"></i>
 							</label>
-							<select>
-								<option>Yocto Project 1.7 "D?"</option>
-								<option>Yocto Project 1.6 "Daisy"</option>
-								<option>Yocto Project 1.5 "Dora"</option>
+							<select name="projectversion">
+		<option value="1.7" {%if projectversion == "1.7" %}selected{%endif%}>Yocto Project 1.7 "D?"</option>
+		<option value="1.6" {%if projectversion == "1.6" %}selected{%endif%}>Yocto Project 1.6 "Daisy"</option>
+		<option value="1.5" {%if projectversion == "1.5" %}selected{%endif%}>Yocto Project 1.5 "Dora"</option>
 							</select>
-				</form>
 						</fieldset>
-									
+
 						<div class="form-actions">
-							<a href="project-with-targets.html" class="btn btn-primary btn-large">Create project</a>
+							<input type="submit" class="btn btn-primary btn-large" value="Create project"/>
 						</div>
-					</form> 
+					</form>
 				</div>
 			</div>
-		</div>  
+		</div>
 {% endblock %}
diff --git a/lib/toaster/toastergui/views.py b/lib/toaster/toastergui/views.py
index 7dc0108..89c02d4 100755
--- a/lib/toaster/toastergui/views.py
+++ b/lib/toaster/toastergui/views.py
@@ -1768,17 +1768,42 @@  def managedcontextprocessor(request):
 # a default "page not available" simple functions for interactive mode
 if toastermain.settings.MANAGED:
 
+    from django.contrib.auth.models import User
+    from django.contrib.auth import authenticate, login
+    from django.contrib.auth.decorators import login_required
+
+
     # new project
     def newproject(request):
         template = "newproject.html"
-        context = {}
+        context = {
+            'email': request.user.email if request.user.is_authenticated() else '',
+            'username': request.user.username if request.user.is_authenticated() else '',
+        }
+
+
         if request.method == "GET":
             # render new project page
             return render(request, template, context)
         elif request.method == "POST":
-            if request.method:
+            mandatory_fields = ['projectname', 'email', 'username', 'projectversion']
+            if reduce( lambda x, y: x and y, map(lambda x: x in request.POST and len(request.POST[x]) > 0, mandatory_fields)):
+                if not request.user.is_authenticated():
+                    user = authenticate(username = request.POST['username'], password = 'nopass')
+                    if user is None:
+                        user = User.objects.create_user(username = request.POST['username'], email = request.POST['email'], password = "nopass")
+                        raise Exception("User cannot be authed, creating")
+                        user = authenticate(username = request.POST['username'], password = '')
+                    login(request, user)
+
                 return redirect(project)
             else:
+                alerts = []
+                # set alerts for missing fields
+                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)
+                # fill in new page with already submitted values
+                map(lambda x: context.__setitem__(x, request.POST[x]), mandatory_fields)
+                context['alerts'] = alerts
                 return render(request, template, context)
         raise Exception("Invalid HTTP method for this page")