Commit 2bfcba1c authored by stammy31's avatar stammy31

Apply change of ForeginKey peer to M2M relationship peers on UserProfile model...

Apply change of ForeginKey peer to M2M relationship peers on UserProfile model to whole project. Make use of model PeerNotify to notify peers on users actions.
parent d0501fe0
......@@ -59,7 +59,8 @@ class RouteForm(forms.ModelForm):
def clean_source(self):
user = User.objects.get(pk=self.data['applier'])
peer = user.get_profile().peer
peers = user.get_profile().peers.all()
peers_names = ''.join(('%s, ' % (peer.peer_name)) for peer in peers)[:-2]
data = self.cleaned_data['source']
private_error = False
protected_error = False
......@@ -71,7 +72,7 @@ class RouteForm(forms.ModelForm):
for net in settings.PROTECTED_SUBNETS:
if address in IPNetwork(net):
protected_error = True
mail_body = "User %s %s (%s) attempted to set %s as the source address in a firewall rule" %(user.username, user.email, peer.peer_name, data)
mail_body = "User %s %s (%s) attempted to set %s as the source address in a firewall rule" % (user.username, user.email, peers_names, data)
send_mail(settings.EMAIL_SUBJECT_PREFIX + "Caught an attempt to set a protected IP/network as a source address",
mail_body, settings.SERVER_EMAIL,
settings.NOTIFY_ADMIN_MAILS, fail_silently=True)
......@@ -101,7 +102,8 @@ class RouteForm(forms.ModelForm):
def clean_destination(self):
user = User.objects.get(pk=self.data['applier'])
peer = user.get_profile().peer
peers = user.get_profile().peers.all()
peers_names = ''.join(('%s, ' % (peer.peer_name)) for peer in peers)[:-2]
data = self.cleaned_data['destination']
error = None
protected_error = False
......@@ -113,13 +115,13 @@ class RouteForm(forms.ModelForm):
for net in settings.PROTECTED_SUBNETS:
if address in IPNetwork(net):
protected_error = True
mail_body = "User %s %s (%s) attempted to set %s as the destination address in a firewall rule" %(user.username, user.email, peer.peer_name, data)
mail_body = "User %s %s (%s) attempted to set %s as the destination address in a firewall rule" % (user.username, user.email, peers_names, data)
send_mail(settings.EMAIL_SUBJECT_PREFIX + "Caught an attempt to set a protected IP/network as the destination address",
mail_body, settings.SERVER_EMAIL,
settings.NOTIFY_ADMIN_MAILS, fail_silently=True)
raise Exception
if address.prefixlen < settings.PREFIX_LENGTH:
error = _("Currently no prefix lengths < %s are allowed") %settings.PREFIX_LENGTH
error = _("Currently no prefix lengths < %s are allowed") % settings.PREFIX_LENGTH
raise Exception
if address.version == 4 and int(address.prefixlen) == 32:
if int(address.network.compressed.split('.')[-1]) == 0:
......@@ -152,7 +154,7 @@ class RouteForm(forms.ModelForm):
def clean(self):
if self.errors:
raise forms.ValidationError(_('Errors in form. Please review and fix them: %s'%", ".join(self.errors)))
raise forms.ValidationError(_('Errors in form. Please review and fix them: %s' % ", ".join(self.errors)))
name = self.cleaned_data.get('name', None)
source = self.cleaned_data.get('source', None)
sourceports = self.cleaned_data.get('sourceport', None)
......@@ -163,8 +165,10 @@ class RouteForm(forms.ModelForm):
protocols = self.cleaned_data.get('protocol', None)
user = self.cleaned_data.get('applier', None)
issuperuser = self.data.get('issuperuser')
peer = user.get_profile().peer
networks = peer.networks.all()
peers = user.get_profile().peers.all()
networks = []
for peer in peers:
networks.extend(peer.networks.all())
if issuperuser:
networks = PeerRange.objects.filter(peer__in=Peer.objects.all()).distinct()
mynetwork = False
......@@ -187,9 +191,9 @@ class RouteForm(forms.ModelForm):
if not (source or sourceports or ports or destination or destinationports):
raise forms.ValidationError(_('Fill at least a Rule Match Condition'))
if not user.is_superuser and then[0].action not in settings.UI_USER_THEN_ACTIONS:
raise forms.ValidationError(_('This action "%s" is not permitted') %(then[0].action))
raise forms.ValidationError(_('This action "%s" is not permitted') % (then[0].action))
existing_routes = Route.objects.all()
existing_routes = existing_routes.filter(applier__userprofile__peer=peer)
existing_routes = existing_routes.filter(applier__userprofile__peer__in=peers)
if source:
source = IPNetwork(source).compressed
existing_routes = existing_routes.filter(source=source)
......@@ -225,7 +229,7 @@ class RouteForm(forms.ModelForm):
if name != route.name:
existing_url = reverse('edit-route', args=[route.name])
if IPNetwork(destination) in IPNetwork(route.destination) or IPNetwork(route.destination) in IPNetwork(destination):
raise forms.ValidationError('Found an exact %s rule, %s with destination prefix %s<br>To avoid overlapping try editing rule <a href=\'%s\'>%s</a>' %(route.status, route.name, route.destination, existing_url, route.name))
raise forms.ValidationError('Found an exact %s rule, %s with destination prefix %s<br>To avoid overlapping try editing rule <a href=\'%s\'>%s</a>' % (route.status, route.name, route.destination, existing_url, route.name))
return self.cleaned_data
......
......@@ -6,13 +6,15 @@ def send_new_mail(subject, message, from_email, recipient_list, bcc_list):
return EmailMessage(subject, message, from_email, recipient_list, bcc_list).send()
def get_peer_techc_mails(user):
def get_peer_techc_mails(user, peer):
mail = []
additional_mail = []
techmails_list = []
user_mail = '%s' % user.email
user_mail = user_mail.split(';')
techmails = user.get_profile().peer.techc_emails.all()
techmails = []
if peer:
techmails = peer.techc_emails.all()
if techmails:
for techmail in techmails:
techmails_list.append(techmail.email)
......
......@@ -29,7 +29,6 @@ from utils import proxy as PR
from ipaddr import *
import datetime
import logging
from time import sleep
from junos import create_junos_name
......@@ -40,19 +39,6 @@ from utils.randomizer import id_generator as id_gen
from tasks import *
def user_unicode_patch(self):
peer = None
try:
peer = self.get_profile().peer.peer_tag
except:
pass
if peer:
return '%s.::.%s' % (self.username, peer)
return self.username
User.__unicode__ = user_unicode_patch
FORMAT = '%(asctime)s %(levelname)s: %(message)s'
logging.basicConfig(format=FORMAT)
logger = logging.getLogger(__name__)
......@@ -193,10 +179,9 @@ class Route(models.Model):
def save(self, *args, **kwargs):
if not self.pk:
hash = id_gen()
self.name = "%s_%s" %(self.name, hash)
self.name = "%s_%s" % (self.name, hash)
super(Route, self).save(*args, **kwargs) # Call the "real" save() method.
def clean(self, *args, **kwargs):
from django.core.exceptions import ValidationError
if self.destination:
......@@ -213,7 +198,20 @@ class Route(models.Model):
raise ValidationError(_('Invalid network address format at Source Field'))
def commit_add(self, *args, **kwargs):
peer = self.applier.get_profile().peer.peer_tag
peers = self.applier.get_profile().peers.all()
username = None
for peer in peers:
if username:
break
for network in peer.networks.all():
net = IPNetwork(network)
if IPNetwork(self.destination) in net:
username = peer
break
if username:
peer = username.peer_tag
else:
peer = None
send_message("[%s] Adding rule %s. Please wait..." % (self.applier.username, self.name), peer)
response = add.delay(self)
logger.info('Got add job id: %s' % response)
......@@ -228,7 +226,8 @@ class Route(models.Model):
'route': self,
'address': self.requesters_address,
'action': 'creation',
'url': admin_url
'url': admin_url,
'peer': username
}
)
user_mail = '%s' % self.applier.email
......@@ -237,7 +236,7 @@ class Route(models.Model):
settings.EMAIL_SUBJECT_PREFIX + 'Rule %s creation request submitted by %s' % (self.name, self.applier.username),
mail_body,
settings.SERVER_EMAIL, user_mail,
get_peer_techc_mails(self.applier)
get_peer_techc_mails(self.applier, username)
)
d = {
'clientip': '%s' % self.requesters_address,
......@@ -246,7 +245,20 @@ class Route(models.Model):
logger.info(mail_body, extra=d)
def commit_edit(self, *args, **kwargs):
peer = self.applier.get_profile().peer.peer_tag
peers = self.applier.get_profile().peers.all()
username = None
for peer in peers:
if username:
break
for network in peer.networks.all():
net = IPNetwork(network)
if IPNetwork(self.destination) in net:
username = peer
break
if username:
peer = username.peer_tag
else:
peer = None
send_message(
'[%s] Editing rule %s. Please wait...' %
(
......@@ -270,7 +282,8 @@ class Route(models.Model):
'route': self,
'address': self.requesters_address,
'action': 'edit',
'url': admin_url
'url': admin_url,
'peer': username
}
)
user_mail = '%s' % self.applier.email
......@@ -278,7 +291,7 @@ class Route(models.Model):
send_new_mail(
settings.EMAIL_SUBJECT_PREFIX + 'Rule %s edit request submitted by %s' % (self.name, self.applier.username),
mail_body, settings.SERVER_EMAIL, user_mail,
get_peer_techc_mails(self.applier)
get_peer_techc_mails(self.applier, username)
)
d = {
'clientip': self.requesters_address,
......@@ -287,12 +300,25 @@ class Route(models.Model):
logger.info(mail_body, extra=d)
def commit_delete(self, *args, **kwargs):
username = None
reason_text = ''
reason = ''
if "reason" in kwargs:
reason = kwargs['reason']
reason_text = 'Reason: %s.' % reason
peer = self.applier.get_profile().peer.peer_tag
peers = self.applier.get_profile().peers.all()
for peer in peers:
if username:
break
for network in peer.networks.all():
net = IPNetwork(network)
if IPNetwork(self.destination) in net:
username = peer
break
if username:
peer = username.peer_tag
else:
peer = None
send_message(
'[%s] Suspending rule %s. %sPlease wait...' % (
self.applier.username,
......@@ -316,7 +342,8 @@ class Route(models.Model):
'route': self,
'address': self.requesters_address,
'action': 'removal',
'url': admin_url
'url': admin_url,
'peer': username
}
)
user_mail = '%s' % self.applier.email
......@@ -326,7 +353,7 @@ class Route(models.Model):
mail_body,
settings.SERVER_EMAIL,
user_mail,
get_peer_techc_mails(self.applier)
get_peer_techc_mails(self.applier, username)
)
d = {
'clientip': self.requesters_address,
......@@ -560,12 +587,13 @@ class Route(models.Model):
get_match.allow_tags = True
@property
def applier_peer(self):
def applier_peers(self):
try:
applier_peer = self.applier.get_profile().peer
peers = self.applier.get_profile().peers.all()
applier_peers = ''.join(('%s, ' % (peer.peer_name)) for peer in peers)[:-2]
except:
applier_peer = None
return applier_peer
applier_peers = None
return applier_peers
@property
def days_to_expire(self):
......@@ -591,6 +619,6 @@ def send_message(msg, user):
peer = user
b = beanstalkc.Connection()
b.use(settings.POLLS_TUBE)
tube_message = json.dumps({'message': str(msg), 'username':peer})
tube_message = json.dumps({'message': str(msg), 'username': peer})
b.put(tube_message)
b.close()
......@@ -30,6 +30,7 @@ from django.core.mail import send_mail
from django.template.loader import render_to_string
import os
from celery.exceptions import TimeLimitExceeded, SoftTimeLimitExceeded
from ipaddr import *
LOG_FILENAME = os.path.join(settings.LOG_FILE_LOCATION, 'celery_jobs.log')
......@@ -57,22 +58,22 @@ def add(route, callback=None):
route.status = status
route.response = response
route.save()
announce("[%s] Rule add: %s - Result: %s" %(route.applier, route.name, response), route.applier)
announce("[%s] Rule add: %s - Result: %s" % (route.applier, route.name, response), route.applier, route)
except TimeLimitExceeded:
route.status = "ERROR"
route.response = "Task timeout"
route.save()
announce("[%s] Rule add: %s - Result: %s"%(route.applier, route.name, route.response), route.applier)
announce("[%s] Rule add: %s - Result: %s" % (route.applier, route.name, route.response), route.applier, route)
except SoftTimeLimitExceeded:
route.status = "ERROR"
route.response = "Task timeout"
route.save()
announce("[%s] Rule add: %s - Result: %s"%(route.applier, route.name, route.response), route.applier)
announce("[%s] Rule add: %s - Result: %s" % (route.applier, route.name, route.response), route.applier, route)
except Exception:
route.status = "ERROR"
route.response = "Error"
route.save()
announce("[%s] Rule add: %s - Result: %s"%(route.applier, route.name, route.response), route.applier)
announce("[%s] Rule add: %s - Result: %s" % (route.applier, route.name, route.response), route.applier, route)
@task(ignore_result=True)
......@@ -87,22 +88,22 @@ def edit(route, callback=None):
route.status = status
route.response = response
route.save()
announce("[%s] Rule edit: %s - Result: %s"%(route.applier, route.name, response), route.applier)
announce("[%s] Rule edit: %s - Result: %s" % (route.applier, route.name, response), route.applier, route)
except TimeLimitExceeded:
route.status = "ERROR"
route.response = "Task timeout"
route.save()
announce("[%s] Rule edit: %s - Result: %s"%(route.applier, route.name, route.response), route.applier)
announce("[%s] Rule edit: %s - Result: %s" % (route.applier, route.name, route.response), route.applier, route)
except SoftTimeLimitExceeded:
route.status = "ERROR"
route.response = "Task timeout"
route.save()
announce("[%s] Rule edit: %s - Result: %s"%(route.applier, route.name, route.response), route.applier)
announce("[%s] Rule edit: %s - Result: %s" % (route.applier, route.name, route.response), route.applier, route)
except Exception:
route.status = "ERROR"
route.response = "Error"
route.save()
announce("[%s] Rule edit: %s - Result: %s"%(route.applier, route.name, route.response), route.applier)
announce("[%s] Rule edit: %s - Result: %s" % (route.applier, route.name, route.response), route.applier, route)
@task(ignore_result=True)
......@@ -113,30 +114,30 @@ def delete(route, **kwargs):
reason_text = ''
if commit:
status = "INACTIVE"
if "reason" in kwargs and kwargs['reason']=='EXPIRED':
if "reason" in kwargs and kwargs['reason'] == 'EXPIRED':
status = 'EXPIRED'
reason_text = " Reason: %s " %status
reason_text = " Reason: %s " % status
else:
status = "ERROR"
route.status = status
route.response = response
route.save()
announce("[%s] Suspending rule : %s%s- Result %s" %(route.applier, route.name, reason_text, response), route.applier)
announce("[%s] Suspending rule : %s%s- Result %s" % (route.applier, route.name, reason_text, response), route.applier, route)
except TimeLimitExceeded:
route.status = "ERROR"
route.response = "Task timeout"
route.save()
announce("[%s] Suspending rule : %s - Result: %s"%(route.applier, route.name, route.response), route.applier)
announce("[%s] Suspending rule : %s - Result: %s" % (route.applier, route.name, route.response), route.applier, route)
except SoftTimeLimitExceeded:
route.status = "ERROR"
route.response = "Task timeout"
route.save()
announce("[%s] Suspending rule : %s - Result: %s"%(route.applier, route.name, route.response), route.applier)
announce("[%s] Suspending rule : %s - Result: %s" % (route.applier, route.name, route.response), route.applier, route)
except Exception:
route.status = "ERROR"
route.response = "Error"
route.save()
announce("[%s] Suspending rule : %s - Result: %s"%(route.applier, route.name, route.response), route.applier)
announce("[%s] Suspending rule : %s - Result: %s" % (route.applier, route.name, route.response), route.applier, route)
# May not work in the first place... proxy is not aware of Route models
......@@ -144,19 +145,19 @@ def delete(route, **kwargs):
def batch_delete(routes, **kwargs):
if routes:
for route in routes:
route.status='PENDING';route.save()
route.status = 'PENDING';route.save()
applier = PR.Applier(route_objects=routes)
conf = applier.delete_routes()
commit, response = applier.apply(configuration = conf)
commit, response = applier.apply(configuration=conf)
reason_text = ''
if commit:
status = "INACTIVE"
if "reason" in kwargs and kwargs['reason']=='EXPIRED':
if "reason" in kwargs and kwargs['reason'] == 'EXPIRED':
status = 'EXPIRED'
reason_text = " Reason: %s " %status
elif "reason" in kwargs and kwargs['reason']!='EXPIRED':
reason_text = " Reason: %s " % status
elif "reason" in kwargs and kwargs['reason'] != 'EXPIRED':
status = kwargs['reason']
reason_text = " Reason: %s " %status
reason_text = " Reason: %s " % status
else:
status = "ERROR"
for route in routes:
......@@ -164,15 +165,24 @@ def batch_delete(routes, **kwargs):
route.response = response
route.expires = datetime.date.today()
route.save()
announce("[%s] Rule removal: %s%s- Result %s" % (route.applier, route.name, reason_text, response), route.applier)
announce("[%s] Rule removal: %s%s- Result %s" % (route.applier, route.name, reason_text, response), route.applier, route)
else:
return False
#@task(ignore_result=True)
def announce(messg, user):
def announce(messg, user, route):
peers = user.get_profile().peers.all()
username = None
for peer in peers:
if username:
break
for network in peer.networks.all():
net = IPNetwork(network)
if IPNetwork(route.destination) in net:
username = peer.peer_tag
break
messg = str(messg)
username = user.get_profile().peer.peer_tag
b = beanstalkc.Connection()
b.use(settings.POLLS_TUBE)
tube_message = json.dumps({'message': messg, 'username': username})
......
......@@ -32,7 +32,7 @@ from django.core.urlresolvers import reverse
from django.contrib import messages
from accounts.models import *
from ipaddr import *
from django.db.models import Q
from django.contrib.auth import authenticate, login
from django.forms.models import model_to_dict
......@@ -87,9 +87,9 @@ def welcome(request):
@login_required
@never_cache
def dashboard(request):
group_routes = []
all_group_routes = []
try:
peer = request.user.get_profile().peer
peers = request.user.get_profile().peers.all()
except UserProfile.DoesNotExist:
error = "User <strong>%s</strong> does not belong to any peer or organization. It is not possible to create new firewall rules.<br>Please contact Helpdesk to resolve this issue" % request.user.username
return render(
......@@ -99,26 +99,31 @@ def dashboard(request):
'error': error
}
)
if peer:
peer_members = UserProfile.objects.filter(peer=peer)
users = [prof.user for prof in peer_members]
group_routes = Route.objects.filter(applier__in=users).order_by('-expires')[:10]
if peers:
if request.user.is_superuser:
group_routes = Route.objects.all().order_by('-expires')[:10]
return render(
request,
'dashboard.html',
{
'routes': group_routes
},
)
all_group_routes = Route.objects.all().order_by('-last_updated')[:10]
else:
query = Q()
for peer in peers:
query |= Q(applier__userprofile__in=peer.user_profile.all())
all_group_routes = Route.objects.filter(query)
return render(
request,
'dashboard.html',
{
'routes': all_group_routes
},
)
@login_required
@never_cache
def group_routes(request):
try:
request.user.get_profile().peer
request.user.get_profile().peers.all()
except UserProfile.DoesNotExist:
error = "User <strong>%s</strong> does not belong to any peer or organization. It is not possible to create new firewall rules.<br>Please contact Helpdesk to resolve this issue" % request.user.username
return render(
......@@ -138,9 +143,9 @@ def group_routes(request):
@login_required
@never_cache
def group_routes_ajax(request):
group_routes = []
all_group_routes = []
try:
peer = request.user.get_profile().peer
peers = request.user.get_profile().peers.all().select_related()
except UserProfile.DoesNotExist:
error = "User <strong>%s</strong> does not belong to any peer or organization. It is not possible to create new firewall rules.<br>Please contact Helpdesk to resolve this issue" % request.user.username
return render(
......@@ -148,14 +153,17 @@ def group_routes_ajax(request):
'error.html',
{'error': error}
)
if peer:
peer_members = UserProfile.objects.filter(peer=peer)
users = [prof.user for prof in peer_members]
group_routes = Route.objects.filter(applier__in=users)
if request.user.is_superuser:
group_routes = Route.objects.all()
if request.user.is_superuser:
all_group_routes = Route.objects.all()
else:
query = Q()
for peer in peers:
query |= Q(applier__userprofile__in=peer.user_profile.all())
all_group_routes = Route.objects.filter(query)
jresp = {}
routes = build_routes_json(group_routes)
routes = build_routes_json(all_group_routes)
jresp['aaData'] = routes
return HttpResponse(json.dumps(jresp), mimetype='application/json')
......@@ -163,20 +171,20 @@ def group_routes_ajax(request):
@login_required
@never_cache
def overview_routes_ajax(request):
group_routes = []
all_group_routes = []
try:
peer = request.user.get_profile().peer
peers = request.user.get_profile().peers.all().select_related()
except UserProfile.DoesNotExist:
error = "User <strong>%s</strong> does not belong to any peer or organization. It is not possible to create new firewall rules.<br>Please contact Helpdesk to resolve this issue" % request.user.username
return render_to_response('error.html', {'error': error}, context_instance=RequestContext(request))
if peer:
peer_members = UserProfile.objects.filter(peer=peer)
users = [prof.user for prof in peer_members]
group_routes = Route.objects.filter(applier__in=users)
if request.user.is_superuser or request.user.has_perm('accounts.overview'):
group_routes = Route.objects.all()
query = Q()
for peer in peers:
query |= Q(applier__userprofile__in=peer.user_profile.all())
all_group_routes = Route.objects.filter(query)
if request.user.is_superuser or request.user.has_perm('accounts.overview'):
all_group_routes = Route.objects.all()
jresp = {}
routes = build_routes_json(group_routes)
routes = build_routes_json(all_group_routes)
jresp['aaData'] = routes
return HttpResponse(json.dumps(jresp), mimetype='application/json')
......@@ -203,8 +211,18 @@ def build_routes_json(groutes):
rd['applier'] = 'unknown'
rd['peer'] = ''
else:
peers = r.applier.get_profile().peers.all()
username = None
for peer in peers:
if username:
break
for network in peer.networks.all():
net = IPNetwork(network)
if IPNetwork(r.destination) in net:
username = peer.peer_name
break
try:
rd['peer'] = r.applier.get_profile().peer.peer_name
rd['peer'] = username
except UserProfile.DoesNotExist:
rd['peer'] = ''
......@@ -217,11 +235,14 @@ def build_routes_json(groutes):
@login_required
@never_cache
def add_route(request):
applier_peer_networks = []
applier = request.user.pk
if request.user.is_superuser:
applier_peer_networks = PeerRange.objects.all()
else:
applier_peer_networks = request.user.get_profile().peer.networks.all()
user_peers = request.user.get_profile().peers.all()
for peer in user_peers:
applier_peer_networks.extend(peer.networks.all())
if not applier_peer_networks:
messages.add_message(
request,
......@@ -286,14 +307,20 @@ def add_route(request):
@never_cache
def edit_route(request, route_slug):
applier = request.user.pk
applier_peer = request.user.get_profile().peer
route_edit = get_object_or_404(Route, name=route_slug)
route_edit_applier_peer = route_edit.applier.get_profile().peer
if applier_peer != route_edit_applier_peer and (not request.user.is_superuser):
applier_peer_networks = []
if request.user.is_superuser:
applier_peer_networks = PeerRange.objects.all()
else:
user_peers = request.user.get_profile().peers.all()
for peer in user_peers:
applier_peer_networks.extend(peer.networks.all())
if not applier_peer_networks:
messages.add_message(
request,
messages.WARNING,
('Insufficient rights to edit rule %s') % (route_slug)
('Insufficient rights on administrative networks. Cannot add rule. Contact your administrator')
)
return HttpResponseRedirect(reverse("group-routes"))
if route_edit.status == 'PENDING':
......@@ -387,15 +414,39 @@ def edit_route(request, route_slug):
def delete_route(request, route_slug):
if request.is_ajax():
route = get_object_or_404(Route, name=route_slug)
applier_peer = route.applier.get_profile().peer
requester_peer = request.user.get_profile().peer
peers = route.applier.get_profile().peers.all()
username = None
for peer in peers:
if username:
break
for network in peer.networks.all():
net = IPNetwork(network)
if IPNetwork(route.destination) in net:
username = peer
break
applier_peer = username
peers = request.user.get_profile().peers.all()
username = None
for peer in peers:
if username:
break
for network in peer.networks.all():
net = IPNetwork(network)
if IPNetwork(route.destination) in net:
username = peer
break
requester_peer = username
if applier_peer == requester_peer or request.user.is_superuser:
route.status = "PENDING"
route.expires = datetime.date.today()
if not request.user.is_superuser:
route.applier = request.user
route.response = "Deactivating"
route.requesters_address = request.META['HTTP_X_FORWARDED_FOR']
try:
route.requesters_address = request.META['HTTP_X_FORWARDED_FOR']
except:
# in case the header is not provided
route.requesters_address = 'unknown'
route.save()
route.commit_delete()
html = "<html><body>Done</body></html>"
......@@ -409,8 +460,7 @@ def delete_route(request, route_slug):
def user_profile(request):
user = request.user
try:
peer = request.user.get_profile().peer
peers = Peer.objects.filter(pk=peer.pk)
peers = request.user.get_profile().peers.all()
if user.is_superuser:
peers = Peer.objects.all()
except UserProfile.DoesNotExist:
......@@ -483,7 +533,7 @@ def user_login(request):
if user is not None:
try:
user.get_profile().peer
user.get_profile().peers.all()
except:
form = UserProfileForm()
form.fields['user'] = forms.ModelChoiceField(queryset=User.objects.filter(pk=user.pk), empty_label=None)
......@@ -524,7 +574,7 @@ def user_login(request):
def user_activation_notify(user):
current_site = Site.objects.get_current()
peer = user.get_profile().peer
peers = user.get_profile()<