Statistics
| Branch: | Tag: | Revision:

root / flowspec / views.py @ c1509909

History | View | Annotate | Download (9.1 kB)

1 a3af8464 Leonidas Poulopoulos
# Create your views here.
2 357d48dc Leonidas Poulopoulos
import urllib2
3 357d48dc Leonidas Poulopoulos
import re
4 357d48dc Leonidas Poulopoulos
import socket
5 97e42c7d Leonidas Poulopoulos
import json
6 357d48dc Leonidas Poulopoulos
from django import forms
7 357d48dc Leonidas Poulopoulos
from django.views.decorators.csrf import csrf_exempt
8 9cad4715 Leonidas Poulopoulos
from django.core import urlresolvers
9 97e42c7d Leonidas Poulopoulos
from django.core import serializers
10 357d48dc Leonidas Poulopoulos
from django.contrib.auth.decorators import login_required
11 357d48dc Leonidas Poulopoulos
from django.http import HttpResponseRedirect, HttpResponseForbidden, HttpResponse
12 357d48dc Leonidas Poulopoulos
from django.shortcuts import get_object_or_404, render_to_response
13 357d48dc Leonidas Poulopoulos
from django.core.context_processors import request
14 357d48dc Leonidas Poulopoulos
from django.template.context import RequestContext
15 357d48dc Leonidas Poulopoulos
from django.template.loader import get_template
16 357d48dc Leonidas Poulopoulos
from django.utils import simplejson
17 357d48dc Leonidas Poulopoulos
from django.core.urlresolvers import reverse
18 357d48dc Leonidas Poulopoulos
from django.contrib import messages
19 6ee21ffd Leonidas Poulopoulos
from flowspy.accounts.models import *
20 357d48dc Leonidas Poulopoulos
21 97e42c7d Leonidas Poulopoulos
from django.contrib.auth import authenticate, login
22 97e42c7d Leonidas Poulopoulos
23 3e99e2d1 Leonidas Poulopoulos
from django.forms.models import model_to_dict
24 3e99e2d1 Leonidas Poulopoulos
25 9cad4715 Leonidas Poulopoulos
from flowspy.flowspec.forms import * 
26 357d48dc Leonidas Poulopoulos
from flowspy.flowspec.models import *
27 357d48dc Leonidas Poulopoulos
28 3e99e2d1 Leonidas Poulopoulos
from copy import deepcopy
29 97e42c7d Leonidas Poulopoulos
from flowspy.utils.decorators import shib_required
30 3e99e2d1 Leonidas Poulopoulos
31 9cad4715 Leonidas Poulopoulos
def days_offset(): return datetime.now() + timedelta(days = settings.EXPIRATION_DAYS_OFFSET)
32 9cad4715 Leonidas Poulopoulos
33 3e99e2d1 Leonidas Poulopoulos
@login_required
34 357d48dc Leonidas Poulopoulos
def user_routes(request):
35 357d48dc Leonidas Poulopoulos
    user_routes = Route.objects.filter(applier=request.user)
36 357d48dc Leonidas Poulopoulos
    return render_to_response('user_routes.html', {'routes': user_routes},
37 357d48dc Leonidas Poulopoulos
                              context_instance=RequestContext(request))
38 357d48dc Leonidas Poulopoulos
39 3e99e2d1 Leonidas Poulopoulos
@login_required
40 6ee21ffd Leonidas Poulopoulos
def group_routes(request):
41 97e42c7d Leonidas Poulopoulos
    group_routes = []
42 6ee21ffd Leonidas Poulopoulos
    peer = request.user.get_profile().peer
43 6ee21ffd Leonidas Poulopoulos
    if peer:
44 6ee21ffd Leonidas Poulopoulos
       peer_members = UserProfile.objects.filter(peer=peer)
45 6ee21ffd Leonidas Poulopoulos
       users = [prof.user for prof in peer_members]
46 6ee21ffd Leonidas Poulopoulos
       group_routes = Route.objects.filter(applier__in=users)
47 6ee21ffd Leonidas Poulopoulos
    return render_to_response('user_routes.html', {'routes': group_routes},
48 6ee21ffd Leonidas Poulopoulos
                              context_instance=RequestContext(request))
49 6ee21ffd Leonidas Poulopoulos
50 6ee21ffd Leonidas Poulopoulos
51 6ee21ffd Leonidas Poulopoulos
@login_required
52 9cad4715 Leonidas Poulopoulos
def add_route(request):
53 97e42c7d Leonidas Poulopoulos
    applier = request.user.pk
54 9cad4715 Leonidas Poulopoulos
    if request.method == "GET":
55 9cad4715 Leonidas Poulopoulos
        form = RouteForm()
56 97e42c7d Leonidas Poulopoulos
        return render_to_response('apply.html', {'form': form, 'applier': applier},
57 9cad4715 Leonidas Poulopoulos
                                  context_instance=RequestContext(request))
58 9cad4715 Leonidas Poulopoulos
59 9cad4715 Leonidas Poulopoulos
    else:
60 9cad4715 Leonidas Poulopoulos
        form = RouteForm(request.POST)
61 9cad4715 Leonidas Poulopoulos
        if form.is_valid():
62 9cad4715 Leonidas Poulopoulos
            route=form.save(commit=False)
63 9cad4715 Leonidas Poulopoulos
            route.applier = request.user
64 9cad4715 Leonidas Poulopoulos
            route.expires = days_offset()
65 97e42c7d Leonidas Poulopoulos
            route.status = "PENDING"
66 9cad4715 Leonidas Poulopoulos
            route.save()
67 9cad4715 Leonidas Poulopoulos
            form.save_m2m()
68 9cad4715 Leonidas Poulopoulos
            route.commit_add()
69 97e42c7d Leonidas Poulopoulos
            return HttpResponseRedirect(reverse("group-routes"))
70 9cad4715 Leonidas Poulopoulos
        else:
71 97e42c7d Leonidas Poulopoulos
            return render_to_response('apply.html', {'form': form, 'applier':applier},
72 9cad4715 Leonidas Poulopoulos
                                      context_instance=RequestContext(request))
73 97e42c7d Leonidas Poulopoulos
74 97e42c7d Leonidas Poulopoulos
@login_required
75 97e42c7d Leonidas Poulopoulos
def add_then(request):
76 97e42c7d Leonidas Poulopoulos
    applier = request.user.pk
77 97e42c7d Leonidas Poulopoulos
    if request.method == "GET":
78 97e42c7d Leonidas Poulopoulos
        form = RouteForm()
79 97e42c7d Leonidas Poulopoulos
        return render_to_response('apply.html', {'form': form, 'applier': applier},
80 97e42c7d Leonidas Poulopoulos
                                  context_instance=RequestContext(request))
81 97e42c7d Leonidas Poulopoulos
82 97e42c7d Leonidas Poulopoulos
    else:
83 97e42c7d Leonidas Poulopoulos
        form = RouteForm(request.POST)
84 97e42c7d Leonidas Poulopoulos
        if form.is_valid():
85 97e42c7d Leonidas Poulopoulos
            route=form.save(commit=False)
86 97e42c7d Leonidas Poulopoulos
            route.applier = request.user
87 97e42c7d Leonidas Poulopoulos
            route.expires = days_offset()
88 97e42c7d Leonidas Poulopoulos
            route.save()
89 97e42c7d Leonidas Poulopoulos
            form.save_m2m()
90 97e42c7d Leonidas Poulopoulos
            route.commit_add()
91 97e42c7d Leonidas Poulopoulos
            return HttpResponseRedirect(reverse("group-routes"))
92 97e42c7d Leonidas Poulopoulos
        else:
93 97e42c7d Leonidas Poulopoulos
            return render_to_response('apply.html', {'form': form, 'applier':applier},
94 97e42c7d Leonidas Poulopoulos
                                      context_instance=RequestContext(request))
95 97e42c7d Leonidas Poulopoulos
96 3e99e2d1 Leonidas Poulopoulos
@login_required
97 3e99e2d1 Leonidas Poulopoulos
def edit_route(request, route_slug):
98 97e42c7d Leonidas Poulopoulos
    applier = request.user.pk
99 5141cf4a Leonidas Poulopoulos
    applier_peer = request.user.get_profile().peer
100 3e99e2d1 Leonidas Poulopoulos
    route_edit = get_object_or_404(Route, name=route_slug)
101 5141cf4a Leonidas Poulopoulos
    route_edit_applier_peer = route_edit.applier.get_profile().peer
102 5141cf4a Leonidas Poulopoulos
    if applier_peer != route_edit_applier_peer:
103 5141cf4a Leonidas Poulopoulos
        messages.add_message(request, messages.WARNING,
104 5141cf4a Leonidas Poulopoulos
                             "Insufficient rights to edit route %s" %(route_slug))
105 5141cf4a Leonidas Poulopoulos
        return HttpResponseRedirect(reverse("group-routes"))
106 3e99e2d1 Leonidas Poulopoulos
    route_original = deepcopy(route_edit)
107 3e99e2d1 Leonidas Poulopoulos
    if request.POST:
108 3e99e2d1 Leonidas Poulopoulos
        form = RouteForm(request.POST, instance = route_edit)
109 3e99e2d1 Leonidas Poulopoulos
        if form.is_valid():
110 3e99e2d1 Leonidas Poulopoulos
            route=form.save(commit=False)
111 3e99e2d1 Leonidas Poulopoulos
            route.name = route_original.name
112 97e42c7d Leonidas Poulopoulos
            route.applier = request.user
113 3e99e2d1 Leonidas Poulopoulos
            route.expires = route_original.expires
114 97e42c7d Leonidas Poulopoulos
            route.status = "PENDING"
115 3e99e2d1 Leonidas Poulopoulos
            route.save()
116 3e99e2d1 Leonidas Poulopoulos
            form.save_m2m()
117 3e99e2d1 Leonidas Poulopoulos
            route.commit_edit()
118 97e42c7d Leonidas Poulopoulos
            return HttpResponseRedirect(reverse("group-routes"))
119 3e99e2d1 Leonidas Poulopoulos
        else:
120 97e42c7d Leonidas Poulopoulos
            return render_to_response('apply.html', {'form': form, 'edit':True, 'applier': applier},
121 3e99e2d1 Leonidas Poulopoulos
                                      context_instance=RequestContext(request))
122 3e99e2d1 Leonidas Poulopoulos
    else:
123 3e99e2d1 Leonidas Poulopoulos
        dictionary = model_to_dict(route_edit, fields=[], exclude=[])
124 97e42c7d Leonidas Poulopoulos
        #form = RouteForm(instance=route_edit)
125 3e99e2d1 Leonidas Poulopoulos
        form = RouteForm(dictionary)
126 97e42c7d Leonidas Poulopoulos
        return render_to_response('apply.html', {'form': form, 'edit':True, 'applier': applier},
127 3e99e2d1 Leonidas Poulopoulos
                                  context_instance=RequestContext(request))
128 3e99e2d1 Leonidas Poulopoulos
129 3e99e2d1 Leonidas Poulopoulos
@login_required
130 3e99e2d1 Leonidas Poulopoulos
def delete_route(request, route_slug):
131 3e99e2d1 Leonidas Poulopoulos
    if request.is_ajax():
132 3e99e2d1 Leonidas Poulopoulos
        route = get_object_or_404(Route, name=route_slug)
133 97e42c7d Leonidas Poulopoulos
        applier_peer = route.applier.get_profile().peer
134 97e42c7d Leonidas Poulopoulos
        requester_peer = request.user.get_profile().peer
135 97e42c7d Leonidas Poulopoulos
        if applier_peer == requester_peer:
136 3e99e2d1 Leonidas Poulopoulos
            route.deactivate()
137 3e99e2d1 Leonidas Poulopoulos
            route.commit_delete()
138 97e42c7d Leonidas Poulopoulos
        html = "<html><body>Done</body></html>"
139 97e42c7d Leonidas Poulopoulos
        return HttpResponse(html)
140 97e42c7d Leonidas Poulopoulos
    else:
141 97e42c7d Leonidas Poulopoulos
        return HttpResponseRedirect(reverse("group-routes"))
142 97e42c7d Leonidas Poulopoulos
143 97e42c7d Leonidas Poulopoulos
@login_required
144 97e42c7d Leonidas Poulopoulos
def user_profile(request):
145 97e42c7d Leonidas Poulopoulos
    user = request.user
146 97e42c7d Leonidas Poulopoulos
    peer = request.user.get_profile().peer
147 97e42c7d Leonidas Poulopoulos
    
148 97e42c7d Leonidas Poulopoulos
    return render_to_response('profile.html', {'user': user, 'peer':peer},
149 97e42c7d Leonidas Poulopoulos
                                  context_instance=RequestContext(request))
150 97e42c7d Leonidas Poulopoulos
151 97e42c7d Leonidas Poulopoulos
152 97e42c7d Leonidas Poulopoulos
def user_login(request):
153 97e42c7d Leonidas Poulopoulos
    try:
154 97e42c7d Leonidas Poulopoulos
        error_username = None
155 97e42c7d Leonidas Poulopoulos
        error_orgname = None
156 97e42c7d Leonidas Poulopoulos
        username = request.META['HTTP_EPPN']
157 97e42c7d Leonidas Poulopoulos
        if not username:
158 97e42c7d Leonidas Poulopoulos
            error_username = True
159 97e42c7d Leonidas Poulopoulos
        firstname = request.META['HTTP_SHIB_INETORGPERSON_GIVENNAME']
160 97e42c7d Leonidas Poulopoulos
        lastname = request.META['HTTP_SHIB_PERSON_SURNAME']
161 97e42c7d Leonidas Poulopoulos
        mail = request.META['HTTP_SHIB_INETORGPERSON_MAIL']
162 97e42c7d Leonidas Poulopoulos
        organization = request.META['HTTP_SHIB_HOMEORGANIZATION']
163 97e42c7d Leonidas Poulopoulos
        if not organization:
164 97e42c7d Leonidas Poulopoulos
            error_orgname = True
165 97e42c7d Leonidas Poulopoulos
166 97e42c7d Leonidas Poulopoulos
        if error_orgname or error_username:
167 97e42c7d Leonidas Poulopoulos
            error = "Your idP should release the HTTP_EPPN, HTTP_SHIB_HOMEORGANIZATION attributes towards this service" 
168 97e42c7d Leonidas Poulopoulos
            return render_to_response('error.html', {'error': error,},
169 97e42c7d Leonidas Poulopoulos
                                  context_instance=RequestContext(request))
170 97e42c7d Leonidas Poulopoulos
        user = authenticate(username=username, firstname=firstname, lastname=lastname, mail=mail, organization=organization, affiliation=None)
171 97e42c7d Leonidas Poulopoulos
        if user is not None:
172 97e42c7d Leonidas Poulopoulos
            login(request, user)
173 97e42c7d Leonidas Poulopoulos
            return HttpResponseRedirect(reverse("group-routes"))
174 97e42c7d Leonidas Poulopoulos
                # Redirect to a success page.
175 97e42c7d Leonidas Poulopoulos
                # Return a 'disabled account' error message
176 97e42c7d Leonidas Poulopoulos
        else:
177 97e42c7d Leonidas Poulopoulos
            html = "<html><body>Invalid User</body></html>"
178 97e42c7d Leonidas Poulopoulos
            return HttpResponse(html)
179 97e42c7d Leonidas Poulopoulos
    except Exception as e:
180 97e42c7d Leonidas Poulopoulos
        html = "<html><body>Invalid Login Procedure %s </body></html>" %e
181 97e42c7d Leonidas Poulopoulos
        return HttpResponse(html)
182 97e42c7d Leonidas Poulopoulos
        # Return an 'invalid login' error message.
183 97e42c7d Leonidas Poulopoulos
#    return HttpResponseRedirect(reverse("user-routes"))
184 97e42c7d Leonidas Poulopoulos
185 97e42c7d Leonidas Poulopoulos
@login_required
186 97e42c7d Leonidas Poulopoulos
def add_rate_limit(request):
187 97e42c7d Leonidas Poulopoulos
    if request.method == "GET":
188 97e42c7d Leonidas Poulopoulos
        form = ThenPlainForm()
189 97e42c7d Leonidas Poulopoulos
        return render_to_response('add_rate_limit.html', {'form': form,},
190 97e42c7d Leonidas Poulopoulos
                                  context_instance=RequestContext(request))
191 97e42c7d Leonidas Poulopoulos
192 97e42c7d Leonidas Poulopoulos
    else:
193 97e42c7d Leonidas Poulopoulos
        form = ThenPlainForm(request.POST)
194 97e42c7d Leonidas Poulopoulos
        if form.is_valid():
195 97e42c7d Leonidas Poulopoulos
            then=form.save(commit=False)
196 97e42c7d Leonidas Poulopoulos
            then.action_value = "%sk"%then.action_value
197 97e42c7d Leonidas Poulopoulos
            then.save()
198 97e42c7d Leonidas Poulopoulos
            response_data = {}
199 97e42c7d Leonidas Poulopoulos
            response_data['pk'] = "%s" %then.pk
200 97e42c7d Leonidas Poulopoulos
            response_data['value'] = "%s:%s" %(then.action, then.action_value)
201 97e42c7d Leonidas Poulopoulos
            return HttpResponse(simplejson.dumps(response_data), mimetype='application/json')
202 97e42c7d Leonidas Poulopoulos
        else:
203 97e42c7d Leonidas Poulopoulos
            return render_to_response('add_rate_limit.html', {'form': form,},
204 97e42c7d Leonidas Poulopoulos
                                      context_instance=RequestContext(request))
205 97e42c7d Leonidas Poulopoulos
206 97e42c7d Leonidas Poulopoulos
@login_required
207 97e42c7d Leonidas Poulopoulos
def add_port(request):
208 97e42c7d Leonidas Poulopoulos
    if request.method == "GET":
209 97e42c7d Leonidas Poulopoulos
        form = PortPlainForm()
210 97e42c7d Leonidas Poulopoulos
        return render_to_response('add_port.html', {'form': form,},
211 97e42c7d Leonidas Poulopoulos
                                  context_instance=RequestContext(request))
212 97e42c7d Leonidas Poulopoulos
213 97e42c7d Leonidas Poulopoulos
    else:
214 97e42c7d Leonidas Poulopoulos
        form = PortPlainForm(request.POST)
215 97e42c7d Leonidas Poulopoulos
        if form.is_valid():
216 97e42c7d Leonidas Poulopoulos
            port=form.save()
217 97e42c7d Leonidas Poulopoulos
            response_data = {}
218 97e42c7d Leonidas Poulopoulos
            response_data['value'] = "%s" %port.pk
219 97e42c7d Leonidas Poulopoulos
            response_data['text'] = "%s" %port.port
220 97e42c7d Leonidas Poulopoulos
            return HttpResponse(simplejson.dumps(response_data), mimetype='application/json')
221 97e42c7d Leonidas Poulopoulos
        else:
222 97e42c7d Leonidas Poulopoulos
            return render_to_response('add_port.html', {'form': form,},
223 97e42c7d Leonidas Poulopoulos
                                      context_instance=RequestContext(request))
224 97e42c7d Leonidas Poulopoulos
225 97e42c7d Leonidas Poulopoulos
@login_required
226 97e42c7d Leonidas Poulopoulos
def user_logout(request):
227 97e42c7d Leonidas Poulopoulos
    return HttpResponseRedirect(settings.SHIB_LOGOUT_URL)
228 97e42c7d Leonidas Poulopoulos
    
229 97e42c7d Leonidas Poulopoulos
    
230 97e42c7d Leonidas Poulopoulos
def load_jscript(request, file):
231 97e42c7d Leonidas Poulopoulos
    return render_to_response('%s.js' % file, context_instance=RequestContext(request), mimetype="text/javascript")