class Template(models.Model):
    name = models.CharField(max_length=100)
    body = models.TextField(default="")
    added = models.DateTimeField(default=datetime.now)

    def __str__(self):  # __unicode__ on Python 2
        return self.name

    class Meta:
        ordering = ('name',) class FlagSerializer(serializers.ModelSerializer):
    class Meta:
        model = Flag
        fields = ('id', 'title', 'note', 'done', 'assessment') class Sh0tSerializer(serializers.ModelSerializer):
    class Meta:
        model = Sh0t
        fields = ('title', 'severity', 'body', 'added', 'assessment') class AssessmentSerializer(serializers.ModelSerializer):
    class Meta:
        model = Assessment
        fields = ('name', 'added', 'project') class ProjectSerializer(serializers.ModelSerializer):
    class Meta:
        model = Project
        fields = ('name', 'added') class TemplateSerializer(serializers.ModelSerializer):
    class Meta:
        model = Template
        fields = ('name', 'body') class CaseMasterSerializer(serializers.ModelSerializer):
    class Meta:
        model = CaseMaster
        fields = ('name', 'description', 'order', 'module') class ModuleMasterSerializer(serializers.ModelSerializer):
    class Meta:
        model = ModuleMaster
        fields = ('name', 'description', 'order', 'methodology') class MethodologyMasterSerializer(serializers.ModelSerializer):
    class Meta:
        model = MethodologyMaster
        fields = ('name', 'description', 'order') class Flag(models.Model):
    title = models.CharField(max_length=100)
    note = models.TextField(default="")
    assessment = models.ForeignKey(Assessment, null=True, on_delete=models.CASCADE)
    done = models.BooleanField(default=False)
    added = models.DateTimeField(default=datetime.now)
    order = models.IntegerField(default=1)

    def __str__(self):  # __unicode__ on Python 2
        return self.title

    class Meta:
        ordering = ('title',) class Sh0t(models.Model):
    title = models.CharField(max_length=200)
    body = models.TextField(default="")
    assessment = models.ForeignKey(Assessment, null=True, on_delete=models.CASCADE)
    added = models.DateTimeField(default=datetime.now)
    severity = models.IntegerField(default=5, validators=[MinValueValidator(0), MaxValueValidator(5)])

    def __str__(self):  # __unicode__ on Python 2
        return self.title

    class Meta:
        ordering = ('severity','title',) class Assessment(models.Model):
    name = models.CharField(max_length=100)
    project = models.ForeignKey(Project, on_delete=models.CASCADE)
    added = models.DateTimeField(default=datetime.now)

    def __str__(self):  # __unicode__ on Python 2
        return self.name

    class Meta:
        ordering = ('name',) class Project(models.Model):
    name = models.CharField(max_length=100)
    added = models.DateTimeField(default=datetime.now)

    def __str__(self):  # __unicode__ on Python 2
        return self.name

    class Meta:
        ordering = ('name',) class CaseMaster(models.Model):
    name = models.CharField(max_length=100)
    module = models.ForeignKey(ModuleMaster, on_delete=models.CASCADE)
    description = models.TextField(default="")
    order = models.IntegerField(default=0)
    created_by = models.ForeignKey(User, related_name="case_module_created_by", null=True, on_delete=models.CASCADE)
    created = models.DateTimeField(default=datetime.now)
    updated_by = models.ForeignKey(User, related_name="case_module_updated_by", null=True, on_delete=models.CASCADE)
    updated = models.DateTimeField(default=datetime.now)

    def __str__(self):  # __unicode__ on Python 2
        return self.name

    def save(self, *args, **kwargs):
        if not self.id:
            self.created = timezone.now()
        self.updated = timezone.now()
        return super(CaseMaster, self).save(*args, **kwargs)

    class Meta:
        ordering = ('name',) class ModuleMaster(models.Model):
    name = models.CharField(max_length=100)
    description = models.TextField(default="")
    order = models.IntegerField(default=0)
    methodology = models.ForeignKey(MethodologyMaster, on_delete=models.CASCADE, null=True, default=None)
    created_by = models.ForeignKey(User, related_name="module_created_by", null=True, on_delete=models.CASCADE)
    created = models.DateTimeField(default=datetime.now)
    updated_by = models.ForeignKey(User, related_name="module_updated_by", null=True, on_delete=models.CASCADE)
    updated = models.DateTimeField(default=datetime.now)

    def __str__(self):  # __unicode__ on Python 2
        return self.name

    def save(self, *args, **kwargs):
        if not self.id:
            self.created = timezone.now()
        self.updated = timezone.now()
        return super(ModuleMaster, self).save(*args, **kwargs)

    class Meta:
        ordering = ('name',) class MethodologyMaster(models.Model):
    name = models.CharField(max_length=100)
    description = models.TextField(default="")
    order = models.IntegerField(default=0)
    created_by = models.ForeignKey(User, related_name="methodology_created_by", null=True, on_delete=models.CASCADE)
    created = models.DateTimeField(default=datetime.now)
    updated_by = models.ForeignKey(User, related_name="methodology_updated_by", null=True, on_delete=models.CASCADE)
    updated = models.DateTimeField(default=datetime.now)

    def __str__(self):  # __unicode__ on Python 2
        return self.name

    def save(self, *args, **kwargs):
        if not self.id:
            self.created = timezone.now()
        self.updated = timezone.now()
        return super(MethodologyMaster, self).save(*args, **kwargs)

    class Meta:
        ordering = ('name',)
from app.models import Template
from .serializers import FlagSerializer, Sh0tSerializer, AssessmentSerializer, ProjectSerializer, TemplateSerializer
from .serializers import CaseMasterSerializer, ModuleMasterSerializer, MethodologyMasterSerializer
from app.models import Flag, Sh0t, Assessment, Project
from configuration.models import CaseMaster, ModuleMaster, MethodologyMaster
from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.response import Response
# -*- coding: utf-8 -*-
from __future__ import unicode_literals


@api_view(['GET', 'PUT', 'PATCH', 'DELETE'])
def flag_detail(request, pk):
    try:
        flag = Flag.objects.get(pk=pk)
    except Flag.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if request.method == 'GET':
        serializer = FlagSerializer(flag)
        return Response(serializer.data)

    elif request.method == 'PUT':
        serializer = FlagSerializer(flag, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    elif request.method == 'PATCH':
        serializer = FlagSerializer(flag, data=request.data, partial=True)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    elif request.method == 'DELETE':
        flag.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)


@api_view(['GET', 'POST'])
def flag_list(request):
    if request.method == 'GET':
        flags = Flag.objects.all()
        serializer = FlagSerializer(flags, many=True)
        return Response(serializer.data)

    elif request.method == 'POST':
        serializer = FlagSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


@api_view(['GET', 'PUT', 'DELETE'])
def sh0t_detail(request, pk):
    try:
        sh0t = Sh0t.objects.get(pk=pk)
    except Sh0t.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if request.method == 'GET':
        serializer = Sh0tSerializer(sh0t)
        return Response(serializer.data)

    elif request.method == 'PUT':
        serializer = Sh0tSerializer(sh0t, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    elif request.method == 'DELETE':
        sh0t.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)


@api_view(['GET', 'POST'])
def sh0t_list(request):
    if request.method == 'GET':
        flags = Sh0t.objects.all()
        serializer = Sh0tSerializer(flags, many=True)
        return Response(serializer.data)

    elif request.method == 'POST':
        serializer = Sh0tSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


@api_view(['GET', 'PUT', 'DELETE'])
def assessment_detail(request, pk):
    try:
        assessment = Assessment.objects.get(pk=pk)
    except Assessment.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if request.method == 'GET':

        serializer = AssessmentSerializer(assessment)