# Path: survey/models.py
# class Survey(models.Model):
#     user = models.ForeignKey(User)
#     key = models.CharField(max_length=22, null=False, unique=True)
#     title = models.CharField(max_length=128, null=False)
#     description = models.TextField(null=True)
#     last_modified = models.DateTimeField(null=True)
#     is_published = models.BooleanField(null=False, default=False)
#     theme_name = models.CharField(max_length=255, null=True)
#     deadline = models.DateTimeField(null=True)
#     # create a unique id for the survey
#     def __init__(self, *args, **kwargs):
#         super(Survey, self).__init__(*args, **kwargs)
#         if not self.key:
#             self.key = shortuuid.uuid()
#         if not self.last_modified:
#             self.last_modified = datetime.now()
#         if kwargs.get('title'):
#             self.title = kwargs['title']
# 
#     def __unicode__(self):
#         return 'id=%s, key = %s, user=%s, title = %s last_modified = %s is_published=%s' % (
#             self.id,
#             self.key,
#             self.user.username,
#             self.title,
#             self.last_modified.strftime('%H:%M, %d %B, %Y'),
#             self.is_published)
# 
#     def get_absolute_url(self):
#         return "/view_survey/%s" % self.key
# 
#     def get_collaborators(self):
#         collaborations = Collaboration.objects.filter(survey_id=self.id)
#         users_ids = [c.user_id for c in collaborations]
#         return User.objects.filter(id__in=users_ids)
# 
#     def get_collaboration(self):
#         collaborations = Collaboration.objects.filter(survey_id=self.id)
#         return collaborations
# 
#     def is_collaborator(self, user):
#         if Collaboration.objects.filter(survey_id=self.id, user_id=user.id, is_active=True).exists():
#             return True
#         else:
#             return False
# 
#     class Meta:
#         ordering = ['id']
# 
# class Collaboration(models.Model):
#     user = models.ForeignKey(User)
#     survey = models.ForeignKey(Survey)
#     activation_code = models.CharField(max_length=20, blank=True, null=True)
#     is_active = models.BooleanField(default=False)
#     date_sent = models.DateTimeField(null=False, auto_now_add=True)


from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.shortcuts import render_to_response
from survey.models import Survey, Collaboration
from django.contrib.auth.models import User
from django.core.mail import send_mass_mail
from django.contrib import messages
from django.contrib.sites.models import get_current_site
from django.template import RequestContext
from django.contrib.auth.decorators import login_required
import sha
import shortuuid
import time


        return HttpResponseRedirect("/account")
    else:
        return HttpResponseRedirect("/account")

def accept(request, activation_code):
    try:
        collaboration = Collaboration.objects.get(activation_code=activation_code)
        collaboration.is_active = True
        collaboration.save()
        survey = Survey.objects.get(id=collaboration.survey_id)
        return render_to_response("collaborate_accept.html", { 'survey': survey }, context_instance=RequestContext(request))
    except Collaboration.DoesNotExist:
        messages.error(request, 'Sorry, the survey you trying to collaborate have been deleted by the owner')
        return HttpResponseRedirect("/")

@login_required
def delete(request, survey_id):
    Collaboration.objects.get(user_id=request.user.id, survey_id=survey_id ).delete();
    messages.success(request, 'You have successfully delete the survey')
    return HttpResponseRedirect("/account")

@login_required
def remove_collaborator(request, survey_id, user_id):
    # Only allow to remove their own survey collaborator
    try:
        Survey.objects.get(user_id=request.user.id, id=survey_id)
        Collaboration.objects.get(user_id=user_id, survey_id=survey_id).delete();
        messages.success(request, 'You have successfully remove the collaborator')
        return HttpResponseRedirect("/account")

# Path: survey/models.py
# class Survey(models.Model):
#     user = models.ForeignKey(User)
#     key = models.CharField(max_length=22, null=False, unique=True)
#     title = models.CharField(max_length=128, null=False)
#     description = models.TextField(null=True)
#     last_modified = models.DateTimeField(null=True)
#     is_published = models.BooleanField(null=False, default=False)
#     theme_name = models.CharField(max_length=255, null=True)
#     deadline = models.DateTimeField(null=True)
#     # create a unique id for the survey
#     def __init__(self, *args, **kwargs):
#         super(Survey, self).__init__(*args, **kwargs)
#         if not self.key:
#             self.key = shortuuid.uuid()
#         if not self.last_modified:
#             self.last_modified = datetime.now()
#         if kwargs.get('title'):
#             self.title = kwargs['title']
# 
#     def __unicode__(self):
#         return 'id=%s, key = %s, user=%s, title = %s last_modified = %s is_published=%s' % (
#             self.id,
#             self.key,
#             self.user.username,
#             self.title,
#             self.last_modified.strftime('%H:%M, %d %B, %Y'),
#             self.is_published)
# 
#     def get_absolute_url(self):
#         return "/view_survey/%s" % self.key
# 
#     def get_collaborators(self):
#         collaborations = Collaboration.objects.filter(survey_id=self.id)
#         users_ids = [c.user_id for c in collaborations]
#         return User.objects.filter(id__in=users_ids)
# 
#     def get_collaboration(self):
#         collaborations = Collaboration.objects.filter(survey_id=self.id)
#         return collaborations
# 
#     def is_collaborator(self, user):
#         if Collaboration.objects.filter(survey_id=self.id, user_id=user.id, is_active=True).exists():
#             return True
#         else:
#             return False
# 
#     class Meta:
#         ordering = ['id']
# 
# class Collaboration(models.Model):
#     user = models.ForeignKey(User)
#     survey = models.ForeignKey(Survey)
#     activation_code = models.CharField(max_length=20, blank=True, null=True)
#     is_active = models.BooleanField(default=False)
#     date_sent = models.DateTimeField(null=False, auto_now_add=True)


# Path: survey/collaborate_views.py
from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.shortcuts import render_to_response
from survey.models import Survey, Collaboration
from django.contrib.auth.models import User
from django.core.mail import send_mass_mail
from django.contrib import messages
from django.contrib.sites.models import get_current_site
from django.template import RequestContext
from django.contrib.auth.decorators import login_required
import sha
import shortuuid
import time


        return HttpResponseRedirect("/account")
    else:
        return HttpResponseRedirect("/account")

def accept(request, activation_code):
    try:
        collaboration = Collaboration.objects.get(activation_code=activation_code)
        collaboration.is_active = True
        collaboration.save()
        survey = Survey.objects.get(id=collaboration.survey_id)
        return render_to_response("collaborate_accept.html", { 'survey': survey }, context_instance=RequestContext(request))
    except Collaboration.DoesNotExist:
        messages.error(request, 'Sorry, the survey you trying to collaborate have been deleted by the owner')
        return HttpResponseRedirect("/")

@login_required
def delete(request, survey_id):
    Collaboration.objects.get(user_id=request.user.id, survey_id=survey_id ).delete();
    messages.success(request, 'You have successfully delete the survey')
    return HttpResponseRedirect("/account")

@login_required
def remove_collaborator(request, survey_id, user_id):
    # Only allow to remove their own survey collaborator
    try:
        Survey.objects.get(user_id=request.user.id, id=survey_id)
        Collaboration.objects.get(user_id=user_id, survey_id=survey_id).delete();
        messages.success(request, 'You have successfully remove the collaborator')
        return HttpResponseRedirect("/account")

    except Survey.DoesNotExist:
