# Path: kraut_accounts/models.py
# class UserExtension(models.Model):
#     user = models.OneToOneField(User, primary_key=True, on_delete=models.CASCADE)
#     icon = models.CharField(max_length=100, default="ns_icon/octalpus.png")
#     namespaces = models.ManyToManyField(Namespace, blank=True)
# 
# Path: kraut_accounts/forms.py
# class PasswordChangeCustomForm(Form):
#     old_password = CharField(required=True, label="Old Password", widget=PasswordInput(attrs={'class': 'form-control'}))
#     new_password1 = CharField(required=True, label="New Password", widget=PasswordInput(attrs={'class': 'form-control'}))
#     new_password2 = CharField(required=True, label="Repeat New Password", widget=PasswordInput(attrs={'class': 'form-control'}))
# 
# Path: kraut_parser/models.py
# class Namespace(models.Model):
#     namespace = models.CharField(max_length=255, default='nospace', unique=True)
#     description = models.TextField(null=True, blank=True)
# 
#     def __unicode__(self):
#         return u"%s" % (self.namespace)
# 
#     class Meta:
#         ordering = ['namespace']
# 
# Path: kraut_intel/utils.py
# def get_icon_for_namespace(namespace):
#     """ Return the static path to the icon associated with given namespace
#     """
#     try:
#         if namespace.count(':')>0:
#             namespace = namespace.split(':')[0]
#         icon = NamespaceIcon.objects.get(namespace=namespace)
#     except Exception as e:
#         return static('ns_icon/octalpus.png')
#     return static('ns_icon/%s' % (icon.icon))


from django.shortcuts import render
from django.contrib.auth import authenticate, login, logout, update_session_auth_hash
from django.contrib.auth.hashers import check_password
from django.http import HttpResponseRedirect
from django.urls import reverse
from django.template import RequestContext
from django.contrib import messages
from kraut_accounts.models import UserExtension
from kraut_accounts.forms import PasswordChangeCustomForm
from kraut_parser.models import Namespace
from kraut_intel.utils import get_icon_for_namespace
            current_user.save()
            messages.info(request, 'Password successfully changed!')
        else:
            messages.error(request, 'Failure updating password!')
        return HttpResponseRedirect(reverse("accounts:changepw"))
    context = {'form': PasswordChangeCustomForm()}
    if hasattr(request.user.userextension, 'namespaces'):
        context['usernamespace'] = request.user.userextension.namespaces.last().namespace.split(':')[0]
        context['namespaceicon'] = get_icon_for_namespace(request.user.userextension.namespaces.last().namespace)
    else:
        context['usernamespace'] = 'nospace'
        context['namespaceicon'] = static('ns_icon/octalpus.png')
    return render(request, 'kraut_accounts/changepw.html', context)


def accounts_login(request):
    """log a user in
    """
    context = {}
    if request.user.is_authenticated():
        return HttpResponseRedirect(reverse("home"))
    if request.method == 'POST':
        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(username=username, password=password)
        if user is not None:
            if user.is_active:
                if not hasattr(user, 'userextension'):
                    user_extension = UserExtension.objects.create(user=user)
                    user_extension.save()

# Path: kraut_accounts/models.py
# class UserExtension(models.Model):
#     user = models.OneToOneField(User, primary_key=True, on_delete=models.CASCADE)
#     icon = models.CharField(max_length=100, default="ns_icon/octalpus.png")
#     namespaces = models.ManyToManyField(Namespace, blank=True)
# 
# Path: kraut_accounts/forms.py
# class PasswordChangeCustomForm(Form):
#     old_password = CharField(required=True, label="Old Password", widget=PasswordInput(attrs={'class': 'form-control'}))
#     new_password1 = CharField(required=True, label="New Password", widget=PasswordInput(attrs={'class': 'form-control'}))
#     new_password2 = CharField(required=True, label="Repeat New Password", widget=PasswordInput(attrs={'class': 'form-control'}))
# 
# Path: kraut_parser/models.py
# class Namespace(models.Model):
#     namespace = models.CharField(max_length=255, default='nospace', unique=True)
#     description = models.TextField(null=True, blank=True)
# 
#     def __unicode__(self):
#         return u"%s" % (self.namespace)
# 
#     class Meta:
#         ordering = ['namespace']
# 
# Path: kraut_intel/utils.py
# def get_icon_for_namespace(namespace):
#     """ Return the static path to the icon associated with given namespace
#     """
#     try:
#         if namespace.count(':')>0:
#             namespace = namespace.split(':')[0]
#         icon = NamespaceIcon.objects.get(namespace=namespace)
#     except Exception as e:
#         return static('ns_icon/octalpus.png')
#     return static('ns_icon/%s' % (icon.icon))


# Path: kraut_accounts/views.py
from django.shortcuts import render
from django.contrib.auth import authenticate, login, logout, update_session_auth_hash
from django.contrib.auth.hashers import check_password
from django.http import HttpResponseRedirect
from django.urls import reverse
from django.template import RequestContext
from django.contrib import messages
from kraut_accounts.models import UserExtension
from kraut_accounts.forms import PasswordChangeCustomForm
from kraut_parser.models import Namespace
from kraut_intel.utils import get_icon_for_namespace

            current_user.save()
            messages.info(request, 'Password successfully changed!')
        else:
            messages.error(request, 'Failure updating password!')
        return HttpResponseRedirect(reverse("accounts:changepw"))
    context = {'form': PasswordChangeCustomForm()}
    if hasattr(request.user.userextension, 'namespaces'):
        context['usernamespace'] = request.user.userextension.namespaces.last().namespace.split(':')[0]
        context['namespaceicon'] = get_icon_for_namespace(request.user.userextension.namespaces.last().namespace)
    else:
        context['usernamespace'] = 'nospace'
        context['namespaceicon'] = static('ns_icon/octalpus.png')
    return render(request, 'kraut_accounts/changepw.html', context)


def accounts_login(request):
    """log a user in
    """
    context = {}
    if request.user.is_authenticated():
        return HttpResponseRedirect(reverse("home"))
    if request.method == 'POST':
        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(username=username, password=password)
        if user is not None:
            if user.is_active:
                if not hasattr(user, 'userextension'):
                    user_extension = UserExtension.objects.create(user=user)
                    user_extension.save()

                    new_namespace = Namespace(namespace='nospace:http://nospace.com', description='no particular namespace')