# Path: harnas/checker/forms.py
# class TestEnvironmentForm(ModelForm):
# 
#     template_name = forms.ChoiceField(
#         choices=lambda: [(template.name, template.name)
#                          for template in get_templates_list()])
#     maintainer = forms.ModelChoiceField(
#         queryset=User.objects.exclude(username='AnonymousUser'),
#         empty_label=None)
# 
#     class Meta:
#         model = TestEnvironment
#         fields = ['summary', 'description']
# 
# Path: harnas/checker/models.py
# class TestEnvironment(models.Model):
#     template_name = models.CharField(max_length=250)
#     summary = models.CharField(max_length=250)
#     description = models.TextField()
#     maintainer = models.ForeignKey(User)
# 
#     class Meta:
#         permissions = (
#             ('view_test_environment', 'Can view test environment'),
#             ('edit_test_environment', 'Can edit test environment'))
# 
#     def __str__(self):
#         return self.template_name


from django.contrib.auth.decorators import login_required
from django.core.cache import cache
from django.core.cache.utils import make_template_fragment_key
from django.core.exceptions import PermissionDenied
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.views.decorators.http import require_http_methods, require_safe
from guardian.shortcuts import assign_perm
from harnas.checker.forms import TestEnvironmentForm
from harnas.checker.models import TestEnvironment
                  {'test_envs': test_envs})


@require_safe
@login_required
def details(request, id):
    test_env = TestEnvironment.objects.get(pk=id)
    if not request.user.has_perm('checker.view_test_environment', test_env):
        raise PermissionDenied
    return render(request, 'checker/test_environment_details.html',
                  {'test_env': test_env})


@require_http_methods(['GET', 'POST'])
@login_required
def edit(request, id=None):
    if id:
        test_env = TestEnvironment.objects.get(pk=id)
        form_post = reverse('test_environment_edit', args=[id])
        if not request.user.has_perm('checker.edit_test_environment',
                                     test_env):
            raise PermissionDenied
    else:
        test_env = TestEnvironment(maintainer=request.user)
        form_post = reverse('test_environment_new')
        if not request.user.has_perm('checker.add_test_environment'):
            raise PermissionDenied
    if request.method == "POST":
        form = TestEnvironmentForm(request.POST, instance=test_env)
    else:

# Path: harnas/checker/forms.py
# class TestEnvironmentForm(ModelForm):
# 
#     template_name = forms.ChoiceField(
#         choices=lambda: [(template.name, template.name)
#                          for template in get_templates_list()])
#     maintainer = forms.ModelChoiceField(
#         queryset=User.objects.exclude(username='AnonymousUser'),
#         empty_label=None)
# 
#     class Meta:
#         model = TestEnvironment
#         fields = ['summary', 'description']
# 
# Path: harnas/checker/models.py
# class TestEnvironment(models.Model):
#     template_name = models.CharField(max_length=250)
#     summary = models.CharField(max_length=250)
#     description = models.TextField()
#     maintainer = models.ForeignKey(User)
# 
#     class Meta:
#         permissions = (
#             ('view_test_environment', 'Can view test environment'),
#             ('edit_test_environment', 'Can edit test environment'))
# 
#     def __str__(self):
#         return self.template_name


# Path: harnas/checker/views/test_environment.py
from django.contrib.auth.decorators import login_required
from django.core.cache import cache
from django.core.cache.utils import make_template_fragment_key
from django.core.exceptions import PermissionDenied
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.views.decorators.http import require_http_methods, require_safe
from guardian.shortcuts import assign_perm
from harnas.checker.forms import TestEnvironmentForm
from harnas.checker.models import TestEnvironment

                  {'test_envs': test_envs})


@require_safe
@login_required
def details(request, id):
    test_env = TestEnvironment.objects.get(pk=id)
    if not request.user.has_perm('checker.view_test_environment', test_env):
        raise PermissionDenied
    return render(request, 'checker/test_environment_details.html',
                  {'test_env': test_env})


@require_http_methods(['GET', 'POST'])
@login_required
def edit(request, id=None):
    if id:
        test_env = TestEnvironment.objects.get(pk=id)
        form_post = reverse('test_environment_edit', args=[id])
        if not request.user.has_perm('checker.edit_test_environment',
                                     test_env):
            raise PermissionDenied
    else:
        test_env = TestEnvironment(maintainer=request.user)
        form_post = reverse('test_environment_new')
        if not request.user.has_perm('checker.add_test_environment'):
            raise PermissionDenied
    if request.method == "POST":
        form = TestEnvironmentForm(request.POST, instance=test_env)
    else:

        form = TestEnvironmentForm(instance=test_env)