<commit_before>import base64, hmac, hashlib, simplejson, time

from django.conf import settings
from django.contrib import auth
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ValidationError
from django.db import IntegrityError, transaction
from django.template.defaultfilters import slugify

from rest_framework import serializers
from rest_framework import relations

from bulbs.images.fields import RemoteImageSerializer

from .models import Content, Tag


class ContentTypeField(serializers.WritableField):
    """Converts between natural key for native use and integer for non-native."""
    def to_native(self, value):
        """Convert to natural key."""
        content_type = ContentType.objects.get_for_id(value)
        return '_'.join(content_type.natural_key())

    def from_native(self, value):
        """Convert to integer id."""
        natural_key = value.split('_')
        content_type = ContentType.objects.get_by_natural_key(*natural_key)
        return content_type.id


class TagSerializer(serializers.ModelSerializer):
    class Meta:
        model = Tag

    def to_native(self, obj):
        return {
            'id': obj.pk,
            'name': obj.name,
            'slug': obj.slug,
            'type': obj.get_mapping_type_name()
        }


class TagField(relations.RelatedField):
    """This is a relational field that handles the addition of tags to content
    objects. This field also allows the user to create tags in the db if they
    don't already exist."""

    read_only = False

    def to_native(self, obj):
        return {
            'id': obj.pk,
            'name': obj.name,
            'slug': obj.slug,
            'type': obj.get_mapping_type_name()
        }

    def from_native(self, value):
        """Basically, each tag dict must include a full dict with id,
        name and slug--or else you need to pass in a dict with just a name,
        which indicated that the Tag doesn't exist, and should be added."""

        if 'id' in value:
            tag = Tag.objects.get(id=value['id'])
        else:
            if 'name' not in value:
                raise ValidationError("Tags must include an ID or a name.")
            name = value['name']
            slug = value.get('slug', slugify(name))
            try:
                tag = Tag.objects.get(slug=slug)
            except Tag.DoesNotExist:
                tag, created = Tag.objects.get_or_create(name=name, slug=slug)
        return tag


class UserSerializer(serializers.ModelSerializer):
    """"Returns basic User fields and an HMAC hash for Disqus authorization"""
    class Meta:
        model = auth.get_user_model()

    def to_native(self, obj):

        def get_remote_auth(user):
            data = simplejson.dumps({
                'id': user.pk,
                'username': user.username,
                'email': user.email,
            })
            message = base64.b64encode(data)
            timestamp = int(time.time())

            sig = hmac.HMAC(
                settings.DISQUS_SECRET, 
                '%s %s' % (message, timestamp), 
                hashlib.sha1
                ).hexdigest()

            return "%(message)s %(sig)s %(timestamp)s" % dict(
                message=message,
                timestamp=timestamp,
                sig=sig)

        return {
            'id': obj.pk,
            'username': obj.username,
            'email': obj.email,
            'first_name': obj.first_name,
            'last_name': obj.last_name,
            'remote_auth_s3': get_remote_auth(obj),
            'f_user': True if obj.facebook_uid else False
        }

 
class AuthorField(relations.RelatedField):
    """This field manages the authors on a piece of content, and allows a "fatter"
    endpoint then would normally be possible with a RelatedField"""

    read_only = False

    def to_native(self, obj):
        return {
            'id': obj.pk,
            'first_name': obj.first_name,
            'last_name': obj.last_name,
            'username': obj.username
        }

    def from_native(self, value):
        """Basically, each author dict must include either a username or id."""
        model = auth.get_user_model()

        if 'id' in value:
            author = model.objects.get(id=value['id'])
        else:
            if 'username' not in value:
                raise ValidationError("Authors must include an ID or a username.")
            username = value['username']<commit_msg>generalizing user serializer. closes #4<commit_after>import base64, hmac, hashlib, simplejson, time

from django.conf import settings
from django.contrib import auth
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ValidationError
from django.db import IntegrityError, transaction
from django.template.defaultfilters import slugify

from rest_framework import serializers
from rest_framework import relations

from bulbs.images.fields import RemoteImageSerializer

from .models import Content, Tag


class ContentTypeField(serializers.WritableField):
    """Converts between natural key for native use and integer for non-native."""
    def to_native(self, value):
        """Convert to natural key."""
        content_type = ContentType.objects.get_for_id(value)
        return '_'.join(content_type.natural_key())

    def from_native(self, value):
        """Convert to integer id."""
        natural_key = value.split('_')
        content_type = ContentType.objects.get_by_natural_key(*natural_key)
        return content_type.id


class TagSerializer(serializers.ModelSerializer):
    class Meta:
        model = Tag

    def to_native(self, obj):
        return {
            'id': obj.pk,
            'name': obj.name,
            'slug': obj.slug,
            'type': obj.get_mapping_type_name()
        }


class TagField(relations.RelatedField):
    """This is a relational field that handles the addition of tags to content
    objects. This field also allows the user to create tags in the db if they
    don't already exist."""

    read_only = False

    def to_native(self, obj):
        return {
            'id': obj.pk,
            'name': obj.name,
            'slug': obj.slug,
            'type': obj.get_mapping_type_name()
        }

    def from_native(self, value):
        """Basically, each tag dict must include a full dict with id,
        name and slug--or else you need to pass in a dict with just a name,
        which indicated that the Tag doesn't exist, and should be added."""

        if 'id' in value:
            tag = Tag.objects.get(id=value['id'])
        else:
            if 'name' not in value:
                raise ValidationError("Tags must include an ID or a name.")
            name = value['name']
            slug = value.get('slug', slugify(name))
            try:
                tag = Tag.objects.get(slug=slug)
            except Tag.DoesNotExist:
                tag, created = Tag.objects.get_or_create(name=name, slug=slug)
        return tag


class UserSerializer(serializers.ModelSerializer):
    """"Returns basic User fields"""
    class Meta:
        model = auth.get_user_model()

    def to_native(self, obj):
        
        return {
            'id': obj.pk,
            'username': obj.username,
            'email': obj.email,
            'first_name': obj.first_name,
            'last_name': obj.last_name
        }

 
class AuthorField(relations.RelatedField):
    """This field manages the authors on a piece of content, and allows a "fatter"
    endpoint then would normally be possible with a RelatedField"""

    read_only = False

    def to_native(self, obj):
        return {
            'id': obj.pk,
            'first_name': obj.first_name,
            'last_name': obj.last_name,
            'username': obj.username
        }

    def from_native(self, value):
        """Basically, each author dict must include either a username or id."""
        model = auth.get_user_model()

        if 'id' in value:
            author = model.objects.get(id=value['id'])
        else:
            if 'username' not in value:
                raise ValidationError("Authors must include an ID or a username.")
            username = value['username']
{'fraction_non_alphanumeric': 0.0962211759024508, 'fraction_numerical': 0.0013280212483399733, 'mean_word_length': 2.219588029537505, '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': '4304596', 'n_tokens_mistral': 2295, 'n_tokens_neox': 2209, 'n_words': 707}