# 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

    collaborators_data = request.POST.get('collaborators');

    # Check whether is a valid request
    if request.method == 'POST' and (len(survey_id) > 0) and (len(collaborators_data) > 0):

        collaborators_username = [u.strip() for u in collaborators_data.split(',')]

        emails = []
        survey = Survey.objects.get(id=survey_id)
        success_user = []
        fail_user = []

        for username in collaborators_username:
            user = None
            try:
                user = User.objects.get(username=username)
            except User.DoesNotExist:
                pass

            try:
                if user is None:
                    user = User.objects.get(email=username)

                if user.id == request.user.id:
                    messages.error(request, "You cannot add yourself as collaborator")
                else:
                    activation_code = shortuuid.uuid()
                    activation_code = activation_code[0:9]

                    if not Collaboration.objects.filter(user=user, survey=survey).exists():

# 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

    collaborators_data = request.POST.get('collaborators');

    # Check whether is a valid request
    if request.method == 'POST' and (len(survey_id) > 0) and (len(collaborators_data) > 0):

        collaborators_username = [u.strip() for u in collaborators_data.split(',')]

        emails = []
        survey = Survey.objects.get(id=survey_id)
        success_user = []
        fail_user = []

        for username in collaborators_username:
            user = None
            try:
                user = User.objects.get(username=username)
            except User.DoesNotExist:
                pass

            try:
                if user is None:
                    user = User.objects.get(email=username)

                if user.id == request.user.id:
                    messages.error(request, "You cannot add yourself as collaborator")
                else:
                    activation_code = shortuuid.uuid()
                    activation_code = activation_code[0:9]

                    if not Collaboration.objects.filter(user=user, survey=survey).exists():

                        Collaboration.objects.create(user=user, survey=survey, activation_code=activation_code)
