<commit_before>from django.utils.translation import ugettext as _
from datetime import datetime
import hashlib
from django import forms
from django.db import transaction
from django.db.models import Q, Count
from django.core.urlresolvers import reverse
from django.core.exceptions import ValidationError
from djangorestframework.views import View
from djangorestframework.resources import FormResource, ModelResource
from djangorestframework.permissions import IsAuthenticated
from djangorestframework.response import Response

from devilry.apps.core.models import Deadline
from devilry.apps.core.models import AssignmentGroup
from devilry.utils.restformat import format_datetime
from devilry.utils.restformat import format_timedelta
from .group import GroupSerializer
from .errors import NotFoundError
from .errors import ValidationErrorResponse
from .errors import BadRequestFieldError
from .errors import PermissionDeniedError
from .auth import IsAssignmentAdmin
from .log import logger
from .fields import ListOfTypedField


ID_DATETIME_FORMAT = '%Y-%m-%dT%H_%M_%S'


def sha1hash(text):
    m = hashlib.sha1()
    m.update(text.encode('utf-8'))
    return m.hexdigest()

def encode_bulkdeadline_id(deadline):
    formatted_datetime = deadline.deadline.strftime(ID_DATETIME_FORMAT)
    if deadline.text:
        texthash = sha1hash(deadline.text)
    else:
        texthash = ''
    return '{0}--{1}'.format(formatted_datetime, texthash)

def decode_bulkdeadline_id(bulkdeadline_id):
    formatted_datetime, texthash = bulkdeadline_id.split('--')
    deadline = datetime.strptime(formatted_datetime, ID_DATETIME_FORMAT)
    return deadline, texthash


def deadlines_as_groupobjects(deadlines):
    return map(lambda deadline: deadline.assignment_group, deadlines)



def texthashmatch(texthash, text):
    if text == None or text == '':
        return bool(texthash) == False
    else:
        return texthash == sha1hash(text)

class GroupsListResource(ModelResource):
    model = Deadline
    fields = ('id', 'name', 'is_open', 'num_deliveries',
              'parentnode', 'feedback', 'candidates')

    def id(self, deadline):
        return int(deadline.assignment_group.id)

    def name(self, deadline):
        return deadline.assignment_group.id

    def is_open(self, deadline):
        return deadline.assignment_group.is_open

    def parentnode(self, deadline):
        return int(deadline.assignment_group.parentnode_id)

    def feedback(self, deadline):<commit_msg>devilry_subjectadmin.rest.deadlinesbulk: Return name correctly.<commit_after>from django.utils.translation import ugettext as _
from datetime import datetime
import hashlib
from django import forms
from django.db import transaction
from django.db.models import Q, Count
from django.core.urlresolvers import reverse
from django.core.exceptions import ValidationError
from djangorestframework.views import View
from djangorestframework.resources import FormResource, ModelResource
from djangorestframework.permissions import IsAuthenticated
from djangorestframework.response import Response

from devilry.apps.core.models import Deadline
from devilry.apps.core.models import AssignmentGroup
from devilry.utils.restformat import format_datetime
from devilry.utils.restformat import format_timedelta
from .group import GroupSerializer
from .errors import NotFoundError
from .errors import ValidationErrorResponse
from .errors import BadRequestFieldError
from .errors import PermissionDeniedError
from .auth import IsAssignmentAdmin
from .log import logger
from .fields import ListOfTypedField


ID_DATETIME_FORMAT = '%Y-%m-%dT%H_%M_%S'


def sha1hash(text):
    m = hashlib.sha1()
    m.update(text.encode('utf-8'))
    return m.hexdigest()

def encode_bulkdeadline_id(deadline):
    formatted_datetime = deadline.deadline.strftime(ID_DATETIME_FORMAT)
    if deadline.text:
        texthash = sha1hash(deadline.text)
    else:
        texthash = ''
    return '{0}--{1}'.format(formatted_datetime, texthash)

def decode_bulkdeadline_id(bulkdeadline_id):
    formatted_datetime, texthash = bulkdeadline_id.split('--')
    deadline = datetime.strptime(formatted_datetime, ID_DATETIME_FORMAT)
    return deadline, texthash


def deadlines_as_groupobjects(deadlines):
    return map(lambda deadline: deadline.assignment_group, deadlines)



def texthashmatch(texthash, text):
    if text == None or text == '':
        return bool(texthash) == False
    else:
        return texthash == sha1hash(text)

class GroupsListResource(ModelResource):
    model = Deadline
    fields = ('id', 'name', 'is_open', 'num_deliveries',
              'parentnode', 'feedback', 'candidates')

    def id(self, deadline):
        return int(deadline.assignment_group.id)

    def name(self, deadline):
        return deadline.assignment_group.name

    def is_open(self, deadline):
        return deadline.assignment_group.is_open

    def parentnode(self, deadline):
        return int(deadline.assignment_group.parentnode_id)

    def feedback(self, deadline):
{'fraction_non_alphanumeric': 0.08084428514536042, 'fraction_numerical': 0.0027877339705296694, 'mean_word_length': 4.714448236632537, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '139141', 'n_tokens_mistral': 1491, 'n_tokens_neox': 1456, 'n_words': 376}