# Path: scrum/models.py
# class Project(models.Model):
#     name = models.CharField(max_length=255, blank=True)
#     description = models.TextField(blank=True)
#     users = models.ManyToManyField(User, related_name='Project_users', blank=True, null=True)
# 
#     class Meta:
#         ordering = ('id',)
# 
#     def __unicode__(self):
#         return "%s" % (self.name, )
# 
# class Story(models.Model):
#     title = models.CharField(max_length=255)
#     note = models.TextField(blank=True)
#     last_modified = models.DateTimeField(auto_now=True)
#     project = models.ForeignKey(Project, related_name='stories')
#     estimated_time = models.FloatField(validators=[MinValueValidator(0), MaxValueValidator(5)])
# 
#     class Meta:
#         ordering = ('title',)
# 
#     def __unicode__(self):
#         return "%s" % (self.title, )
# 
# class Task(models.Model):
#     title = models.CharField(max_length=255)
#     note = models.TextField(blank=True)
#     status = models.CharField(max_length=2, choices=TASKS_STATUS)
#     last_modified = models.DateTimeField(auto_now=True)
#     assigned_to = models.ForeignKey(User, related_name='Task_users', blank=True, null=True)
#     story = models.ForeignKey(Story, related_name='Task_story')
#     estimated_time = models.FloatField(validators=[MinValueValidator(0), MaxValueValidator(8)])
#     objects = TaskManager()
# 
#     class Meta:
#         ordering = ('id',)
#         verbose_name = "Task"
# 
#     def __unicode__(self):
#         return "%s" % (self.title,)
# 
# class Sprint(models.Model):
#     number = models.IntegerField()
#     is_closed = models.BooleanField()
#     project = models.ForeignKey(Project, related_name='sprints')
#     last_modified = models.DateTimeField(auto_now=True)
#     tasks = models.ManyToManyField(Task, through='SprintTasks', blank=True, null=True)
#     objects = SprintManager()
# 
#     class Meta:
#         ordering = ('number',)
#         unique_together = ('number', 'project')
# 
#     def __unicode__(self):
#         return "sprint %s" % (self.number, )
# 
#     def save(self, *args, **kwargs):
#         if self.id is None:
#             self.number = (Sprint.objects.filter(project_id=self.project_id).aggregate(Max('number'))['number__max'] or 0) + 1
# 
#         super(Sprint, self).save(*args, **kwargs)
# 
# class SprintTasks(models.Model):
#     sprint = models.ForeignKey(Sprint, related_name='sprints')
#     task = models.ForeignKey(Task, related_name='tasks')
#     task_end_status = models.CharField(max_length=2, choices=TASKS_STATUS)
# 
#     class Meta:
#         unique_together = ('sprint', 'task')
# 
# Path: scrum/forms.py
# class ProjectForm(ModelForm):
#     class Meta:
#         model = Project
# 
# class StoryForm(ModelForm):
#     class Meta:
#         model = Story
# 
# class TaskForm(ModelForm):
#     class Meta:
#         model = Task
# 
# class SprintTasksForm(ModelForm):
#     class Meta:
#         model = SprintTasks


from django.views.generic.detail import DetailView
from django.views.generic.list import ListView
from django.http import HttpResponse, Http404, HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.db.models import Q
from .models import Project, Story, Task, Sprint, SprintTasks
from .forms import ProjectForm, StoryForm, TaskForm, SprintTasksForm
import json
        f = SprintTasksForm(post_data)
        if f.is_valid():
            f.save()
        else:
            print f.errors

        return HttpResponse()
    else:
        raise Http404


def add_sprint(request, pk):
    if request.method == 'GET':
        newSprint = Sprint.objects.create(project_id=pk)
        response = {'id': newSprint.id, 'number': newSprint.number}
        return HttpResponse(json.dumps(response), content_type="application/json")
    else:
        raise Http404


def close_sprint(request, pk):
    if request.method == 'POST':

        response_data = {}
        #If all tasks are not in backlog or done
        if SprintTasks.objects.filter(sprint__pk=pk).exclude(Q(task__status='DO') | Q(task__status='BA')).count() != 0:
            response_data['error_message'] = 'All tasks must be done or backloged!'
            return HttpResponse(json.dumps(response_data), content_type="application/json")

        #We change the task end status of each sprinttask

# Path: scrum/models.py
# class Project(models.Model):
#     name = models.CharField(max_length=255, blank=True)
#     description = models.TextField(blank=True)
#     users = models.ManyToManyField(User, related_name='Project_users', blank=True, null=True)
# 
#     class Meta:
#         ordering = ('id',)
# 
#     def __unicode__(self):
#         return "%s" % (self.name, )
# 
# class Story(models.Model):
#     title = models.CharField(max_length=255)
#     note = models.TextField(blank=True)
#     last_modified = models.DateTimeField(auto_now=True)
#     project = models.ForeignKey(Project, related_name='stories')
#     estimated_time = models.FloatField(validators=[MinValueValidator(0), MaxValueValidator(5)])
# 
#     class Meta:
#         ordering = ('title',)
# 
#     def __unicode__(self):
#         return "%s" % (self.title, )
# 
# class Task(models.Model):
#     title = models.CharField(max_length=255)
#     note = models.TextField(blank=True)
#     status = models.CharField(max_length=2, choices=TASKS_STATUS)
#     last_modified = models.DateTimeField(auto_now=True)
#     assigned_to = models.ForeignKey(User, related_name='Task_users', blank=True, null=True)
#     story = models.ForeignKey(Story, related_name='Task_story')
#     estimated_time = models.FloatField(validators=[MinValueValidator(0), MaxValueValidator(8)])
#     objects = TaskManager()
# 
#     class Meta:
#         ordering = ('id',)
#         verbose_name = "Task"
# 
#     def __unicode__(self):
#         return "%s" % (self.title,)
# 
# class Sprint(models.Model):
#     number = models.IntegerField()
#     is_closed = models.BooleanField()
#     project = models.ForeignKey(Project, related_name='sprints')
#     last_modified = models.DateTimeField(auto_now=True)
#     tasks = models.ManyToManyField(Task, through='SprintTasks', blank=True, null=True)
#     objects = SprintManager()
# 
#     class Meta:
#         ordering = ('number',)
#         unique_together = ('number', 'project')
# 
#     def __unicode__(self):
#         return "sprint %s" % (self.number, )
# 
#     def save(self, *args, **kwargs):
#         if self.id is None:
#             self.number = (Sprint.objects.filter(project_id=self.project_id).aggregate(Max('number'))['number__max'] or 0) + 1
# 
#         super(Sprint, self).save(*args, **kwargs)
# 
# class SprintTasks(models.Model):
#     sprint = models.ForeignKey(Sprint, related_name='sprints')
#     task = models.ForeignKey(Task, related_name='tasks')
#     task_end_status = models.CharField(max_length=2, choices=TASKS_STATUS)
# 
#     class Meta:
#         unique_together = ('sprint', 'task')
# 
# Path: scrum/forms.py
# class ProjectForm(ModelForm):
#     class Meta:
#         model = Project
# 
# class StoryForm(ModelForm):
#     class Meta:
#         model = Story
# 
# class TaskForm(ModelForm):
#     class Meta:
#         model = Task
# 
# class SprintTasksForm(ModelForm):
#     class Meta:
#         model = SprintTasks


# Path: scrum/views.py
from django.views.generic.detail import DetailView
from django.views.generic.list import ListView
from django.http import HttpResponse, Http404, HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.db.models import Q
from .models import Project, Story, Task, Sprint, SprintTasks
from .forms import ProjectForm, StoryForm, TaskForm, SprintTasksForm
import json

        f = SprintTasksForm(post_data)
        if f.is_valid():
            f.save()
        else:
            print f.errors

        return HttpResponse()
    else:
        raise Http404


def add_sprint(request, pk):
    if request.method == 'GET':
        newSprint = Sprint.objects.create(project_id=pk)
        response = {'id': newSprint.id, 'number': newSprint.number}
        return HttpResponse(json.dumps(response), content_type="application/json")
    else:
        raise Http404


def close_sprint(request, pk):
    if request.method == 'POST':

        response_data = {}
        #If all tasks are not in backlog or done
        if SprintTasks.objects.filter(sprint__pk=pk).exclude(Q(task__status='DO') | Q(task__status='BA')).count() != 0:
            response_data['error_message'] = 'All tasks must be done or backloged!'
            return HttpResponse(json.dumps(response_data), content_type="application/json")

        #We change the task end status of each sprinttask

        for st in SprintTasks.objects.filter(sprint__pk=pk):