# 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
                    if sprint.is_closed:
                        response_data['error_message'] = 'This sprint is closed!'
                        return HttpResponse(json.dumps(response_data), content_type="application/json")

                post_values['status'] = post_status
                post_values['estimated_time'] = task.estimated_time
                post_values['story'] = task.story.pk
                post_values['title'] = task.title
                post_values['assigned_to'] = None
                
                if task.assigned_to:
                    post_values['assigned_to'] = task.assigned_to.pk

                #if in progress, affect user
                if post_status == 'IN':
                    post_values['assigned_to'] = request.user.pk
                  
                #if to do, delete assignated user    
                elif post_status == 'TO':
                    post_values['assigned_to'] = None

                #Only manager can move to or from backlog
                elif (old_task_status != 'BA' and post_status == 'BA') or (old_task_status == 'BA' and post_status != 'BA'):
                    #If the user is not a manager
                    if not(is_manager(request.user)):

                        response_data['error_message'] = 'You are not allowed to do that!'
                        return HttpResponse(json.dumps(response_data), content_type="application/json")

            #Call form with post values

# 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

                    if sprint.is_closed:
                        response_data['error_message'] = 'This sprint is closed!'
                        return HttpResponse(json.dumps(response_data), content_type="application/json")

                post_values['status'] = post_status
                post_values['estimated_time'] = task.estimated_time
                post_values['story'] = task.story.pk
                post_values['title'] = task.title
                post_values['assigned_to'] = None
                
                if task.assigned_to:
                    post_values['assigned_to'] = task.assigned_to.pk

                #if in progress, affect user
                if post_status == 'IN':
                    post_values['assigned_to'] = request.user.pk
                  
                #if to do, delete assignated user    
                elif post_status == 'TO':
                    post_values['assigned_to'] = None

                #Only manager can move to or from backlog
                elif (old_task_status != 'BA' and post_status == 'BA') or (old_task_status == 'BA' and post_status != 'BA'):
                    #If the user is not a manager
                    if not(is_manager(request.user)):

                        response_data['error_message'] = 'You are not allowed to do that!'
                        return HttpResponse(json.dumps(response_data), content_type="application/json")

            #Call form with post values

            f = TaskForm(post_values, instance=task)