<commit_before>from gis.models import Dataset, MapPoint, Tag, TagIndiv, MapPolygon
from rest_framework import serializers
from rest_framework_gis import serializers as gis_serializers

class TagSerializer(serializers.ModelSerializer):
    def __init__(self, *args, **kwargs):
        many = kwargs.pop('many', True)
        super(TagSerializer, self).__init__(many=many, *args, **kwargs)

    class Meta:
        model = TagIndiv
        fields = ('mappoint','tag')

class NewTagSerializer(serializers.ModelSerializer):
    mappoint = serializers.IntegerField()
    tag = serializers.CharField()

    class Meta:
        model = TagIndiv
        fields = ('mappoint','tag')

    def restore_object(self, attrs, instance=None):
        #only react to a post
        if not instance:
            #find the mappoint
            try:
                mp = MapPoint.objects.get(id=attrs['mappoint']) 
            except:
                return None
            new_tag = Tag(dataset = mp.dataset, tag = attrs['tag'])
            new_tag.save()
            instance = TagIndiv(mappoint = mp, tag = new_tag)  
        return instance

class TagCountSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Tag 
        fields = ['dataset','tag','count']

class DatasetSerializer(serializers.ModelSerializer):
    tags = serializers.SerializerMethodField('get_tags')
    def get_tags(self, dataset):
        #build nested distinct list
        return Tag.objects.filter(approved=True, dataset=dataset).order_by('-count').values('id','tag')<commit_msg>allow api-newtag to accept existing tags by text too<commit_after>from gis.models import Dataset, MapPoint, Tag, TagIndiv, MapPolygon
from rest_framework import serializers
from rest_framework_gis import serializers as gis_serializers

class TagSerializer(serializers.ModelSerializer):
    def __init__(self, *args, **kwargs):
        many = kwargs.pop('many', True)
        super(TagSerializer, self).__init__(many=many, *args, **kwargs)

    class Meta:
        model = TagIndiv
        fields = ('mappoint','tag')

class NewTagSerializer(serializers.ModelSerializer):
    mappoint = serializers.IntegerField()
    tag = serializers.CharField()

    class Meta:
        model = TagIndiv
        fields = ('mappoint','tag')

    def restore_object(self, attrs, instance=None):
        #only react to a post
        if not instance:
            #find the mappoint
            try:
                mp = MapPoint.objects.get(id=attrs['mappoint']) 
            except:
                return None
            attrs['tag'] = attrs['tag'].strip()
            tags = Tag.objects.filter(dataset = mp.dataset, tag = attrs['tag'])
            len_tags = len(tags)
            if len_tags == 0:
                tag = Tag(dataset = mp.dataset, tag = attrs['tag'])
                tag.save()
            elif len_tags == 1:
                tag = tags[0]
            else:
                approved_tags = tags.filter(approved = True)
                if len(approved_tags) > 0:
                    tag = approved_tags[0]
                else:
                    tag = tags[0]
            instance = TagIndiv(mappoint = mp, tag = tag)  
        return instance

class TagCountSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Tag 
        fields = ['dataset','tag','count']

class DatasetSerializer(serializers.ModelSerializer):
    tags = serializers.SerializerMethodField('get_tags')
    def get_tags(self, dataset):
        #build nested distinct list
        return Tag.objects.filter(approved=True, dataset=dataset).order_by('-count').values('id','tag')
{'fraction_non_alphanumeric': 0.10556621880998081, 'fraction_numerical': 0.0016451878256100905, 'mean_word_length': 2.5383123181377303, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '4848550', 'n_tokens_mistral': 1004, 'n_tokens_neox': 990, 'n_words': 246}