<commit_before># -*- encoding: utf8 -*-

import copy, traceback, time, subprocess
from pprint import pprint
from itertools import chain
from md5 import md5
from sha import sha
from random import random
from django.http import *
from django.utils.html import escape
from django.utils.safestring import mark_safe
from django.core.urlresolvers import reverse
from django import forms
from django.forms.util import smart_unicode
from django.shortcuts import render_to_response
from django.template import RequestContext
from ngw import settings
from ngw.gp.alchemy_models import *
from ngw.gp.basicauth import *

GROUP_PREFIX = '_group_'
DEFAULT_INITIAL_FIELDS=['name', 'email', 'tel_mobile', 'tel_prive', 'tel_professionel', 'region']
NB_LINES_PER_PAGE=100


def ngw_auth(username, passwd):
    username = unicode(username, 'utf-8', 'replace')
    passwd = unicode(passwd, 'utf-8', 'replace')
    if not username or not passwd:
        return None
    c = Query(Contact).filter(Contact.c.login==username).first()
    if c==None:
        return None
    dbpasswd=c.passwd
    algo, salt, digest = dbpasswd.split('$')
    if algo=="crypt":
        targetdigest=subprocess.Popen(["openssl", "passwd", "-crypt", "-salt", salt, passwd], stdout=subprocess.PIPE).communicate()[0]
        targetdigest=targetdigest[:-1] # remove extra "\n"
        if salt+digest==targetdigest:
            return c
    elif algo=="md5":
        if md5(salt+passwd).hexdigest()==digest:
            return c
    elif algo=="sha1":
        if sha(salt+passwd).hexdigest()==digest:
            return c
    else:
        print "Unsupported password algorithm", algo.encode('utf-8')
        return None
    return None # authentification failed


def get_default_display_fields():
    # check the field still exists
    result = []
    for fname in DEFAULT_INITIAL_FIELDS:
        if fname=='name':
            pass
        elif fname.startswith(GROUP_PREFIX):
            try:
                groupid = int(fname[len(GROUP_PREFIX):])
            except ValueError:
                print "Error in default fields: %s has invalid syntax." % fname
                continue
            if not Query(ContactGroup).get(groupid):
                print "Error in default fields: There is no group #%d." % groupid
                continue
        else:
            if not Query(ContactField).filter(ContactField.c.name==fname).first():
                print "Error in default fields: %s doesn't exists." % fname
                continue
        result.append(fname)
    return result


def name_internal2nice(txt):
    """
    Capitalize first letter and replace _ by spaces
    """
    txt = txt.replace('_', ' ')
    if len(txt)>0:
        txt = txt[0].upper() + txt[1:]
    return txt

def MultiValueDict_unicode(d):
    "Create a copy of the dict, converting values to unicode when necessary"
    def u(x):
        if isinstance(x, str):
            return unicode(x, "utf-8")
        else:
            return x
    result = MultiValueDict()
    for k,v in d.iteritems():
        #print "kv=",k,v
        result.setlist(k, [ u(vi) for vi in v ])
    return result

@http_authenticate(ngw_auth, 'ngw')
def index(request):
    return render_to_response('index.html', {
        'title':'Action DB',
        'ncontacts': Query(Contact).count(),
    }, RequestContext(request))

@http_authenticate(ngw_auth, 'ngw')
def generic_delete(request, o, next_url):
    objtypename = o.__class__.__name__.lower()
    title = "Please confirm deletetion"

    if not o:
        raise Http404()

    confirm = request.GET.get("confirm", "")
    if confirm:
        Session.delete(o)
        Session.commit()
        return HttpResponseRedirect(next_url)
    else:
        return render_to_response('delete.html', {'title':title, 'id':id, 'objtypename':objtypename, 'o': o}, RequestContext(request))
        

# That class is a clone of forms.CheckboxSelectMultiple
# It only adds an extra class=multiplechoice to the <ul>
class NgwCheckboxSelectMultiple(forms.SelectMultiple):
    def render(self, name, value, attrs=None, choices=()):
        if value is None: value = []
        has_id = attrs and attrs.has_key('id')
        final_attrs = self.build_attrs(attrs, name=name)
        output = [u'<ul class=multiplechoice>']
        str_values = set([smart_unicode(v) for v in value]) # Normalize to strings.
        for i, (option_value, option_label) in enumerate(chain(self.choices, choices)):
            # If an ID attribute was given, add a numeric index as a suffix,
            # so that the checkboxes don't all have the same ID attribute.
            if has_id:
                final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i))
            cb = forms.CheckboxInput(final_attrs, check_test=lambda value: value in str_values)
            option_value = smart_unicode(option_value)
            rendered_cb = cb.render(name, option_value)
            output.append(u'<li><label>%s %s</label></li>' % (rendered_cb, escape(smart_unicode(option_label))))
        output.append(u'</ul>')
        return u'\n'.join(output)

    def id_for_label(self, id_):
        # See the comment for RadioSelect.id_for_label()
        if id_:
            id_ += '_0'
        return id_
    id_for_label = classmethod(id_for_label)


class FilterMultipleSelectWidget(forms.SelectMultiple):
    class Media:
        js = ('ngw.js',)

    def __init__(self, verbose_name, is_stacked, attrs=None, choices=()):
        self.verbose_name = verbose_name
        self.is_stacked = is_stacked
        super(FilterMultipleSelectWidget, self).__init__(attrs, choices)

    def render(self, name, value, attrs=None, choices=()):
        output = [super(FilterMultipleSelectWidget, self).render(name, value, attrs, choices)]
        output.append(u'<script type="text/javascript">addEvent(window, "load", function(e) {')
        # TODO: "id_" is hard-coded here. This should instead use the correct
        # API to determine the ID dynamically.
        output.append(u'SelectFilter.init("id_%s", "%s", %s, "%s"); });</script>\n' % \
            (name, self.verbose_name, int(self.is_stacked), settings.ADMIN_MEDIA_PREFIX))
        return mark_safe(u''.join(output))



def query_print_entities(request, template_name, args):
    q = args['query']
    cols = args['cols']

    # get sort column name
    order = request.REQUEST.get("_order", 0)
    try:
        intorder=int(order)
    except ValueError:
        intorder = 0

    sort_col = cols[abs(intorder)][3]
    if not order or order[0]!="-":
        q = q.order_by(sort_col)
    else:
        q = q.order_by(sort_col.desc())<commit_msg>Fixed media location for filtered multi select box<commit_after># -*- encoding: utf8 -*-

import copy, traceback, time, subprocess
from pprint import pprint
from itertools import chain
from md5 import md5
from sha import sha
from random import random
from django.http import *
from django.utils.html import escape
from django.utils.safestring import mark_safe
from django.core.urlresolvers import reverse
from django import forms
from django.forms.util import smart_unicode
from django.shortcuts import render_to_response
from django.template import RequestContext
from ngw import settings
from ngw.gp.alchemy_models import *
from ngw.gp.basicauth import *

GROUP_PREFIX = '_group_'
DEFAULT_INITIAL_FIELDS=['name', 'email', 'tel_mobile', 'tel_prive', 'tel_professionel', 'region']
NB_LINES_PER_PAGE=100


def ngw_auth(username, passwd):
    username = unicode(username, 'utf-8', 'replace')
    passwd = unicode(passwd, 'utf-8', 'replace')
    if not username or not passwd:
        return None
    c = Query(Contact).filter(Contact.c.login==username).first()
    if c==None:
        return None
    dbpasswd=c.passwd
    algo, salt, digest = dbpasswd.split('$')
    if algo=="crypt":
        targetdigest=subprocess.Popen(["openssl", "passwd", "-crypt", "-salt", salt, passwd], stdout=subprocess.PIPE).communicate()[0]
        targetdigest=targetdigest[:-1] # remove extra "\n"
        if salt+digest==targetdigest:
            return c
    elif algo=="md5":
        if md5(salt+passwd).hexdigest()==digest:
            return c
    elif algo=="sha1":
        if sha(salt+passwd).hexdigest()==digest:
            return c
    else:
        print "Unsupported password algorithm", algo.encode('utf-8')
        return None
    return None # authentification failed


def get_default_display_fields():
    # check the field still exists
    result = []
    for fname in DEFAULT_INITIAL_FIELDS:
        if fname=='name':
            pass
        elif fname.startswith(GROUP_PREFIX):
            try:
                groupid = int(fname[len(GROUP_PREFIX):])
            except ValueError:
                print "Error in default fields: %s has invalid syntax." % fname
                continue
            if not Query(ContactGroup).get(groupid):
                print "Error in default fields: There is no group #%d." % groupid
                continue
        else:
            if not Query(ContactField).filter(ContactField.c.name==fname).first():
                print "Error in default fields: %s doesn't exists." % fname
                continue
        result.append(fname)
    return result


def name_internal2nice(txt):
    """
    Capitalize first letter and replace _ by spaces
    """
    txt = txt.replace('_', ' ')
    if len(txt)>0:
        txt = txt[0].upper() + txt[1:]
    return txt

def MultiValueDict_unicode(d):
    "Create a copy of the dict, converting values to unicode when necessary"
    def u(x):
        if isinstance(x, str):
            return unicode(x, "utf-8")
        else:
            return x
    result = MultiValueDict()
    for k,v in d.iteritems():
        #print "kv=",k,v
        result.setlist(k, [ u(vi) for vi in v ])
    return result

@http_authenticate(ngw_auth, 'ngw')
def index(request):
    return render_to_response('index.html', {
        'title':'Action DB',
        'ncontacts': Query(Contact).count(),
    }, RequestContext(request))

@http_authenticate(ngw_auth, 'ngw')
def generic_delete(request, o, next_url):
    objtypename = o.__class__.__name__.lower()
    title = "Please confirm deletetion"

    if not o:
        raise Http404()

    confirm = request.GET.get("confirm", "")
    if confirm:
        Session.delete(o)
        Session.commit()
        return HttpResponseRedirect(next_url)
    else:
        return render_to_response('delete.html', {'title':title, 'id':id, 'objtypename':objtypename, 'o': o}, RequestContext(request))
        

# That class is a clone of forms.CheckboxSelectMultiple
# It only adds an extra class=multiplechoice to the <ul>
class NgwCheckboxSelectMultiple(forms.SelectMultiple):
    def render(self, name, value, attrs=None, choices=()):
        if value is None: value = []
        has_id = attrs and attrs.has_key('id')
        final_attrs = self.build_attrs(attrs, name=name)
        output = [u'<ul class=multiplechoice>']
        str_values = set([smart_unicode(v) for v in value]) # Normalize to strings.
        for i, (option_value, option_label) in enumerate(chain(self.choices, choices)):
            # If an ID attribute was given, add a numeric index as a suffix,
            # so that the checkboxes don't all have the same ID attribute.
            if has_id:
                final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i))
            cb = forms.CheckboxInput(final_attrs, check_test=lambda value: value in str_values)
            option_value = smart_unicode(option_value)
            rendered_cb = cb.render(name, option_value)
            output.append(u'<li><label>%s %s</label></li>' % (rendered_cb, escape(smart_unicode(option_label))))
        output.append(u'</ul>')
        return u'\n'.join(output)

    def id_for_label(self, id_):
        # See the comment for RadioSelect.id_for_label()
        if id_:
            id_ += '_0'
        return id_
    id_for_label = classmethod(id_for_label)


class FilterMultipleSelectWidget(forms.SelectMultiple):
    class Media:
        js = ('ngw.js',)

    def __init__(self, verbose_name, is_stacked, attrs=None, choices=()):
        self.verbose_name = verbose_name
        self.is_stacked = is_stacked
        super(FilterMultipleSelectWidget, self).__init__(attrs, choices)

    def render(self, name, value, attrs=None, choices=()):
        output = [super(FilterMultipleSelectWidget, self).render(name, value, attrs, choices)]
        output.append(u'<script type="text/javascript">addEvent(window, "load", function(e) {')
        # TODO: "id_" is hard-coded here. This should instead use the correct
        # API to determine the ID dynamically.
        output.append(u'SelectFilter.init("id_%s", "%s", %s, "%s"); });</script>\n' % \
            (name, self.verbose_name, int(self.is_stacked), settings.MEDIA_URL+settings.ADMIN_MEDIA_PREFIX))
        return mark_safe(u''.join(output))



def query_print_entities(request, template_name, args):
    q = args['query']
    cols = args['cols']

    # get sort column name
    order = request.REQUEST.get("_order", 0)
    try:
        intorder=int(order)
    except ValueError:
        intorder = 0

    sort_col = cols[abs(intorder)][3]
    if not order or order[0]!="-":
        q = q.order_by(sort_col)
    else:
        q = q.order_by(sort_col.desc())
{'fraction_non_alphanumeric': 0.11880444577951337, 'fraction_numerical': 0.004055271853409433, 'mean_word_length': 2.9018458833870495, 'pattern_counts': {'":': 8, '<': 21, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2697569', 'n_tokens_mistral': 4129, 'n_tokens_neox': 4008, 'n_words': 1076}