<commit_before>import json

from django.conf import settings
from django.core.urlresolvers import reverse, reverse_lazy
from django.contrib import messages
from django.contrib.auth import models as auth_models
from django.contrib.contenttypes.models import ContentType
from django.core.mail import send_mass_mail
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import get_object_or_404
from django.template.loader import render_to_string
from django.utils.translation import ugettext_lazy as _
from django.views import generic as generic_views

from userprofiles.contrib.emailverification.models import EmailVerification
from userprofiles.contrib.emailverification.views import EmailChangeView as BaseEmailChangeView
from userprofiles.contrib.profiles.views import ProfileChangeView as BaseProfileChangeView
from userprofiles.mixins import LoginRequiredMixin

from pyconde.conference.models import current_conference
from pyconde.reviews.models import Reviewer

from . import forms
from . import models
from .utils import get_addressed_as, get_display_name


class AutocompleteUser(generic_views.View):
    """
    This view is used for instance within the proposals application to support
    the autocompletion widget in there.

    The current implementation matches users with the dipslay namee being
    equal to the given "term" parameter and returns their speaker pk as JSON
    object.

    TODO: Evaluation if this might be better placed somewhere within the
          speakers application.
    """

    def get_matching_users(self, term):
        """
        Returns a list of dicts containing a user's name ("label") and her
        speaker pk ("value").
        """
        result = []
        for profile in models.Profile.objects.filter(
                display_name__icontains=term):
            user = profile.user
            result.append({
                'label': u'{0} ({1})'.format(profile.display_name,
                    user.username),
                'value': user.speaker_profile.pk
            })
        return result

    def get(self, request):
        term = request.GET['term']
        result = []
        if term and len(term) >= 2:
            result = self.get_matching_users(term)
        return HttpResponse(json.dumps(result), content_type='application/json')


class AutocompleteTags(generic_views.View):

    def get_matching_tags(self, term):
        data = list(models.Profile.tags.filter(name__icontains=term)
                                       .values_list('name', flat=True)
                                       .all())
        return data

    def get(self, request):
        term = request.GET.get('term', '')
        result = []
        if term and len(term) >= 2:
            result = self.get_matching_tags(term)
        return HttpResponse(json.dumps(result), content_type='application/json')


class ProfileView(generic_views.TemplateView):
    """
    Displays a profile page for the given user. If the user also has a
    speaker_profile, also render the information given there.
    """

    template_name = 'userprofiles/profile_view.html'

    def get_context_data(self, uid):
        user = get_object_or_404(auth_models.User, pk=uid)
        speaker_profile = user.speaker_profile<commit_msg>Refers #105 -- Limit tags to 7<commit_after>import json

from django.conf import settings
from django.core.urlresolvers import reverse, reverse_lazy
from django.contrib import messages
from django.contrib.auth import models as auth_models
from django.contrib.contenttypes.models import ContentType
from django.core.mail import send_mass_mail
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import get_object_or_404
from django.template.loader import render_to_string
from django.utils.translation import ugettext_lazy as _
from django.views import generic as generic_views

from userprofiles.contrib.emailverification.models import EmailVerification
from userprofiles.contrib.emailverification.views import EmailChangeView as BaseEmailChangeView
from userprofiles.contrib.profiles.views import ProfileChangeView as BaseProfileChangeView
from userprofiles.mixins import LoginRequiredMixin

from pyconde.conference.models import current_conference
from pyconde.reviews.models import Reviewer

from . import forms
from . import models
from .utils import get_addressed_as, get_display_name


class AutocompleteUser(generic_views.View):
    """
    This view is used for instance within the proposals application to support
    the autocompletion widget in there.

    The current implementation matches users with the dipslay namee being
    equal to the given "term" parameter and returns their speaker pk as JSON
    object.

    TODO: Evaluation if this might be better placed somewhere within the
          speakers application.
    """

    def get_matching_users(self, term):
        """
        Returns a list of dicts containing a user's name ("label") and her
        speaker pk ("value").
        """
        result = []
        for profile in models.Profile.objects.filter(
                display_name__icontains=term):
            user = profile.user
            result.append({
                'label': u'{0} ({1})'.format(profile.display_name,
                    user.username),
                'value': user.speaker_profile.pk
            })
        return result

    def get(self, request):
        term = request.GET['term']
        result = []
        if term and len(term) >= 2:
            result = self.get_matching_users(term)
        return HttpResponse(json.dumps(result), content_type='application/json')


class AutocompleteTags(generic_views.View):

    def get_matching_tags(self, term):
        data = list(models.Profile.tags.filter(name__icontains=term)
                                       .values_list('name', flat=True)
                                       .all()[:7])
        return data

    def get(self, request):
        term = request.GET.get('term', '')
        result = []
        if term and len(term) >= 2:
            result = self.get_matching_tags(term)
        return HttpResponse(json.dumps(result), content_type='application/json')


class ProfileView(generic_views.TemplateView):
    """
    Displays a profile page for the given user. If the user also has a
    speaker_profile, also render the information given there.
    """

    template_name = 'userprofiles/profile_view.html'

    def get_context_data(self, uid):
        user = get_object_or_404(auth_models.User, pk=uid)
        speaker_profile = user.speaker_profile
{'fraction_non_alphanumeric': 0.07418622255866768, 'fraction_numerical': 0.003785011355034065, 'mean_word_length': 3.02069385270846, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '6242332', 'n_tokens_mistral': 1768, 'n_tokens_neox': 1711, 'n_words': 524}