<commit_before>'''
API for the agendas app
'''
from django.contrib.auth.models import User
from tastypie.constants import ALL
import tastypie.fields as fields
from tastypie.cache import SimpleCache
from avatar.templatetags.avatar_tags import avatar_url

from knesset.api.resources.base import BaseResource
from knesset.mks.models import Member, Party
from knesset.agendas.models import Agenda, AgendaVote

class UserResource(BaseResource):
    class Meta(BaseResource.Meta):
        queryset = User.objects.all()
        include_absolute_url = True
        include_resource_uri = False
        allowed_methods = ['get']
        fields = ['username']

    avatar = fields.CharField()

    def dehydrate_avatar(self, bundle):
        return avatar_url(bundle.obj, 48)

class AgendaVoteResource(BaseResource):
    class Meta(BaseResource.Meta):
        queryset = AgendaVote.objects.select_related()
        allowed_methods = ['get']

    title = fields.CharField()

    def dehydrate_title(self, bundle):
        return bundle.obj.vote.title

class AgendaTodoResource(BaseResource):
    class Meta(BaseResource.Meta):
        allowed_methods = ['get']
        queryset = Agenda.objects.all()
        resource_name = 'agenda-todo'
        fields = ['votes_by_conrtoversy', 'votes_by_agendas']

    votes_by_controversy = fields.ListField()
    votes_by_agendas = fields.ListField()

    # TODO: Make this a parameter or setting or something
    NUM_SUGGESTIONS = 10

    def dehydrate_votes_by_agendas(self, bundle):
        votes = bundle.obj.get_suggested_votes_by_agendas(AgendaTodoResource.NUM_SUGGESTIONS)
        return self._dehydrate_votes(votes)

    def dehydrate_votes_by_controversy(self, bundle):
        votes = bundle.obj.get_suggested_votes_by_controversy(AgendaTodoResource.NUM_SUGGESTIONS)
        return self._dehydrate_votes(votes)

    def _dehydrate_votes(self, votes):
        def dehydrate_vote(vote):
            return dict(id=vote.id,
                        url=vote.get_absolute_url(),
                        title=vote.title,
                        score=vote.score)
        return [dehydrate_vote(v) for v in votes]


class AgendaResource(BaseResource):
    ''' Agenda API '''
    class Meta(BaseResource.Meta):
        queryset = Agenda.objects.filter(is_public=True).prefetch_related('agendavotes__vote', 'editors')
        allowed_methods = ['get']
        include_absolute_url = True
        excludes = ['is_public']
        cache = SimpleCache(timeout = 300)

    #editors = fields.ToManyField(UserResource,
    #                'editors',
    #                full=True)
    #votes = fields.ToManyField(AgendaVoteResource,
    #                'agendavotes',
    #                full=True)

    def dehydrate(self, bundle):
        a = bundle.obj
        if self.get_resource_uri(bundle) == bundle.request.path:
            # it's a detailed request so we add the mks & parties scores
            mks_values = dict(a.get_mks_values())
            members = []
            for mk in Member.objects.filter(pk__in=mks_values.keys()).select_related('current_party'):
                # TODO: this sucks, performance wise
                current_party = mk.current_party
                members.append (dict(name=mk.name,
                        score = mks_values[mk.id]['score'],
                        rank = mks_values[mk.id]['rank'],
                        volume = mks_values[mk.id]['volume'],
                        absolute_url = mk.get_absolute_url(),
                        party = current_party.name,
                        party_url = current_party.get_absolute_url(),
                    ))
            bundle.data['members'] = members
            bundle.data['parties'] = [
                dict(
                    name=x.name, score=a.party_score(x),
                    absolute_url=x.get_absolute_url()
                ) for x in Party.objects.prefetch_related('members')
            ]
            bundle.data['votes'] = [
                dict(title=v.vote.title, id=v.id, importance=v.importance,
                     score=v.score, reasoning=v.reasoning)
                for v in bundle.obj.agendavotes.select_related()
            ]


        bundle.data['editors'] = [<commit_msg>Improve agenda list API to use extra_fields param<commit_after>'''
API for the agendas app
'''
from django.contrib.auth.models import User
from tastypie.constants import ALL
import tastypie.fields as fields
from tastypie.cache import SimpleCache
from avatar.templatetags.avatar_tags import avatar_url

from knesset.api.resources.base import BaseResource
from knesset.mks.models import Member, Party
from knesset.agendas.models import Agenda, AgendaVote

class UserResource(BaseResource):
    class Meta(BaseResource.Meta):
        queryset = User.objects.all()
        include_absolute_url = True
        include_resource_uri = False
        allowed_methods = ['get']
        fields = ['username']

    avatar = fields.CharField()

    def dehydrate_avatar(self, bundle):
        return avatar_url(bundle.obj, 48)

class AgendaVoteResource(BaseResource):
    class Meta(BaseResource.Meta):
        queryset = AgendaVote.objects.select_related()
        allowed_methods = ['get']

    title = fields.CharField()

    def dehydrate_title(self, bundle):
        return bundle.obj.vote.title

class AgendaTodoResource(BaseResource):
    class Meta(BaseResource.Meta):
        allowed_methods = ['get']
        queryset = Agenda.objects.all()
        resource_name = 'agenda-todo'
        fields = ['votes_by_conrtoversy', 'votes_by_agendas']

    votes_by_controversy = fields.ListField()
    votes_by_agendas = fields.ListField()

    # TODO: Make this a parameter or setting or something
    NUM_SUGGESTIONS = 10

    def dehydrate_votes_by_agendas(self, bundle):
        votes = bundle.obj.get_suggested_votes_by_agendas(AgendaTodoResource.NUM_SUGGESTIONS)
        return self._dehydrate_votes(votes)

    def dehydrate_votes_by_controversy(self, bundle):
        votes = bundle.obj.get_suggested_votes_by_controversy(AgendaTodoResource.NUM_SUGGESTIONS)
        return self._dehydrate_votes(votes)

    def _dehydrate_votes(self, votes):
        def dehydrate_vote(vote):
            return dict(id=vote.id,
                        url=vote.get_absolute_url(),
                        title=vote.title,
                        score=vote.score)
        return [dehydrate_vote(v) for v in votes]


class AgendaResource(BaseResource):
    ''' Agenda API '''
    class Meta(BaseResource.Meta):
        queryset = Agenda.objects.filter(is_public=True).prefetch_related('agendavotes__vote', 'editors')
        allowed_methods = ['get']
        include_absolute_url = True
        excludes = ['is_public']
        cache = SimpleCache(timeout = 300)
        list_fields= ['absolute_url', 'description', 'editors', 'id', 'name', 'num_followers', 
                      'public_owner_name', 'resource_uri', ]

    #editors = fields.ToManyField(UserResource,
    #                'editors',
    #                full=True)
    #votes = fields.ToManyField(AgendaVoteResource,
    #                'agendavotes',
    #                full=True)

    def dehydrate(self, bundle):
        a = bundle.obj
        mks_values = dict(a.get_mks_values())
        members = []
        for mk in Member.objects.filter(pk__in=mks_values.keys()).select_related('current_party'):
            # TODO: this sucks, performance wise
            current_party = mk.current_party
            members.append (dict(name=mk.name,
                    score = mks_values[mk.id]['score'],
                    rank = mks_values[mk.id]['rank'],
                    volume = mks_values[mk.id]['volume'],
                    absolute_url = mk.get_absolute_url(),
                    party = current_party.name,
                    party_url = current_party.get_absolute_url(),
                ))
        bundle.data['members'] = members
        bundle.data['parties'] = [
            dict(
                name=x.name, score=a.party_score(x),
                absolute_url=x.get_absolute_url()
            ) for x in Party.objects.prefetch_related('members')
        ]
        bundle.data['votes'] = [
            dict(title=v.vote.title, id=v.id, importance=v.importance,
                 score=v.score, reasoning=v.reasoning)
            for v in bundle.obj.agendavotes.select_related()
        ]

        bundle.data['editors'] = [
{'fraction_non_alphanumeric': 0.099644128113879, 'fraction_numerical': 0.00166073546856465, 'mean_word_length': 2.38322632423756, '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': '5186752', 'n_tokens_mistral': 2592, 'n_tokens_neox': 2453, 'n_words': 462}