<commit_before>
from collections import defaultdict
import json
import logging
import re
import urlparse
import requests
from os.path import splitext

from django.core.urlresolvers import reverse
from django import http
from django.template.defaultfilters import date
from django.views.generic.base import View
from django.views.decorators.csrf import csrf_exempt
from iso8601 import parse_date
from tastypie.resources import ModelResource
from tastypie.resources import ALL
from tastypie.resources import ALL_WITH_RELATIONS
from tastypie import fields

from july.people.models import Commit, Project, Location, Team, Language
from july.models import User

EMAIL_MATCH = re.compile('<(.+?)>')


class UserResource(ModelResource):

    class Meta:
        queryset = User.objects.all()
        excludes = ['password', 'email', 'is_superuser', 'is_staff',
                    'is_active']


class ProjectResource(ModelResource):

    class Meta:
        queryset = Project.objects.all()
        allowed_methods = ['get']
        filtering = {
            'user': ALL_WITH_RELATIONS,
            'locations': ALL,
            'teams': ALL
        }


class LocationResource(ModelResource):

    class Meta:
        queryset = Location.objects.all()
        allowed_methods = ['get']
        filtering = {
            'name': ['istartswith', 'exact', 'icontains'],
        }


class TeamResource(ModelResource):

    class Meta:
        queryset = Team.objects.all()
        allowed_methods = ['get']
        filtering = {
            'name': ['istartswith', 'exact', 'icontains'],
        }


class CommitResource(ModelResource):
    user = fields.ForeignKey(UserResource, 'user', blank=True, null=True)
    project = fields.ForeignKey(ProjectResource, 'project',
                                blank=True, null=True)

    class Meta:
        queryset = Commit.objects.all().select_related('user', 'project')
        allowed_methods = ['get']
        filtering = {
            'user': ALL_WITH_RELATIONS,
            'project': ALL_WITH_RELATIONS,
            'timestamp': ['exact', 'range', 'gt', 'lt'],
        }

    def gravatar(self, email):
        """Return a link to gravatar image."""
        url = 'http://www.gravatar.com/avatar/%s?s=48'
        from hashlib import md5
        email = email.strip().lower()
        hashed = md5(email).hexdigest()
        return url % hashed

    def dehydrate(self, bundle):
        email = bundle.data.pop('email')
        gravatar = self.gravatar(email)
        bundle.data['project_name'] = bundle.obj.project.name
        bundle.data['project_url'] = reverse('project-details',
                                             args=[bundle.obj.project.slug])
        bundle.data['username'] = getattr(bundle.obj.user, 'username', None)
        # Format the date properly using django template filter
        bundle.data['timestamp'] = date(bundle.obj.timestamp, 'c')
        bundle.data['picture_url'] = getattr(bundle.obj.user,
                                             'picture_url',
                                             gravatar)
        return bundle


class JSONMixin(object):

    def respond_json(self, data, **kwargs):
        content = json.dumps(data)
        resp = http.HttpResponse(content,
                                 content_type='application/json',<commit_msg>Dehydration of the files jsonfield.<commit_after>
from collections import defaultdict
import json
import logging
import re
import urlparse
import requests
from os.path import splitext

from django.core.urlresolvers import reverse
from django import http
from django.template.defaultfilters import date
from django.views.generic.base import View
from django.views.decorators.csrf import csrf_exempt
from iso8601 import parse_date
from tastypie.resources import ModelResource
from tastypie.resources import ALL
from tastypie.resources import ALL_WITH_RELATIONS
from tastypie import fields

from july.people.models import Commit, Project, Location, Team, Language
from july.models import User

EMAIL_MATCH = re.compile('<(.+?)>')


class UserResource(ModelResource):

    class Meta:
        queryset = User.objects.all()
        excludes = ['password', 'email', 'is_superuser', 'is_staff',
                    'is_active']


class ProjectResource(ModelResource):

    class Meta:
        queryset = Project.objects.all()
        allowed_methods = ['get']
        filtering = {
            'user': ALL_WITH_RELATIONS,
            'locations': ALL,
            'teams': ALL
        }


class LocationResource(ModelResource):

    class Meta:
        queryset = Location.objects.all()
        allowed_methods = ['get']
        filtering = {
            'name': ['istartswith', 'exact', 'icontains'],
        }


class TeamResource(ModelResource):

    class Meta:
        queryset = Team.objects.all()
        allowed_methods = ['get']
        filtering = {
            'name': ['istartswith', 'exact', 'icontains'],
        }


class CommitResource(ModelResource):
    user = fields.ForeignKey(UserResource, 'user', blank=True, null=True)
    project = fields.ForeignKey(ProjectResource, 'project',
                                blank=True, null=True)

    class Meta:
        queryset = Commit.objects.all().select_related('user', 'project')
        allowed_methods = ['get']
        filtering = {
            'user': ALL_WITH_RELATIONS,
            'project': ALL_WITH_RELATIONS,
            'timestamp': ['exact', 'range', 'gt', 'lt'],
        }

    def gravatar(self, email):
        """Return a link to gravatar image."""
        url = 'http://www.gravatar.com/avatar/%s?s=48'
        from hashlib import md5
        email = email.strip().lower()
        hashed = md5(email).hexdigest()
        return url % hashed

    def dehydrate(self, bundle):
        email = bundle.data.pop('email')
        gravatar = self.gravatar(email)
        bundle.data['project_name'] = bundle.obj.project.name
        bundle.data['project_url'] = reverse('project-details',
                                             args=[bundle.obj.project.slug])
        bundle.data['username'] = getattr(bundle.obj.user, 'username', None)
        # Format the date properly using django template filter
        bundle.data['timestamp'] = date(bundle.obj.timestamp, 'c')
        bundle.data['picture_url'] = getattr(bundle.obj.user,
                                             'picture_url',
                                             gravatar)
        bundle.data['files'] = bundle.obj.files
        return bundle


class JSONMixin(object):

    def respond_json(self, data, **kwargs):
        content = json.dumps(data)
        resp = http.HttpResponse(content,
                                 content_type='application/json',
{'fraction_non_alphanumeric': 0.11109463067339069, 'fraction_numerical': 0.002373183031741323, 'mean_word_length': 2.391851106639839, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3974086', 'n_tokens_mistral': 1853, 'n_tokens_neox': 1792, 'n_words': 447}