from datetime import datetime

import django.db
from django.shortcuts import render, get_object_or_404, redirect
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseForbidden
from django.contrib.auth.views import login as django_login
from django.views.generic.simple import direct_to_template
from session_csrf import anonymous_csrf
from django.contrib import messages
from django.contrib.auth.models import User
from django.contrib.auth.decorators import permission_required
from django.conf import settings
from django_browserid.auth import default_username_algo

import forms

username_algo = getattr(settings, 'BROWSERID_USERNAME_ALGO',
                        default_username_algo)

@anonymous_csrf
def main(request):
    return direct_to_template(request, template="main.html")


@login_required
def edit(request, display_name=None):
    return direct_to_template(request, template="profiles_edit.html")


def list_profiles(request):
    return render(request, 'profiles_people.html',
                  {'people': User.objects.\
                   filter(is_active=True, groups__name="Rep")
                   })


def view_profile(request, display_name):
    return direct_to_template(request, template="profiles_view.html")


def invite(request):
    if not request.user.has_perm("profiles.create_user"):
        messages.error(request, 'Permission denied')
        return redirect('main')

    if request.POST:
        form = forms.InviteUserForm(request.POST)

        if form.is_valid():
            email = form.cleaned_data['email']

            try:
                User.objects.create_user(username=username_algo(email),
                                         email=email)

            except django.db.IntegrityError:
                messages.error(request, 'User already exists')

            else:
                messages.success(request, 'User was invited')
                return redirect('profiles_invite')

    else:
        form = forms.InviteUserForm()

    return render(request, "profiles_invite.html",
                  {'form': form}
                  )


def delete_user(request, display_name):
    user = get_object_or_404(User, userprofile__display_name=display_name)

    if user != request.user and \
           request.user.has_perm("profiles.can_edit_profiles") == False:
        messages.error(request, 'Permission denied')
        return redirect('main')

    if request.POST:
        user.delete()
        messages.success(request, 'User was deleted')

    return redirect('main')


@login_required
def view_my_profile(request):
    return view_profile(request,
                        display_name=request.user.userprofile.display_name)


@anonymous_csrf
def plainlogin(request, template_name):
    return django_login(request, template_name=template_name)

@@ -110,66 +110,8 @@
 ect%0A
-from django.contrib.auth.decorators import login_required%0A
 from
@@ -542,16 +542,66 @@
 ame_algo
+%0Afrom remo.base.decorators import permission_check
 %0A%0Aimport
@@ -717,16 +717,17 @@
 _algo)%0A%0A
+%0A
 @anonymo
@@ -1268,172 +1268,84 @@
 )%0A%0A%0A
-def invite(request):%0A    if not request.user.has_perm(%22profiles.create_user%22):%0A        messages.error(request, 'Permission denied')%0A        return redirect('main')%0A
+@permission_check(permissions=%5B'profiles.create_user'%5D)%0Adef invite(request):
 %0A   
@@ -2025,24 +2025,86 @@
         )%0A%0A%0A
+@permission_check(permissions=%5B'profiles.can_edit_profiles'%5D)%0A
 def delete_u
@@ -2211,201 +2211,8 @@
 e)%0A%0A
-    if user != request.user and %5C%0A           request.user.has_perm(%22profiles.can_edit_profiles%22) == False:%0A        messages.error(request, 'Permission denied')%0A        return redirect('main')%0A%0A
     
@@ -2332,38 +2332,42 @@
 ('main')%0A%0A%0A@
-login_required
+permission_check()
 %0Adef view_my
@@ -2610,12 +2610,14 @@
 plate_name)%0A
+%0A%0A

Replace login_required with permission_check decorator