<commit_before># -*- coding: utf-8 -*-

from ..constants import Status
from ..models import UserConnection


class UserConnectionViewMixin(object):
    """Gets a user connection by the user connection_id from the url.

    This view mixin places the following attributes on the view:

    * connection_id_pk_url_kwarg: the url primary key for the connection.
        Defaults to 'connection_id'
    * user_connection: the UserConnection object for the users connected.
    * connection_user: the user the authenticated user is connected with.
    """
    connection_id_pk_url_kwarg = 'connection_id'
    user_connection = None
    connection_user = None

    def dispatch(self, *args, **kwargs):
        self.user_connection = UserConnection.objects.get_by_id_or_404(
                                id=kwargs.get(self.connection_id_pk_url_kwarg),
                                prefetch_related=('with_user',))
        self.connection_user = self.user_connection.with_user
        return super(UserConnectionViewMixin, self).dispatch(*args, **kwargs)

    def get_context_data(self, **kwargs):
        context = super(UserConnectionViewMixin, self).get_context_data(
                                                                    **kwargs)
        context['user_connection'] = self.user_connection
        context['connection_user'] = self.connection_user
        return context


class BaseUserConnectionsViewMixin(object):
    """Base user connection mixin."""
    user_connections = None<commit_msg>updated the user connection view mixin to allow the connection url to accept either a connection id or username.<commit_after># -*- coding: utf-8 -*-

from ..constants import Status
from ..models import UserConnection
from django.contrib.auth import get_user_model
from django.http.response import Http404

User = get_user_model()


class UserConnectionViewMixin(object):
    """Gets a user connection by the user connection_id from the url.

    This view mixin places the following attributes on the view:

    * connection_id_pk_url_kwarg: the url primary key for the connection.
        Defaults to 'connection_id'
    * user_connection: the UserConnection object for the users connected.
    * connection_user: the user the authenticated user is connected with.
    """
    connection_id_pk_url_kwarg = 'connection_id'
    user_connection = None
    connection_user = None

    def dispatch(self, *args, **kwargs):
        # TODO: if I want to be able to hit a connection by their username,
        #       i can do a check on the connection_id to see if it's digits
        #       (which would represent a connection_id) or at least 1 non-digit
        #       (which would represent a username)
        connection_key = kwargs.get(self.connection_id_pk_url_kwarg)

        if connection_key.isdigit():
            # It's the connection primary key object.
            self.user_connection = UserConnection.objects.get_by_id_or_404(
                                id=kwargs.get(self.connection_id_pk_url_kwarg),
                                prefetch_related=('with_user',))

        else:
            # The connection key is a username (string)
            try:
                connection_user = User.objects.get(username=connection_key)
            except:
                raise Http404

            self.user_connection = UserConnection.objects.get_for_users(
                            user_1=self.request.user,
                            user_2=connection_user)

            if not self.user_connection:
                raise Http404

        self.connection_user = self.user_connection.get_connected_user(
                                                        user=self.request.user)
        return super(UserConnectionViewMixin, self).dispatch(*args, **kwargs)

    def get_context_data(self, **kwargs):
        context = super(UserConnectionViewMixin, self).get_context_data(
                                                                    **kwargs)
        context['user_connection'] = self.user_connection
        context['connection_user'] = self.connection_user
        return context


class BaseUserConnectionsViewMixin(object):
    """Base user connection mixin."""
    user_connections = None
{'fraction_non_alphanumeric': 0.0717827626918536, 'fraction_numerical': 0.004722550177095631, 'mean_word_length': 2.386091127098321, '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': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3939308', 'n_tokens_mistral': 1090, 'n_tokens_neox': 1047, 'n_words': 309}