<commit_before>'''
Created on Jun 18, 2012

Testing views. Each of these views is referenced in urls.py 

@author: Sjoerd
'''
import pdb
import mimetypes
import logging
 

from itertools import chain
from os import path
from django import forms
from django.db import models
from django.conf import settings
from django.contrib.admin.options import ModelAdmin
from django.contrib.auth.models import Group
from django.core.exceptions import ImproperlyConfigured
from django.core.files import File
from django.core.mail import send_mail
from django.core.urlresolvers import reverse
from django.http import HttpResponse,Http404,HttpResponseForbidden
from django.shortcuts import render_to_response,get_object_or_404
from django.template import RequestContext,Context,Template,TemplateSyntaxError
from django.template.defaulttags import VerbatimNode

from userena import views as userena_views

from comicmodels.models import ComicSite,Page,ErrorPage,DropboxFolder,ComicSiteModel,RegistrationRequest,ProjectMetaData
from comicsite.admin import ComicSiteAdmin
from comicsite.core.urlresolvers import reverse
from comicsite.template.context import ComicSiteRequestContext
from comicsite.models import ComicSiteException,send_existing_project_link_submission_notification_email


from filetransfers.api import serve_file
from filetransfers.views import download_handler_file
from dataproviders import FileSystemDataProvider


def index(request):
    return  HttpResponse("ComicSite index page.",context_instance=RequestContext(request))


def _register(request, site_short_name):
    """ Register the current user for given comicsite """
   
    #TODO: check whether user is allowed to register, maybe wait for verification,
    #send email to admins of new registration
    
    [site, pages, metafooterpages] = site_get_standard_vars(site_short_name)
    if request.user.is_authenticated():
        if site.require_participant_review:
            currentpage = _register_after_approval(request, site)
        else:
            currentpage = _register_directly(request, site)
        
    else:
        if "user_just_registered" in request.GET:
            # show message to use activation mail first, then refresh the page
            html = """<h2> Please activate your account </h2> 
            <p>An activation link has been sent to the email adress you provided.
            Please use this link to activate your account.</p> 
            
            After activating your account, click <a href="{0}">here to continue</a>  
            """.format("")
            
            currentpage = Page(comicsite=site, title="activate_your_account", display_title="activate your account", html=html)
        else: 
            html = "you need to be logged in to use this url"
            currentpage = Page(comicsite=site, title="please_log_in", display_title="Please log in", html=html)
        
    return render_to_response('page.html', {'site': site, 'currentpage': currentpage, "pages":pages},context_instance=RequestContext(request))


def _register_directly(request, project):
    
    title = "registration_successful"
    display_title = "registration successful"
    if request.user.is_authenticated():
        project.add_participant(request.user)        
        html = "<p> You are now registered to " + project.short_name + "<p>"
    
        
    currentpage = Page(comicsite=project, title=title, display_title=display_title, html=html)
    return currentpage


def _register_after_approval(request, project):
    
    title = "registration requested"
    display_title = "registration requested"
            
    pending = RegistrationRequest.objects.get_pending_registration_requests(request.user,project)
    accepted = RegistrationRequest.objects.get_accepted_registration_requests(request.user,project)
    
    pending_or_accepted = list(chain(pending,accepted))
    
    if pending_or_accepted:            
        html = pending_or_accepted[0].status_to_string()        
        pass #do not add another request
    else:
        participantsgroup = Group.objects.get(name=project.participants_group_name())
        reg_request = RegistrationRequest()
        reg_request.project = project
        reg_request.user = request.user
        reg_request.save()
        
        from comicsite.models import send_participation_request_notification_email
        send_participation_request_notification_email(request,reg_request)
        
        html = "<p> A participation request has been sent to the " + project.short_name + " organizers.You will receive an email when your request has been reviewed<p>"
            

    
    currentpage = Page(comicsite=project, title=title, display_title=display_title, html=html)
    return currentpage


def site(request, site_short_name):    
   
    #TODO: Doing two calls to getSite here. (second one in site_get_standard_vars)
    # How to handle not found nicely? Throwing exception in site_get_standard_vars<commit_msg>fixed typo<commit_after>'''
Created on Jun 18, 2012

Testing views. Each of these views is referenced in urls.py 

@author: Sjoerd
'''
import pdb
import mimetypes
import logging
 

from itertools import chain
from os import path
from django import forms
from django.db import models
from django.conf import settings
from django.contrib.admin.options import ModelAdmin
from django.contrib.auth.models import Group
from django.core.exceptions import ImproperlyConfigured
from django.core.files import File
from django.core.mail import send_mail
from django.core.urlresolvers import reverse
from django.http import HttpResponse,Http404,HttpResponseForbidden
from django.shortcuts import render_to_response,get_object_or_404
from django.template import RequestContext,Context,Template,TemplateSyntaxError
from django.template.defaulttags import VerbatimNode

from userena import views as userena_views

from comicmodels.models import ComicSite,Page,ErrorPage,DropboxFolder,ComicSiteModel,RegistrationRequest,ProjectMetaData
from comicsite.admin import ComicSiteAdmin
from comicsite.core.urlresolvers import reverse
from comicsite.template.context import ComicSiteRequestContext
from comicsite.models import ComicSiteException,send_existing_project_link_submission_notification_email


from filetransfers.api import serve_file
from filetransfers.views import download_handler_file
from dataproviders import FileSystemDataProvider


def index(request):
    return  HttpResponse("ComicSite index page.",context_instance=RequestContext(request))


def _register(request, site_short_name):
    """ Register the current user for given comicsite """
   
    #TODO: check whether user is allowed to register, maybe wait for verification,
    #send email to admins of new registration
    
    [site, pages, metafooterpages] = site_get_standard_vars(site_short_name)
    if request.user.is_authenticated():
        if site.require_participant_review:
            currentpage = _register_after_approval(request, site)
        else:
            currentpage = _register_directly(request, site)
        
    else:
        if "user_just_registered" in request.GET:
            # show message to use activation mail first, then refresh the page
            html = """<h2> Please activate your account </h2> 
            <p>An activation link has been sent to the email adress you provided.
            Please use this link to activate your account.</p> 
            
            After activating your account, click <a href="{0}">here to continue</a>  
            """.format("")
            
            currentpage = Page(comicsite=site, title="activate_your_account", display_title="activate your account", html=html)
        else: 
            html = "you need to be logged in to use this url"
            currentpage = Page(comicsite=site, title="please_log_in", display_title="Please log in", html=html)
        
    return render_to_response('page.html', {'site': site, 'currentpage': currentpage, "pages":pages},context_instance=RequestContext(request))


def _register_directly(request, project):
    
    title = "registration_successful"
    display_title = "registration successful"
    if request.user.is_authenticated():
        project.add_participant(request.user)        
        html = "<p> You are now registered to " + project.short_name + "<p>"
    
        
    currentpage = Page(comicsite=project, title=title, display_title=display_title, html=html)
    return currentpage


def _register_after_approval(request, project):
    
    title = "registration requested"
    display_title = "registration requested"
            
    pending = RegistrationRequest.objects.get_pending_registration_requests(request.user,project)
    accepted = RegistrationRequest.objects.get_accepted_registration_requests(request.user,project)
    
    pending_or_accepted = list(chain(pending,accepted))
    
    if pending_or_accepted:            
        html = pending_or_accepted[0].status_to_string()        
        pass #do not add another request
    else:
        participantsgroup = Group.objects.get(name=project.participants_group_name())
        reg_request = RegistrationRequest()
        reg_request.project = project
        reg_request.user = request.user
        reg_request.save()
        
        from comicsite.models import send_participation_request_notification_email
        send_participation_request_notification_email(request,reg_request)
        
        html = "<p> A participation request has been sent to the " + project.short_name + " organizers. You will receive an email when your request has been reviewed<p>"
            

    
    currentpage = Page(comicsite=project, title=title, display_title=display_title, html=html)
    return currentpage


def site(request, site_short_name):    
   
    #TODO: Doing two calls to getSite here. (second one in site_get_standard_vars)
    # How to handle not found nicely? Throwing exception in site_get_standard_vars
{'fraction_non_alphanumeric': 0.06868849166499297, 'fraction_numerical': 0.0032134966860815424, 'mean_word_length': 3.808788025108643, 'pattern_counts': {'":': 2, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6927167', 'n_tokens_mistral': 2697, 'n_tokens_neox': 2594, 'n_words': 757}