<commit_before># -*- coding: utf-8 -*-
from django.contrib.auth.models import User, Group
from rest_framework import serializers
from api import models
from dateutil.relativedelta import relativedelta
from django.core.urlresolvers import reverse as django_reverse

import datetime

class UserSerializer(serializers.ModelSerializer):
    email = serializers.SerializerMethodField()
    accounts = serializers.SerializerMethodField()

    def get_email(self, obj):
        if self.context['request'].method == 'POST' or self.context['request'].user == obj:
            return obj.email
        return None

    def get_accounts(self, obj):
        accounts = models.Account.objects.filter(owner=obj)
        serializer = AccountSerializer(accounts, many=True, context=self.context)
        return serializer.data

    class Meta:
        model = User
        fields = ('username', 'email', 'accounts')
        extra_kwargs = {'password': {'<PASSWORD>': True}}

    def create(self, data):
        if 'password' not in self.context['request'].data:
            raise serializers.ValidationError(detail={'password': ['<PASSWORD>.']})
        user = super(UserSerializer, self).create(data)
        user.set_password(self.context['request'].data['password'])
        user.save()
        return user

class GroupSerializer(serializers.ModelSerializer):
    class Meta:
        model = Group
        fields = ('url', 'name')

class EventSerializer(serializers.ModelSerializer):
    japan_current = serializers.SerializerMethodField()
    world_current = serializers.SerializerMethodField()
    cards = serializers.SerializerMethodField()

    def get_japan_current(self, obj):
        return obj.is_japan_current()

    def get_world_current(self, obj):
        return obj.is_world_current()

    def get_cards(self, obj):
        cards = models.Card.objects.filter(event=obj)
        cards = [card.pk for card in cards]
        return cards

    class Meta:
        model = models.Event
        fields=('japanese_name', 'english_name', 'beginning', 'end', 'japan_current', 'world_current', 'cards', 'japanese_t1_points', 'japanese_t1_rank', 'japanese_t2_points', 'japanese_t2_rank', 'note')

class CardSerializer(serializers.ModelSerializer):
    japan_only = serializers.SerializerMethodField()
    event = EventSerializer()
    card_image = serializers.SerializerMethodField()
    card_idolized_image = serializers.SerializerMethodField()
    round_card_image = serializers.SerializerMethodField()
    owned_cards = serializers.SerializerMethodField()
    japanese_attribute = serializers.SerializerMethodField()
    website_url = serializers.SerializerMethodField()

    def _image_file_to_url(self, path, card, circle=False, idolized=False):
        url = 'http://' + self.context['request'].META['HTTP_HOST']
        if (not path and 'imagedefault' in self.context['request'].GET and self.context['request'].GET['imagedefault'] and self.context['request'].GET['imagedefault'].title() != 'False' and
            (idolized or circle or (not card.is_special and not card.is_promo))):
            if circle:
                return url + '/static/circle-' + card.attribute + '.png'
            return url + '/static/default-' + card.attribute + '.png'
        return path.replace('web', 'http://' + self.context['request'].META['HTTP_HOST'])

    def get_japanese_attribute(self, obj):
        return obj.japanese_attribute()

    def get_japan_only(self, obj):
        return obj.is_japan_only()

    def get_card_image(self, obj):
        return self._image_file_to_url(str(obj.card_image), obj)
    def get_card_idolized_image(self, obj):<commit_msg>Use CDN paths in the API<commit_after># -*- coding: utf-8 -*-
from django.contrib.auth.models import User, Group
from django.conf import settings
from rest_framework import serializers
from api import models
from dateutil.relativedelta import relativedelta
from django.core.urlresolvers import reverse as django_reverse

import datetime

class UserSerializer(serializers.ModelSerializer):
    email = serializers.SerializerMethodField()
    accounts = serializers.SerializerMethodField()

    def get_email(self, obj):
        if self.context['request'].method == 'POST' or self.context['request'].user == obj:
            return obj.email
        return None

    def get_accounts(self, obj):
        accounts = models.Account.objects.filter(owner=obj)
        serializer = AccountSerializer(accounts, many=True, context=self.context)
        return serializer.data

    class Meta:
        model = User
        fields = ('username', 'email', 'accounts')
        extra_kwargs = {'password': {'write_only': True}}

    def create(self, data):
        if 'password' not in self.context['request'].data:
            raise serializers.ValidationError(detail={'password': ['This field is required.']})
        user = super(UserSerializer, self).create(data)
        user.set_password(self.context['request'].data['password'])
        user.save()
        return user

class GroupSerializer(serializers.ModelSerializer):
    class Meta:
        model = Group
        fields = ('url', 'name')

class EventSerializer(serializers.ModelSerializer):
    japan_current = serializers.SerializerMethodField()
    world_current = serializers.SerializerMethodField()
    cards = serializers.SerializerMethodField()

    def get_japan_current(self, obj):
        return obj.is_japan_current()

    def get_world_current(self, obj):
        return obj.is_world_current()

    def get_cards(self, obj):
        cards = models.Card.objects.filter(event=obj)
        cards = [card.pk for card in cards]
        return cards

    class Meta:
        model = models.Event
        fields=('japanese_name', 'english_name', 'beginning', 'end', 'japan_current', 'world_current', 'cards', 'japanese_t1_points', 'japanese_t1_rank', 'japanese_t2_points', 'japanese_t2_rank', 'note')

class CardSerializer(serializers.ModelSerializer):
    japan_only = serializers.SerializerMethodField()
    event = EventSerializer()
    card_image = serializers.SerializerMethodField()
    card_idolized_image = serializers.SerializerMethodField()
    round_card_image = serializers.SerializerMethodField()
    owned_cards = serializers.SerializerMethodField()
    japanese_attribute = serializers.SerializerMethodField()
    website_url = serializers.SerializerMethodField()

    def _image_file_to_url(self, path, card, circle=False, idolized=False):
        base_url = settings.IMAGES_HOSTING_PATH
        if (not path and 'imagedefault' in self.context['request'].GET and self.context['request'].GET['imagedefault'] and self.context['request'].GET['imagedefault'].title() != 'False' and
            (idolized or circle or (not card.is_special and not card.is_promo))):
            if circle:
                return base_url + '/static/circle-' + card.attribute + '.png'
            return base_url + '/static/default-' + card.attribute + '.png'
        return '%s%s' % (base_url, path)

    def get_japanese_attribute(self, obj):
        return obj.japanese_attribute()

    def get_japan_only(self, obj):
        return obj.is_japan_only()

    def get_card_image(self, obj):
        return self._image_file_to_url(str(obj.card_image), obj)
    def get_card_idolized_image(self, obj):
{'fraction_non_alphanumeric': 0.11466191194623508, 'fraction_numerical': 0.001371553970648745, 'mean_word_length': 3.6924066924066925, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '762325', 'n_tokens_mistral': 2098, 'n_tokens_neox': 2072, 'n_words': 466}