From e5c0bbc48086806d79aac2ebfb226be61ace6115 Mon Sep 17 00:00:00 2001
From: Flavio Tarsetti <flavio.tarsetti@idiap.ch>
Date: Thu, 27 Jul 2017 17:04:18 +0200
Subject: [PATCH] [accounts] static/templates/templatetags/views added first
 account information for valid user and implemented yearly revalidation
 functionality

---
 .../accounts/static/accounts/css/dialogs.css  |  46 ++++++++
 .../accounts/static/accounts/js/dialogs.js    | 103 ++++++++++++++++++
 .../accounts/dialogs/renew_account.html       |  29 +++++
 .../accounts/templates/accounts/settings.html |  75 +++++++++++++
 beat/web/accounts/templatetags/__init__.py    |   0
 .../web/accounts/templatetags/account_tags.py |  40 +++++++
 beat/web/accounts/views.py                    |  13 +++
 7 files changed, 306 insertions(+)
 create mode 100644 beat/web/accounts/static/accounts/css/dialogs.css
 create mode 100644 beat/web/accounts/static/accounts/js/dialogs.js
 create mode 100644 beat/web/accounts/templates/accounts/dialogs/renew_account.html
 create mode 100644 beat/web/accounts/templatetags/__init__.py
 create mode 100644 beat/web/accounts/templatetags/account_tags.py

diff --git a/beat/web/accounts/static/accounts/css/dialogs.css b/beat/web/accounts/static/accounts/css/dialogs.css
new file mode 100644
index 000000000..14ea486ea
--- /dev/null
+++ b/beat/web/accounts/static/accounts/css/dialogs.css
@@ -0,0 +1,46 @@
+/****************** ACCOUNT DIALOG ******************************/
+
+div.renew_account .progress
+{
+    margin: 40px auto;
+}
+
+
+div.renew_account #infos
+{
+    text-align: center;
+    display: block;
+    margin-top: 10px;
+    margin-bottom: 10px;
+    color: #777777;
+}
+
+
+div.renew_account span.label
+{
+    display: inline-block;
+    width: 15%;
+    color: black;
+    font-size: 1em;
+}
+
+
+div.renew_account input#prefix,
+div.renew_account input#suffix
+{
+    display: inline-block;
+    width: 79%;
+}
+
+
+div.renew_account input#suffix
+{
+    margin-top: 10px;
+}
+
+
+div.renew_account span.documentation
+{
+    color: #AAAAAA;
+    font-size: 0.8em;
+}
diff --git a/beat/web/accounts/static/accounts/js/dialogs.js b/beat/web/accounts/static/accounts/js/dialogs.js
new file mode 100644
index 000000000..78e3d728f
--- /dev/null
+++ b/beat/web/accounts/static/accounts/js/dialogs.js
@@ -0,0 +1,103 @@
+/*
+ * Copyright (c) 2016 Idiap Research Institute, http://www.idiap.ch/
+ * Contact: beat.support@idiap.ch
+ * 
+ * This file is part of the beat.web module of the BEAT platform.
+ * 
+ * Commercial License Usage
+ * Licensees holding valid commercial BEAT licenses may use this file in
+ * accordance with the terms contained in a written agreement between you
+ * and Idiap. For further information contact tto@idiap.ch
+ * 
+ * Alternatively, this file may be used under the terms of the GNU Affero
+ * Public License version 3 as published by the Free Software and appearing
+ * in the file LICENSE.AGPL included in the packaging of this file.
+ * The BEAT platform is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.
+ * 
+ * You should have received a copy of the GNU Affero Public License along
+ * with the BEAT platform. If not, see http://www.gnu.org/licenses/.
+*/
+/**
+ * beat.accounts.dialogs.js
+ * Implementation of experiment-related dialogs
+ */
+if (beat === undefined) var beat = {};
+if (beat.accounts === undefined) beat.accounts = {};
+if (beat.accounts.dialogs === undefined) beat.accounts.dialogs = {};
+
+/**
+ * Implements a modal dialog to renew a user account.
+ * The user is presented with a small modal window that will allow
+ * him to set the new experiment name. It is possible to cancel the action at
+ * any moment by pressing "Cancel" or hitting the ESC key.
+ *
+ * Parameters:
+ *
+ *   dialog_id: ID of the DOM element to use
+ *   api_url (str): The URL towards the API for renewing the account
+ *   redirect_url (str): The URL where to redirect the user if the operation is
+ *       successful
+ */
+beat.accounts.dialogs.modal_renew_account = function(dialog_id, api_url, redirect_url) {
+
+  // Create the dialog
+  $('#' + dialog_id).dialog({
+    autoOpen: false,
+    resizable: false,
+    width: Math.min($(window).width() * 0.6, 700),
+    position: { my: "center", at: "center", of: window },
+    modal: true,
+    closeOnEscape: true,
+    buttons: [
+    {
+      id: 'button-' + dialog_id + '-renameit',
+      text: 'Revalidate account',
+      click: function() {
+
+        $.ajaxSetup({
+          beforeSend: function(xhr, settings) {
+            var csrftoken = $.cookie('csrftoken');
+            xhr.setRequestHeader('X-CSRFToken', csrftoken);
+          }
+        });
+
+        $.ajax({
+          type: "PUT",
+          url: api_url /*+ experiment_fullname  + '/' */,
+          data: JSON.stringify({
+            name: name,
+          }),
+          contentType: "application/json; charset=utf-8",
+          dataType: "json",
+
+          success: function(data) {
+            $('#' + dialog_id).dialog("close");
+            window.location.href = redirect_url;
+          },
+
+          error: function(jqXHR, textStatus, errorThrown) {
+            if ((jqXHR.status == 400) && (jqXHR.responseText.length > 0))
+              alert(jqXHR.responseText);
+            else
+              alert('Error: ' + errorThrown);
+
+            $(this).dialog("close");
+          }
+        });
+      }
+    },
+    {
+      id: 'button-' + dialog_id + '-cancel',
+      text: 'Cancel',
+      click: function() {
+        $(this).dialog("close");
+      }
+    }
+    ]
+  });
+
+  // Display the dialog
+  $('#' + dialog_id).dialog("open");
+}
diff --git a/beat/web/accounts/templates/accounts/dialogs/renew_account.html b/beat/web/accounts/templates/accounts/dialogs/renew_account.html
new file mode 100644
index 000000000..e94afdb87
--- /dev/null
+++ b/beat/web/accounts/templates/accounts/dialogs/renew_account.html
@@ -0,0 +1,29 @@
+{% comment %}
+ * Copyright (c) 2016 Idiap Research Institute, http://www.idiap.ch/
+ * Contact: beat.support@idiap.ch
+ * 
+ * This file is part of the beat.web module of the BEAT platform.
+ * 
+ * Commercial License Usage
+ * Licensees holding valid commercial BEAT licenses may use this file in
+ * accordance with the terms contained in a written agreement between you
+ * and Idiap. For further information contact tto@idiap.ch
+ * 
+ * Alternatively, this file may be used under the terms of the GNU Affero
+ * Public License version 3 as published by the Free Software and appearing
+ * in the file LICENSE.AGPL included in the packaging of this file.
+ * The BEAT platform is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.
+ * 
+ * You should have received a copy of the GNU Affero Public License along
+ * with the BEAT platform. If not, see http://www.gnu.org/licenses/.
+{% endcomment %}
+
+<div id="{{ dialog_id }}" class="renew_account" style="display:none;" title="Account revalidation">
+    <div class="settings">
+        <div><span id="infos"></span></div>
+        By revalidating your account you acknowledge that you wish to continue using the platform and your
+        supervision is still valid.
+    </div>
+</div>
diff --git a/beat/web/accounts/templates/accounts/settings.html b/beat/web/accounts/templates/accounts/settings.html
index 6b00af350..b760f7318 100644
--- a/beat/web/accounts/templates/accounts/settings.html
+++ b/beat/web/accounts/templates/accounts/settings.html
@@ -25,9 +25,27 @@
 {% load ui_tags %}
 {% load gravatar %}
 {% load registration_tags %}
+{% load account_tags %}
 
 {% block title %}Settings for {{ request.user.first_name }} ({{ request.user.username }}){% endblock %}
 
+{% block stylesheets %}
+{{ block.super }}
+<link rel="stylesheet" href="{% fingerprint "accounts/css/dialogs.css" %}" type="text/css" media="screen" />
+<link rel="stylesheet" href="{% fingerprint "jquery-ui/themes/base/minified/jquery-ui.min.css" %}" type="text/css" media="screen" />
+{% endblock %}
+
+{% block scripts %}
+{{ block.super }}
+{% csrf_token %}
+<script src="{% fingerprint "accounts/js/dialogs.js" %}" type="text/javascript" charset="utf-8"></script>
+<script src="{% fingerprint "jquery-ui/ui/minified/jquery.ui.core.min.js" %}" type="text/javascript" charset="utf-8"></script>
+<script src="{% fingerprint "jquery-ui/ui/minified/jquery.ui.position.min.js" %}" type="text/javascript" charset="utf-8"></script>
+<script src="{% fingerprint "jquery-ui/ui/minified/jquery.ui.widget.min.js" %}" type="text/javascript" charset="utf-8"></script>
+<script src="{% fingerprint "jquery-ui/ui/minified/jquery.ui.button.min.js" %}" type="text/javascript" charset="utf-8"></script>
+<script src="{% fingerprint "jquery-ui/ui/minified/jquery.ui.dialog.min.js" %}" type="text/javascript" charset="utf-8"></script>
+{% endblock %}
+
 {% block content %}
 <div class="row">
   <div class="col-sm-6 col-sm-offset-3">
@@ -178,6 +196,63 @@
   </div>
 </div>
 
+<div class="row">
+
+  <div class="col-sm-4 col-sm-offset-1">
+
+    <div class="panel panel-default">
+      <div class="panel-heading"><i class="fa fa-credit-card-alt"></i> Account management
+          {% if user.profile.status == 'A' %}
+          <span class="label label-success" style="float:right">Status Accepted</span>
+          {% elif user.profile.status == 'R' %}
+          <span class="label label-danger" style="float:right">Status Rejected</span>
+          {% elif user.profile.status == 'Y' %}
+          <span class="label label-warning" style="float:right">Status Revalidation Required</span>
+          {% endif %}
+      </div>
+      <div class="panel-body">
+        <form id="token" method="post" action="" class="form">
+          {% csrf_token %}
+          <div class="form-group" style="text-align: justify;">
+          {% if user.profile.status == 'A' %}
+          <li class="list-group-item list-group-item-success" style="text-align:center;">Status is currently accepted: No action is directly required</li>
+          <br>
+          <h4>General Information</h4>
+          <ul class="list-group">
+            <li class="list-group-item"><b>Supervisor:</b> {{ supervisiontrack.godfather.username }}</li>
+            <li class="list-group-item"><b>Supervision Track started on:</b> {{ supervisiontrack.start_date }}</li>
+            <li class="list-group-item"><b>Supervision Track last validation date:</b> {{ supervisiontrack.last_validation_date }}</li>
+            <li class="list-group-item"><b>Supervision Track expiration date:</b> {{ supervisiontrack.expiration_date }}</li>
+          </ul>
+          {% elif user.profile.status == 'R' %}
+          <li class="list-group-item list-group-item-warning" style="text-align:center;">Rejected: You need to find a new supervisor</li>
+          <br>
+          {% elif user.profile.status == 'Y' %}
+          <li class="list-group-item list-group-item-warning" style="text-align:center;">Revalidation required: You need to confirm that you wish to revalidate your account (year basis check)</li>
+          <br>
+          <h4>General Information</h4>
+          <ul class="list-group">
+            <li class="list-group-item"><b>Supervisor:</b> {{ supervisiontrack.godfather.username }}</li>
+            <li class="list-group-item"><b>Supervision Track started on:</b> {{ supervisiontrack.start_date }}</li>
+            <li class="list-group-item"><b>Supervision Track last validation date:</b> {{ supervisiontrack.last_validation_date }}</li>
+            <li class="list-group-item"><b>Supervision Track expiration date:</b> {{ supervisiontrack.expiration_date }}</li>
+          </ul>
+          <div class="form-group" style="text-align: center;">
+          <input type="hidden" name="token" value="true">
+            <button type="button" class="btn btn-warning" onclick="beat.accounts.dialogs.modal_renew_account('account_renew', '{% url 'api_accounts:revalidate_account' %}', '{% url 'accounts:settings' %}');"><i class="fa fa-refresh fa-fw fa-lg"></i> Revalidate account for 12 months</button>
+          </div>
+          {% endif %}
+          </div>
+        </form>
+      </div>
+
+    </div>
+
+  </div>
+</div>
+
+{% account_renew "account_renew" %}
+
 <script type="text/javascript">
 jQuery(document).ready(function() {
   $('form#token').submit(function(event) {
diff --git a/beat/web/accounts/templatetags/__init__.py b/beat/web/accounts/templatetags/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/beat/web/accounts/templatetags/account_tags.py b/beat/web/accounts/templatetags/account_tags.py
new file mode 100644
index 000000000..4fa3958c0
--- /dev/null
+++ b/beat/web/accounts/templatetags/account_tags.py
@@ -0,0 +1,40 @@
+#!/usr/bin/env python
+# vim: set fileencoding=utf-8 :
+
+###############################################################################
+#                                                                             #
+# Copyright (c) 2016 Idiap Research Institute, http://www.idiap.ch/           #
+# Contact: beat.support@idiap.ch                                              #
+#                                                                             #
+# This file is part of the beat.web module of the BEAT platform.              #
+#                                                                             #
+# Commercial License Usage                                                    #
+# Licensees holding valid commercial BEAT licenses may use this file in       #
+# accordance with the terms contained in a written agreement between you      #
+# and Idiap. For further information contact tto@idiap.ch                     #
+#                                                                             #
+# Alternatively, this file may be used under the terms of the GNU Affero      #
+# Public License version 3 as published by the Free Software and appearing    #
+# in the file LICENSE.AGPL included in the packaging of this file.            #
+# The BEAT platform is distributed in the hope that it will be useful, but    #
+# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY  #
+# or FITNESS FOR A PARTICULAR PURPOSE.                                        #
+#                                                                             #
+# You should have received a copy of the GNU Affero Public License along      #
+# with the BEAT platform. If not, see http://www.gnu.org/licenses/.           #
+#                                                                             #
+###############################################################################
+
+
+import random
+
+from django import template
+from django.conf import settings
+
+register = template.Library()
+
+
+@register.inclusion_tag('accounts/dialogs/renew_account.html')
+def account_renew(id):
+    return { 'dialog_id': id,
+           }
diff --git a/beat/web/accounts/views.py b/beat/web/accounts/views.py
index 4e4917de0..a3eab03a5 100644
--- a/beat/web/accounts/views.py
+++ b/beat/web/accounts/views.py
@@ -29,11 +29,13 @@ from django.shortcuts import render
 from django.contrib.auth.decorators import login_required
 from django.contrib.auth.forms import PasswordChangeForm
 from django.contrib import messages
+from django.db import models
 
 from rest_framework.authtoken.models import Token
 
 from .forms import AccountSettingsForm
 from .models import AccountSettings
+from .models import SupervisionTrack, Profile
 
 @login_required
 def account_settings(request):
@@ -65,8 +67,19 @@ def account_settings(request):
         account_settings_form = AccountSettingsForm(instance=account_settings)
         password_change_form = PasswordChangeForm(user=user)
 
+    supervisiontrack = None
+    if user.profile.is_godfather == False:
+        supervisee = user
+        if supervisee.profile.supervision_key is not None:
+            #There's a key check if there's a valid track
+            supervisiontrack = SupervisionTrack.objects.get(supervisee=supervisee, supervisee__profile__supervision_key=models.F('supervision_key'))
+        else:
+            supervisiontrack = None
+
     return render(request,
                   'accounts/settings.html',
                   {'account_settings_form': account_settings_form,
                    'password_change_form': password_change_form,
+                   'user': user,
+                   'supervisiontrack': supervisiontrack,
                    'token' : user.auth_token})
-- 
GitLab