# Path: classphoto/models.py
# def _bio2dict( bio_db ):
# def save_bio( email, sequence, name, bio, avatar, twitter=None, gplus=None):
# def has_bio( email, sequence ):
# def get_bio( email ):
# def get_bios( sequence, limit=100 ):
# def get_bios_by_email( sequence, emails ):
# 
# Path: classphoto/utils.py
# def create_s3_policy_doc(bucket, upload_path, redirect=None, max_file_size=2**20):
#     policy_template = """{{"expiration": "{expiration_date}",
#   "conditions": [ 
#     {{"bucket": "{bucket}"}}, 
#     ["starts-with", "$key", "{upload_path}"],
#     {{"acl": "public-read"}},
#     {{"success_action_status": "201"}},
#     ["starts-with", "$Content-Type", ""],
#     ["content-length-range", 0, {max_file_size}]
#   ]
# }}"""
# 
#     expiration_date = datetime.datetime.utcnow() + datetime.timedelta(minutes=15)
#     policy_doc = policy_template.format(
#         expiration_date=expiration_date.strftime("%Y-%M-%dT%H:%m:%SZ"),
#         bucket=bucket, 
#         upload_path=upload_path,
#         redirect=redirect,
#         max_file_size=max_file_size
#     )
#     policy_b64 = base64.b64encode(policy_doc)
#     signature = base64.b64encode(hmac.new(settings.AWS_SECRET_ACCESS_KEY, policy_b64, hashlib.sha1).digest())
# 
#     return policy_b64, signature
# 
# Path: classphoto/emails.py
# def send_user_link( email, key ):
#     context = {
#         'email': email,
#         'key': key,
#         'mooc_title': settings.MOOC_TITLE,
#         'mooc_domain': settings.MOOC_DOMAIN
#     }
#     subject = render_to_string('classphoto/emails/user-link-subject.txt', context).strip()
#     text_body = render_to_string('classphoto/emails/user-link.txt', context).strip()
#     html_body = render_to_string('classphoto/emails/user-link.html', context).strip()
#     mailgun.api.send_email(email, settings.DEFAULT_FROM_EMAIL, subject, text_body, html_body, tags=['user_link'])
# 
# Path: signup/models.py
# def create_signup( email, questions ):
# def update_signup( email, questions ):
# def create_or_update_signup( email, questions ):
# def delete_signup( email, sequence ):
# def _signup2json( signup_db ):
# def get_signup( email, sequence ):
# def get_all_user_signups( email ):
# def get_signup_by_invite_code( invite_code ):
# def get_signups( sequence ):
# def get_signups_for_archiving( sequence ):
# def get_new_signups( ):
# def handle_new_signups( ):
# def add_user_to_global_list( email, sequence ):
# 
# Path: sequence/models.py
# def sequence_list_name( sequence_number ):
# def sequence_campaign( sequence_number ):
# def sequence2dict( sequence_db ):
# def create_sequence( start_date, signup_close_date ):
# def get_all_sequences( ):
# def get_current_sequence( ):
# def get_current_sequence_number( ):


from django.shortcuts import render_to_response
from django.template import RequestContext
from django.conf import settings
from django import http
from django.core.urlresolvers import reverse
from django.views.decorators.http import require_http_methods
from django.contrib import messages
from classphoto import models as classphoto_api
from classphoto.utils import create_s3_policy_doc
from classphoto.emails import send_user_link
from signup import models as signup_api
from sequence import models as sequence_api
import hmac, hashlib
import random
    else:
        # make a gap at position 12
        bios[11] = {'email': ''}

    context = {
        'bios': bios,
        'user_bio': user_bio,
        'user_email': request.session.get('user_email'),
        'sequence': sequence,
        's3_policy': s3_policy,
        's3_signature': signature,
        'AWS_ACCESS_KEY_ID': settings.AWS_ACCESS_KEY_ID,
        'AWS_S3_BUCKET': settings.AWS_S3_BUCKET,
        'key_prefix': 'classphoto/{0}'.format(prefix)
    }
    
    return render_to_response('classphoto/index.html', context, context_instance=RequestContext(request))


@require_http_methods(['POST'])
def save_bio(request, sequence):
    """ receive AJAX post from class classphoto page """

    url = reverse('classphoto_classphoto', kwargs={'sequence': sequence})

    # TODO validate data on the server side also!

    # check if user signed up for the mooc
    signed_up = False
    try:

# Path: classphoto/models.py
# def _bio2dict( bio_db ):
# def save_bio( email, sequence, name, bio, avatar, twitter=None, gplus=None):
# def has_bio( email, sequence ):
# def get_bio( email ):
# def get_bios( sequence, limit=100 ):
# def get_bios_by_email( sequence, emails ):
# 
# Path: classphoto/utils.py
# def create_s3_policy_doc(bucket, upload_path, redirect=None, max_file_size=2**20):
#     policy_template = """{{"expiration": "{expiration_date}",
#   "conditions": [ 
#     {{"bucket": "{bucket}"}}, 
#     ["starts-with", "$key", "{upload_path}"],
#     {{"acl": "public-read"}},
#     {{"success_action_status": "201"}},
#     ["starts-with", "$Content-Type", ""],
#     ["content-length-range", 0, {max_file_size}]
#   ]
# }}"""
# 
#     expiration_date = datetime.datetime.utcnow() + datetime.timedelta(minutes=15)
#     policy_doc = policy_template.format(
#         expiration_date=expiration_date.strftime("%Y-%M-%dT%H:%m:%SZ"),
#         bucket=bucket, 
#         upload_path=upload_path,
#         redirect=redirect,
#         max_file_size=max_file_size
#     )
#     policy_b64 = base64.b64encode(policy_doc)
#     signature = base64.b64encode(hmac.new(settings.AWS_SECRET_ACCESS_KEY, policy_b64, hashlib.sha1).digest())
# 
#     return policy_b64, signature
# 
# Path: classphoto/emails.py
# def send_user_link( email, key ):
#     context = {
#         'email': email,
#         'key': key,
#         'mooc_title': settings.MOOC_TITLE,
#         'mooc_domain': settings.MOOC_DOMAIN
#     }
#     subject = render_to_string('classphoto/emails/user-link-subject.txt', context).strip()
#     text_body = render_to_string('classphoto/emails/user-link.txt', context).strip()
#     html_body = render_to_string('classphoto/emails/user-link.html', context).strip()
#     mailgun.api.send_email(email, settings.DEFAULT_FROM_EMAIL, subject, text_body, html_body, tags=['user_link'])
# 
# Path: signup/models.py
# def create_signup( email, questions ):
# def update_signup( email, questions ):
# def create_or_update_signup( email, questions ):
# def delete_signup( email, sequence ):
# def _signup2json( signup_db ):
# def get_signup( email, sequence ):
# def get_all_user_signups( email ):
# def get_signup_by_invite_code( invite_code ):
# def get_signups( sequence ):
# def get_signups_for_archiving( sequence ):
# def get_new_signups( ):
# def handle_new_signups( ):
# def add_user_to_global_list( email, sequence ):
# 
# Path: sequence/models.py
# def sequence_list_name( sequence_number ):
# def sequence_campaign( sequence_number ):
# def sequence2dict( sequence_db ):
# def create_sequence( start_date, signup_close_date ):
# def get_all_sequences( ):
# def get_current_sequence( ):
# def get_current_sequence_number( ):


# Path: classphoto/views.py
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.conf import settings
from django import http
from django.core.urlresolvers import reverse
from django.views.decorators.http import require_http_methods
from django.contrib import messages
from classphoto import models as classphoto_api
from classphoto.utils import create_s3_policy_doc
from classphoto.emails import send_user_link
from signup import models as signup_api
from sequence import models as sequence_api
import hmac, hashlib
import random

    else:
        # make a gap at position 12
        bios[11] = {'email': ''}

    context = {
        'bios': bios,
        'user_bio': user_bio,
        'user_email': request.session.get('user_email'),
        'sequence': sequence,
        's3_policy': s3_policy,
        's3_signature': signature,
        'AWS_ACCESS_KEY_ID': settings.AWS_ACCESS_KEY_ID,
        'AWS_S3_BUCKET': settings.AWS_S3_BUCKET,
        'key_prefix': 'classphoto/{0}'.format(prefix)
    }
    
    return render_to_response('classphoto/index.html', context, context_instance=RequestContext(request))


@require_http_methods(['POST'])
def save_bio(request, sequence):
    """ receive AJAX post from class classphoto page """

    url = reverse('classphoto_classphoto', kwargs={'sequence': sequence})

    # TODO validate data on the server side also!

    # check if user signed up for the mooc
    signed_up = False
    try:

        signup = signup_api.get_signup(request.POST['email'], sequence)