# Path: mumble/models.py
# class MumbleService(BaseServiceModel):
#     address = models.CharField(max_length=254)
#     port = models.PositiveIntegerField(default=65535)
# 
#     def _hash(self, username, password):
#         return hashlib.sha1(username+password).hexdigest()
# 
#     def test_connection(self):
#         return True
# 
#     def sync_groups(self):
#         pass
# 
#     def update_user_groups(self, user):
#         if MumbleUser.objects.filter(service=self).filter(user=user).exists():
#             user_model = MumbleUser.objects.get(service=self, user=user)
#             for g in user.groups.all():
#                 for m in g.mumblegroup_set.filter(service=self):
#                     if not m in user_model.mumble_groups.all():
#                         user_model.mumble_groups.add(m)
#             for m in user_model.mumble_groups.all():
#                 for g in m.groups.all():
#                     if g in user.groups.all():
#                         break
#                 else:
#                     user_model.groups.remove(m)
# 
#     def create_group(self, group_name):
#         safe_name = self.__sanatize_username(group_name)
#         if MumbleGroup.objects.filter(service=self).filter(group_name=safe_name).exists():
#             raise services.errors.DuplicateGroupError(self, None, safe_name)
#         else:
#             logger.info("Creating group %s on mumble service %s" % (safe_name, self))
#             model = MumbleGroup(group_name=safe_name, service=self)
#             model.save()
#             return model
# 
#     def auto_configure_groups(self):
#         logger.debug("Initiating auto-configuration of groups for mumble service %s" % self)
#         for g in Group.objects.all():
#             safe_name = self.__sanatize_username(g.name)
#             if MumbleGroup.objects.filter(group_name=safe_name).filter(service=self).exists() is False:
#                 model = self.create_group(g.name)
#                 model.groups.add(g)
# 
#     def add_user(self, user, password=None):
#         if MumbleUser.objects.filter(user=user).filter(service=self).exists():
#             logger.error("User %s already has user account on mumble service %s" % (user, self))
#         if self.check_user_has_access(user) is False:
#             raise services.errors.RequiredGroupsError(self, None, user)
#         if not password:
#             logger.debug("No password supplied. Generating random.")
#             password = self._generate_random_pass()
#         model = MumbleUser.objects.create(service=self, username=str(user), pwhash=self._hash(str(user), password), user=user)
#         return {'username': str(user), 'password': password}
# 
#     def remove_user(self, user):
#         user_model = MumbleUser.objects.get(service=self, user=user)
#         logger.info("Removing user %s from mumble service %s" % (user, self))
#         user_model.delete()
# 
#     def set_password(self, user, password=None):
#         user_model = MumbleUser.objects.get(service=self, user=user)
#         if not password:
#             password = self._generate_random_pass()
#         logger.info("Updating user %s password on mumble service %s" % (user, self))
#         user_model.pwhash = self._hash(user_model.username, password)
#         user_model.save(update_fields=['pwhash'])
#         return {'username': user_model.username, 'password': password}
# 
#     def get_display_parameters(self, user):
#         if MumbleUser.objects.filter(service=self).filter(user=user).exists():
#             user_model = MumbleUser.objects.get(service=self, user=user)
#             username = user_model.username
#             address = "%s:%s" % (self.address, self.port)
#             return {'username':username, 'address':address}
#         else:
#             return {}
# 
# Path: services/forms.py
# class ServicePasswordForm(forms.Form):
#     password = forms.CharField(widget=forms.PasswordInput, max_length=254, label="Password")
#     password2 = forms.CharField(widget=forms.PasswordInput, max_length=254, label="Password Again")
# 
#     def clean_password2(self):
#         if self.cleaned_data['password'] == self.cleaned_data['password2']:
#             return self.cleaned_data['password2']
#         raise forms.ValidationError("Passwords do not match")


from django.shortcuts import render, get_object_or_404, redirect
from django.contrib.auth.decorators import login_required, permission_required, user_passes_test
from .models import MumbleService
from services.forms import ServicePasswordForm
import services.errors
import logging
        credentials = m.add_user(request.user)
        return render(request, 'registered/services/credentials.html', context={'credentials':credentials, 'next': 'mumble_list'})
    except services.errors.RequiredGroupsError:
        logger.warn("User %s not authorized to activate mumble %s" % (request.user, m))
    return rediect('mumble_list')

@login_required
@permission_required('access.site_access')
def mumble_deactivate(request, mumble_id):
    logger.debug("mumble_deactivate called by user %s for mumble_id %s" % (request.user, mumble_id))
    m = get_object_or_404(MumbleService, pk=mumble_id)
    try:
        m.remove_user(request.user)
    except services.errors.UserNotActiveError:
        logger.warn("Unable to deactivate user %s mumble %s - user does not have account on service" % (request.user, m))
    return redirect('mumble_list')

@login_required
@permission_required('access.site_access')
def mumble_password(request, mumble_id):
    logger.debug("mumble_password called by user %s for mumble_id %s" % (request.user, mumble_id))
    m = get_object_or_404(MumbleService, pk=mumble_id)
    try:
        if request.method == "POST":
            form = ServicePasswordForm(request.POST)
            if form.is_valid():
                password = form.cleaned_data['password']
                credentials = m.set_password(request.user, password)
                return render(request, 'registered/services/credentials.html', context={'credentials':credentials, 'next': 'mumble_list'})
        else:

# Path: mumble/models.py
# class MumbleService(BaseServiceModel):
#     address = models.CharField(max_length=254)
#     port = models.PositiveIntegerField(default=65535)
# 
#     def _hash(self, username, password):
#         return hashlib.sha1(username+password).hexdigest()
# 
#     def test_connection(self):
#         return True
# 
#     def sync_groups(self):
#         pass
# 
#     def update_user_groups(self, user):
#         if MumbleUser.objects.filter(service=self).filter(user=user).exists():
#             user_model = MumbleUser.objects.get(service=self, user=user)
#             for g in user.groups.all():
#                 for m in g.mumblegroup_set.filter(service=self):
#                     if not m in user_model.mumble_groups.all():
#                         user_model.mumble_groups.add(m)
#             for m in user_model.mumble_groups.all():
#                 for g in m.groups.all():
#                     if g in user.groups.all():
#                         break
#                 else:
#                     user_model.groups.remove(m)
# 
#     def create_group(self, group_name):
#         safe_name = self.__sanatize_username(group_name)
#         if MumbleGroup.objects.filter(service=self).filter(group_name=safe_name).exists():
#             raise services.errors.DuplicateGroupError(self, None, safe_name)
#         else:
#             logger.info("Creating group %s on mumble service %s" % (safe_name, self))
#             model = MumbleGroup(group_name=safe_name, service=self)
#             model.save()
#             return model
# 
#     def auto_configure_groups(self):
#         logger.debug("Initiating auto-configuration of groups for mumble service %s" % self)
#         for g in Group.objects.all():
#             safe_name = self.__sanatize_username(g.name)
#             if MumbleGroup.objects.filter(group_name=safe_name).filter(service=self).exists() is False:
#                 model = self.create_group(g.name)
#                 model.groups.add(g)
# 
#     def add_user(self, user, password=None):
#         if MumbleUser.objects.filter(user=user).filter(service=self).exists():
#             logger.error("User %s already has user account on mumble service %s" % (user, self))
#         if self.check_user_has_access(user) is False:
#             raise services.errors.RequiredGroupsError(self, None, user)
#         if not password:
#             logger.debug("No password supplied. Generating random.")
#             password = self._generate_random_pass()
#         model = MumbleUser.objects.create(service=self, username=str(user), pwhash=self._hash(str(user), password), user=user)
#         return {'username': str(user), 'password': password}
# 
#     def remove_user(self, user):
#         user_model = MumbleUser.objects.get(service=self, user=user)
#         logger.info("Removing user %s from mumble service %s" % (user, self))
#         user_model.delete()
# 
#     def set_password(self, user, password=None):
#         user_model = MumbleUser.objects.get(service=self, user=user)
#         if not password:
#             password = self._generate_random_pass()
#         logger.info("Updating user %s password on mumble service %s" % (user, self))
#         user_model.pwhash = self._hash(user_model.username, password)
#         user_model.save(update_fields=['pwhash'])
#         return {'username': user_model.username, 'password': password}
# 
#     def get_display_parameters(self, user):
#         if MumbleUser.objects.filter(service=self).filter(user=user).exists():
#             user_model = MumbleUser.objects.get(service=self, user=user)
#             username = user_model.username
#             address = "%s:%s" % (self.address, self.port)
#             return {'username':username, 'address':address}
#         else:
#             return {}
# 
# Path: services/forms.py
# class ServicePasswordForm(forms.Form):
#     password = forms.CharField(widget=forms.PasswordInput, max_length=254, label="Password")
#     password2 = forms.CharField(widget=forms.PasswordInput, max_length=254, label="Password Again")
# 
#     def clean_password2(self):
#         if self.cleaned_data['password'] == self.cleaned_data['password2']:
#             return self.cleaned_data['password2']
#         raise forms.ValidationError("Passwords do not match")


# Path: mumble/views.py
from django.shortcuts import render, get_object_or_404, redirect
from django.contrib.auth.decorators import login_required, permission_required, user_passes_test
from .models import MumbleService
from services.forms import ServicePasswordForm
import services.errors
import logging

        credentials = m.add_user(request.user)
        return render(request, 'registered/services/credentials.html', context={'credentials':credentials, 'next': 'mumble_list'})
    except services.errors.RequiredGroupsError:
        logger.warn("User %s not authorized to activate mumble %s" % (request.user, m))
    return rediect('mumble_list')

@login_required
@permission_required('access.site_access')
def mumble_deactivate(request, mumble_id):
    logger.debug("mumble_deactivate called by user %s for mumble_id %s" % (request.user, mumble_id))
    m = get_object_or_404(MumbleService, pk=mumble_id)
    try:
        m.remove_user(request.user)
    except services.errors.UserNotActiveError:
        logger.warn("Unable to deactivate user %s mumble %s - user does not have account on service" % (request.user, m))
    return redirect('mumble_list')

@login_required
@permission_required('access.site_access')
def mumble_password(request, mumble_id):
    logger.debug("mumble_password called by user %s for mumble_id %s" % (request.user, mumble_id))
    m = get_object_or_404(MumbleService, pk=mumble_id)
    try:
        if request.method == "POST":
            form = ServicePasswordForm(request.POST)
            if form.is_valid():
                password = form.cleaned_data['password']
                credentials = m.set_password(request.user, password)
                return render(request, 'registered/services/credentials.html', context={'credentials':credentials, 'next': 'mumble_list'})
        else:

            form = ServicePasswordForm()