From ced1a8761a46fcc3da7af5bfbbad57be636da1fe Mon Sep 17 00:00:00 2001 From: Stauros Kroustouris <staurosk@noc.grnet.gr> Date: Fri, 5 Jun 2015 16:59:52 +0300 Subject: [PATCH] Ui to generate auth token via the profile page --- README.md | 11 ++-- accounts/views.py | 12 +++- static/js/accounts.js | 17 +++++ templates/profile.html | 141 +++++++++++++++++++++-------------------- 4 files changed, 109 insertions(+), 72 deletions(-) create mode 100644 static/js/accounts.js diff --git a/README.md b/README.md index 205ce94..9dff337 100644 --- a/README.md +++ b/README.md @@ -51,17 +51,20 @@ Here are some examples: #### GET items - List all the rules your user has created (admin users can see all the rules) - curl -X GET https://fod.example.com/api/routes/ -H 'Authorization: Token <Your users token>' + + curl -X GET https://fod.example.com/api/routes/ -H 'Authorization: Token <Your users token>' - Retrieve a specific rule: - curl -X GET https:/fod.example.com/api/routes/<rule_id>/ -H 'Authorization: Token <Your users token>' + + curl -X GET https:/fod.example.com/api/routes/<rule_id>/ -H 'Authorization: Token <Your users token>' - In order to create or modify a rule you have to use POST/PUT methods. -#### POST/PUT items +#### POST/PUT rules +In order to update or create rules you can follow this example: ##### Foreign Keys -In order to create/modify a rule you have to connect the rule with: +In order to create/modify a rule you have to connect the rule with some foreign keys: ###### Ports, Fragmentypes, protocols, thenactions When creating a rule, one can specify: diff --git a/accounts/views.py b/accounts/views.py index a86b74e..e71d3a8 100644 --- a/accounts/views.py +++ b/accounts/views.py @@ -21,17 +21,27 @@ from django.conf import settings from django.core.mail import send_mail from django.contrib.sites.models import Site from django.contrib.auth.models import User -from django.shortcuts import render +from django.shortcuts import render, HttpResponse from django.template.loader import render_to_string from django.utils.translation import ugettext_lazy as _ from django.views.decorators.cache import never_cache +from rest_framework.authtoken.models import Token from accounts.models import UserProfile from peers.models import Peer from flowspec.forms import UserProfileForm from registration.models import RegistrationProfile +def generate_token(request): + user = request.user + try: + token = user.auth_token + except Token.DoesNotExist: + token = Token.objects.create(user=request.user) + return HttpResponse(token) + + @never_cache def activate(request, activation_key): account = None diff --git a/static/js/accounts.js b/static/js/accounts.js new file mode 100644 index 0000000..f015058 --- /dev/null +++ b/static/js/accounts.js @@ -0,0 +1,17 @@ +$(document).ready(function() { + $('a#generate_token').one('click', function (ev) { + ev.preventDefault(); + var url = $(this).prop('href'); + var result = $(this).parent() + result.text('loading...'); + $.ajax({ + url: url, + success: function (data) { + result.text(data); + }, + }) + .fail(function() { + result.text('An error has occured...'); + }); + }); +}); diff --git a/templates/profile.html b/templates/profile.html index 4fdd703..458f4de 100644 --- a/templates/profile.html +++ b/templates/profile.html @@ -1,80 +1,87 @@ {% extends "base.html" %} {% load i18n %} +{% load staticfiles %} {% block title %}{% trans "My profile" %}{% endblock %} {% block contentplaceholder %} - <div class="row"> - <div class="col-lg-12"> - <h1 class="page-header">{% trans "My profile" %}</h1> - </div> - <!-- /.col-lg-12 --> - </div> - <div class="row"> - <div class="col-md-8"> - <div class="panel panel-primary"> - <div class="panel-heading"> - <i class="fa fa-bolt"></i> {% trans "My Networks" %} - </div> - <!-- /.panel-heading --> - <div class="panel-body"> - <table class="table table-striped table-bordered"> - <thead> - <tr> - <th>{% trans "Organization" %}</th><th>{% trans "Networks" %}</th> - </tr> - </thead> - {% for peer in peers %} - <tr> - <td>{{peer}} </td> - <td> - {% for network in peer.networks.all %} - {{network}}<br> - {% empty %} - <span style="color:red">{% blocktrans %}Ooops! Seems you have no networks associated with your peer. Contact Helpdesk to resolve this issue.{% endblocktrans %}</span> - {% endfor %} - </td></tr> - {% endfor %} - </table> - </div> - </div> - </div> + <div class="col-lg-12"> + <h1 class="page-header">{% trans "My profile" %}</h1> + </div> + <!-- /.col-lg-12 --> +</div> - <div class="col-md-4"> - <div class="panel panel-default"> - <div class="panel-heading"> - <i class="fa fa-tags"></i> Shortcuts - </div> - <!-- /.panel-heading --> -<div class="panel-body"> +<div class="row"> + <div class="col-md-8"> + <div class="panel panel-primary"> + <div class="panel-heading"> + <i class="fa fa-bolt"></i> {% trans "My Networks" %} + </div> + <!-- /.panel-heading --> + <div class="panel-body"> + <table class="table table-striped table-bordered"> + <thead> + <tr> + <th>{% trans "Organization" %}</th><th>{% trans "Networks" %}</th> + </tr> + </thead> + {% for peer in peers %} + <tr> + <td>{{peer}} </td> + <td> + {% for network in peer.networks.all %} + {{network}}<br> + {% empty %} + <span style="color:red">{% blocktrans %}Ooops! Seems you have no networks associated with your peer. Contact Helpdesk to resolve this issue.{% endblocktrans %}</span> + {% endfor %} + </td> + </tr> + {% endfor %} + </table> + </div> + </div> + </div> + <div class="col-md-4"> + <div class="panel panel-default"> + <div class="panel-heading"> + <i class="fa fa-tags"></i> Shortcuts + </div> + <!-- /.panel-heading --> + <div class="panel-body"> <a class="btn btn-sm btn-outline btn-success" id="routebutton" href="{% url add-route %}"><i class="fa fa-plus-circle"></i> {% trans "Add Rule" %}</a> <a class="btn btn-sm btn-outline btn-primary" href="{% url group-routes %}"><i class="fa fa-shield fa-fw"></i> {% trans "My Rules" %}</a> <a class="btn btn-sm btn-outline btn-info" href="{% url dashboard %}"><i class="fa fa-dashboard"></i> {% trans "Dashboard" %}</a> - </div> - <!-- /.panel-body --> - </div> - - <div class="panel panel-info"> - <div class="panel-heading"> - <i class="fa fa-user"></i> {% trans "My Profile" %} - </div> - <!-- /.panel-heading --> - <div class="panel-body"> - <dl class="dl-horizontal"> - <dt>{% trans "Username" %}:</dt><dd>{{user.username}}</dd> - <dt>{% trans "First name" %}:</dt><dd>{{user.first_name}}</dd> - <dt>{% trans "Last name" %}:</dt><dd>{{user.last_name}}</dd> - <dt>{% trans "Email" %}:</dt><dd>{{user.email}}</dd> - </dl> - </div> - </div> - </div> - </div> - - - + </div> + <!-- /.panel-body --> + </div> + <div class="panel panel-info"> + <div class="panel-heading"> + <i class="fa fa-user"></i> {% trans "My Profile" %} + </div> + <!-- /.panel-heading --> + <div class="panel-body"> + <dl class="dl-horizontal"> + <dt>{% trans "Username" %}:</dt> + <dd>{{user.username}}</dd> + <dt>{% trans "First name" %}:</dt><dd>{{user.first_name}}</dd> + <dt>{% trans "Last name" %}:</dt><dd>{{user.last_name}}</dd> + <dt>{% trans "Email" %}:</dt><dd>{{user.email}}</dd> + <dt>{% trans "Api Token" %}:</dt> + <dd> + {% if user.auth_token %} + {{ user.auth_token }} + {% else %} + <a id="generate_token" href="{% url user-profile-token %}">{% trans "Generate One" %}</a> + {% endif %} + </dd> + </dl> + </div> + </div> + </div> +</div> {% endblock %} - - +{% block pagejs %} + <script type="text/javascript" src="{% static 'js/accounts.js' %}"></script> +{% endblock %} -- GitLab