from django.conf import settings
from django.core.urlresolvers import reverse
from django.shortcuts import redirect
from django_future.csrf import ensure_csrf_cookie
from mitxmako.shortcuts import render_to_response
from student.models import ResourceLibrary,StaticContent
from collections import deque
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
from django.contrib.auth.decorators import login_required
from django.contrib.auth.decorators import user_passes_test


from django.contrib.auth.models import User
from student.models import UserProfile,Registration,CourseEnrollmentAllowed
from django import db
import random
import json
import time
import logging
import csv
from multiprocessing import Process
from django.core.validators import validate_email, validate_slug, ValidationError

log = logging.getLogger("tracking")

def postpone(function):
    def decorator(*args, **kwargs):
        p = Process(target = function, args=args, kwargs=kwargs)
        p.daemon = True
        p.start()
    return decorator

@login_required
@user_passes_test(lambda u: u.is_superuser)
def import_user(request):
    return render_to_response('configuration/import_user.html', {})

@login_required
@user_passes_test(lambda u: u.is_superuser)
def import_user_submit(request):
    if request.method == 'POST':
        do_import_user(request.FILES['file'])
        return HttpResponse(json.dumps({'success': True}))
    else:
        return HttpResponse('')

USER_CSV_COLS=('email',)

@postpone
def do_import_user(file):
    #** ==================== testing 
    # curr=""
    # while 1:
    #     now=time.strftime("%Y-%m-%d %X", time.localtime() )
    #     if now!=curr:
    #         log.debug(now)
    #         curr=now
    
    #** ==================== importing
    # message={}
    try:
        count_success=0
        r=csv.reader(file,delimiter='\t', quotechar='|', quoting=csv.QUOTE_MINIMAL)
        rl = []
        rl.extend(r)

        #** import into cohort
        # cohort_id=request.POST.get("cohort_id")
        # cohort=Cohort.objects.get(id=cohort_id)
        # if cohort.licences < UserProfile.objects.filter(~Q(subscription_status = "Inactive"),cohort_id=cohort_id).count() + len(rl):
        #     raise Exception("Licences limit exceeded")
        
        for line in rl:
            validate_user_cvs_line(line)

            email=line[USER_CSV_COLS.index('email')]

            #** generating origin username
            username="".join(random.sample('abcdefg&#%^*1234567890',20))

            #** user
            user = User(username=username, email=email, is_active=False)
            user.set_password(username)
            user.save()

            #** registration
            registration = Registration()
            registration.register(user)

            #** profile
            profile=UserProfile(user=user)
            # profile.cohort_id=cohort_id
            profile.subscription_status="Imported"
            profile.save()

            #** course enroll
            cea, _ = CourseEnrollmentAllowed.objects.get_or_create(course_id='PCG/PEP101x/2014_Spring', email=email)
            cea.is_active = True
            cea.auto_enroll = True
            cea.save()

            #** email
            # reg = Registration.objects.get(user=user)
            # d = {'name': profile.name, 'key': reg.activation_key}
            # subject = render_to_string('emails/activation_email_subject.txt', d)
            # subject = ''.join(subject.splitlines())
            # message = render_to_string('emails/activation_emailh.txt', d)
            # send_html_mail(subject, body, settings.SUPPORT_EMAIL, [email])

            #** count success
            count_success=count_success+1
            
        db.transaction.commit()
    except Exception as e:
        db.transaction.rollback()
        log.debug("import error: %s" % e)

def validate_user_cvs_line(line):
    #** check field count
    n=0
    for item in line:
        if len(item.strip()):
            n=n+1
    if n != len(USER_CSV_COLS):
        raise Exception("Wrong fields count")

    #** email
    email=line[USER_CSV_COLS.index('email')]
    validate_email(email)

    #** check user exists
    if len(User.objects.filter(email=email)) > 0:
        raise Exception("An account with the Email '{email}' already exists".format(email=email))


@@ -1823,21 +1823,8 @@
 =%7B%7D%0A
-    try:%0A    
     
@@ -1839,20 +1839,16 @@
 ccess=0%0A
-    
     r=cs
@@ -1927,20 +1927,16 @@
     
- 
-   
 rl = %5B%5D%0A
     
@@ -1931,20 +1931,16 @@
 rl = %5B%5D%0A
-    
     rl.e
@@ -1949,28 +1949,24 @@
 end(r)%0A%0A    
-  
-  
 #** import i
@@ -1976,20 +1976,16 @@
  cohort%0A
-    
     # co
@@ -2026,20 +2026,16 @@
 d%22)%0A    
-  
-  
 # cohort
@@ -2068,20 +2068,16 @@
 ort_id)%0A
-    
     # if
@@ -2199,20 +2199,16 @@
 en(rl):%0A
-    
     #   
@@ -2252,29 +2252,17 @@
 eeded%22)%0A
-        %0A    
+%0A
     for 
@@ -2273,16 +2273,29 @@
  in rl:%0A
+        try:%0A
         
@@ -3719,32 +3719,36 @@
        %0A        
+    
 db.transaction.c
@@ -3751,24 +3751,28 @@
 on.commit()%0A
+    
     except E
@@ -3786,32 +3786,36 @@
 n as e:%0A        
+    
 db.transaction.r
@@ -3824,16 +3824,20 @@
 lback()%0A
+    
         

Test importing by subprocess