<commit_before>import os, urllib2

from django.core.urlresolvers import reverse
from django.contrib.auth.decorators import login_required
from django.shortcuts import render_to_response
from django.template import RequestContext
from django import forms
from django.db import IntegrityError
from django.contrib.auth.models import User
from django.views.decorators.http import require_GET, require_POST
from django.http import HttpResponse, HttpResponseBadRequest, \
    HttpResponseRedirect, HttpResponseNotAllowed, HttpResponseForbidden
from pyxform.errors import PyXFormError
from odk_viewer.models import DataDictionary
from odk_viewer.models.data_dictionary import upload_to
from main.models import UserProfile, MetaData
from odk_logger.models import Instance, XForm
from utils.logger_tools import response_with_mimetype_and_name
from utils.decorators import is_owner
from odk_logger.models.xform import XLSFormError
from utils.user_auth import check_and_set_user, set_profile_data, has_permission
from main.forms import UserProfileForm, FormLicenseForm, DataLicenseForm,\
     SupportDocForm, QuickConverterFile, QuickConverterURL, QuickConverter,\
     SourceForm, PermissionForm
from django.core.files.storage import default_storage
from django.utils import simplejson
from django.shortcuts import render_to_response, get_object_or_404
from odk_viewer.views import image_urls_for_form
from guardian.shortcuts import assign, remove_perm, get_users_with_perms

def home(request):
    context = RequestContext(request)
    context.num_forms = Instance.objects.count()
    context.num_users = User.objects.count()
    context.num_shared_forms = XForm.objects.filter(shared__exact=1).count()
    if request.user.username:
        return HttpResponseRedirect(reverse(profile,
            kwargs={'username': request.user.username}))
    else:
        return render_to_response('home.html', context_instance=context)


@login_required
def login_redirect(request):
    return HttpResponseRedirect(reverse(profile,
        kwargs={'username': request.user.username}))


@require_POST
@login_required
def clone_xlsform(request, username):
    """
    Copy a public/Shared form to a users list of forms.
    Eliminates the need to download Excel File and upload again.
    """
    to_username = request.user.username
    context = RequestContext(request)
    context.message = {'type': None, 'text': '....'}

    try:
        form_owner = request.POST.get('username')
        id_string = request.POST.get('id_string')
        if len(id_string) > 0 and id_string[0].isdigit():
            id_string = '_' + id_string
        xform = XForm.objects.get(user__username=form_owner, \
                                    id_string=id_string)
        path = xform.xls.name
        if default_storage.exists(path):
            xls_file = upload_to(None, id_string + '_cloned.xls', to_username)
            xls_data = default_storage.open(path)
            xls_file = default_storage.save(xls_file, xls_data)
            context.message = u"%s-%s" % (form_owner, xls_file)
            survey = DataDictionary.objects.create(
                user=request.user,
                xls=xls_file
                ).survey
            context.message = {
                'type': 'success',
                'text': 'Successfully cloned %s into your '\
                        '<a href="%s">profile</a>.' % \
                        (survey.id_string, reverse(profile,
                            kwargs={'username': to_username}))
                }
    except (PyXFormError, XLSFormError) as e:
        context.message = {
            'type': 'error',
            'text': unicode(e),
            }
    except IntegrityError as e:
        context.message = {
            'type': 'error',
            'text': 'Form with this id already exists.',
            }
    if request.is_ajax():
        return HttpResponse(simplejson.dumps(context.message), \
                        mimetype='application/json')
    else:
        return HttpResponseRedirect(reverse(show, kwargs={
                    'username': to_username,
                    'id_string': survey.id_string
                }))


def profile(request, username):
    context = RequestContext(request)
    content_user = None
    context.num_surveys = Instance.objects.count()
    context.form = QuickConverter()<commit_msg>change id_string after db query<commit_after>import os, urllib2

from django.core.urlresolvers import reverse
from django.contrib.auth.decorators import login_required
from django.shortcuts import render_to_response
from django.template import RequestContext
from django import forms
from django.db import IntegrityError
from django.contrib.auth.models import User
from django.views.decorators.http import require_GET, require_POST
from django.http import HttpResponse, HttpResponseBadRequest, \
    HttpResponseRedirect, HttpResponseNotAllowed, HttpResponseForbidden
from pyxform.errors import PyXFormError
from odk_viewer.models import DataDictionary
from odk_viewer.models.data_dictionary import upload_to
from main.models import UserProfile, MetaData
from odk_logger.models import Instance, XForm
from utils.logger_tools import response_with_mimetype_and_name
from utils.decorators import is_owner
from odk_logger.models.xform import XLSFormError
from utils.user_auth import check_and_set_user, set_profile_data, has_permission
from main.forms import UserProfileForm, FormLicenseForm, DataLicenseForm,\
     SupportDocForm, QuickConverterFile, QuickConverterURL, QuickConverter,\
     SourceForm, PermissionForm
from django.core.files.storage import default_storage
from django.utils import simplejson
from django.shortcuts import render_to_response, get_object_or_404
from odk_viewer.views import image_urls_for_form
from guardian.shortcuts import assign, remove_perm, get_users_with_perms

def home(request):
    context = RequestContext(request)
    context.num_forms = Instance.objects.count()
    context.num_users = User.objects.count()
    context.num_shared_forms = XForm.objects.filter(shared__exact=1).count()
    if request.user.username:
        return HttpResponseRedirect(reverse(profile,
            kwargs={'username': request.user.username}))
    else:
        return render_to_response('home.html', context_instance=context)


@login_required
def login_redirect(request):
    return HttpResponseRedirect(reverse(profile,
        kwargs={'username': request.user.username}))


@require_POST
@login_required
def clone_xlsform(request, username):
    """
    Copy a public/Shared form to a users list of forms.
    Eliminates the need to download Excel File and upload again.
    """
    to_username = request.user.username
    context = RequestContext(request)
    context.message = {'type': None, 'text': '....'}

    try:
        form_owner = request.POST.get('username')
        id_string = request.POST.get('id_string')
        xform = XForm.objects.get(user__username=form_owner, \
                                    id_string=id_string)
        if len(id_string) > 0 and id_string[0].isdigit():
            id_string = '_' + id_string
        path = xform.xls.name
        if default_storage.exists(path):
            xls_file = upload_to(None, id_string + '_cloned.xls', to_username)
            xls_data = default_storage.open(path)
            xls_file = default_storage.save(xls_file, xls_data)
            context.message = u"%s-%s" % (form_owner, xls_file)
            survey = DataDictionary.objects.create(
                user=request.user,
                xls=xls_file
                ).survey
            context.message = {
                'type': 'success',
                'text': 'Successfully cloned %s into your '\
                        '<a href="%s">profile</a>.' % \
                        (survey.id_string, reverse(profile,
                            kwargs={'username': to_username}))
                }
    except (PyXFormError, XLSFormError) as e:
        context.message = {
            'type': 'error',
            'text': unicode(e),
            }
    except IntegrityError as e:
        context.message = {
            'type': 'error',
            'text': 'Form with this id already exists.',
            }
    if request.is_ajax():
        return HttpResponse(simplejson.dumps(context.message), \
                        mimetype='application/json')
    else:
        return HttpResponse(context.message['text'])


def profile(request, username):
    context = RequestContext(request)
    content_user = None
    context.num_surveys = Instance.objects.count()
    context.form = QuickConverter()
{'fraction_non_alphanumeric': 0.08814307281384276, 'fraction_numerical': 0.0016258274300313552, 'mean_word_length': 3.0111783884489984, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6666291', 'n_tokens_mistral': 2392, 'n_tokens_neox': 2364, 'n_words': 558}