# Path: api/serializers.py
# class EventListSerializers(serializers.ModelSerializer):
#     description_short = serializers.CharField(source='description')
# 
#     class Meta:
#         model = Event
#         fields = (
#             'geoposition',
#             'id'
#             )
# 
#     def transform_description_short(self, obj, value):
#         return Truncator(value).chars(160)
# 
# class EventDetailSerializer(serializers.ModelSerializer):
#     description_short = serializers.CharField(source='description')
# 
#     class Meta:
#         model = Event
#         fields = (
#             'geoposition',
#             'title',
#             'id',
#             'slug',
#             'description_short',
#             'picture'
#             )
# 
#     def transform_description_short(self, obj, value):
#         return Truncator(value).chars(160)
# 
# Path: api/processors.py
# def get_approved_events(limit=None, order=None, country_code=None, past=False):
#     """
#     Select all events which are approved and optionally limit and/or order them
#     """
# 
#     events = Event.objects.filter(status='APPROVED')
# 
#     if not past:
#         events = events.filter(end_date__gte=datetime.datetime.now())
#     if country_code:
#         events = events.filter(country=country_code)
#     if order:
#         events = events.order_by(order)
#     if limit:
#         events = events[:limit]
# 
#     return events
# 
# def get_event_detail(id):
#     """
#     Select event by ID
#     """
# 
#     events = Event.objects.filter(id=id)
# 
# 
# 
#     return events
# 
# Path: api/serializers.py
# class ScoreboardSerializer(serializers.Serializer):
#     country_name = serializers.CharField()
#     score = serializers.FloatField()
#     events = serializers.IntegerField()
#     country_code = serializers.CharField(max_length=2)
# 
# Path: web/processors/event.py
# def \
#         count_approved_events_for_country(past=True):
#     """
#     Count the number of approved events and score for each country
#     """
# 
#     all_events = Event.objects.filter(status='APPROVED')
# 
#     country_count = []
# 
#     # not including the first two fake countries in the list
#     for country in list(countries)[2:]:
#         country_code = country[0]
#         country_name = country[1]
#         number_of_events = all_events.filter(
#             country=country_code).filter(
#             start_date__gte=datetime.date(
#                 datetime.datetime.now().year, 1, 1)).count()
#         population = Country.objects.get(iso=country_code).population
#         country_score = 0
#         if number_of_events > 0 and population > 0 and population != "":
#             country_score = 1. * number_of_events / population
#         country_entry = {'country_code': country_code,
#                          'country_name': country_name,
#                          'events': number_of_events,
#                          'score': country_score}
#         if number_of_events > 0:
#             country_count.append(country_entry)
# 
#     sorted_count = sorted(
#         country_count,
#         key=lambda k: k['score'],
#         reverse=True)
#     return sorted_count


from hashlib import sha1
from rest_framework import generics
from rest_framework_extensions.cache.decorators import cache_response
from api.serializers import EventListSerializers, EventDetailSerializer
from api.processors import get_approved_events, get_event_detail
from api.serializers import ScoreboardSerializer
from web.processors.event import count_approved_events_for_country
    def calculate_cache_key(self, view_instance, view_method, request, args, kwargs):
        return sha1('-'.join([
                repr(request.GET),
                repr(args),
                repr(kwargs),
            ])).hexdigest()


class EventListApi(CachedListAPIView):
    """ Lists approved Events, takes the following optional GET parameters:

* limit
* order
* country_code
* past
    """
    serializer_class = EventListSerializers

    def get_queryset(self):
        params = {
            'limit': self.request.GET.get('limit', None),
            'order': self.request.GET.get('order', None),
            'country_code': self.request.GET.get('country_code', None),
            'past': self.request.GET.get('past', False)
        }

        return get_approved_events(**params)


class EventDetailApi(CachedListAPIView):

# Path: api/serializers.py
# class EventListSerializers(serializers.ModelSerializer):
#     description_short = serializers.CharField(source='description')
# 
#     class Meta:
#         model = Event
#         fields = (
#             'geoposition',
#             'id'
#             )
# 
#     def transform_description_short(self, obj, value):
#         return Truncator(value).chars(160)
# 
# class EventDetailSerializer(serializers.ModelSerializer):
#     description_short = serializers.CharField(source='description')
# 
#     class Meta:
#         model = Event
#         fields = (
#             'geoposition',
#             'title',
#             'id',
#             'slug',
#             'description_short',
#             'picture'
#             )
# 
#     def transform_description_short(self, obj, value):
#         return Truncator(value).chars(160)
# 
# Path: api/processors.py
# def get_approved_events(limit=None, order=None, country_code=None, past=False):
#     """
#     Select all events which are approved and optionally limit and/or order them
#     """
# 
#     events = Event.objects.filter(status='APPROVED')
# 
#     if not past:
#         events = events.filter(end_date__gte=datetime.datetime.now())
#     if country_code:
#         events = events.filter(country=country_code)
#     if order:
#         events = events.order_by(order)
#     if limit:
#         events = events[:limit]
# 
#     return events
# 
# def get_event_detail(id):
#     """
#     Select event by ID
#     """
# 
#     events = Event.objects.filter(id=id)
# 
# 
# 
#     return events
# 
# Path: api/serializers.py
# class ScoreboardSerializer(serializers.Serializer):
#     country_name = serializers.CharField()
#     score = serializers.FloatField()
#     events = serializers.IntegerField()
#     country_code = serializers.CharField(max_length=2)
# 
# Path: web/processors/event.py
# def \
#         count_approved_events_for_country(past=True):
#     """
#     Count the number of approved events and score for each country
#     """
# 
#     all_events = Event.objects.filter(status='APPROVED')
# 
#     country_count = []
# 
#     # not including the first two fake countries in the list
#     for country in list(countries)[2:]:
#         country_code = country[0]
#         country_name = country[1]
#         number_of_events = all_events.filter(
#             country=country_code).filter(
#             start_date__gte=datetime.date(
#                 datetime.datetime.now().year, 1, 1)).count()
#         population = Country.objects.get(iso=country_code).population
#         country_score = 0
#         if number_of_events > 0 and population > 0 and population != "":
#             country_score = 1. * number_of_events / population
#         country_entry = {'country_code': country_code,
#                          'country_name': country_name,
#                          'events': number_of_events,
#                          'score': country_score}
#         if number_of_events > 0:
#             country_count.append(country_entry)
# 
#     sorted_count = sorted(
#         country_count,
#         key=lambda k: k['score'],
#         reverse=True)
#     return sorted_count


# Path: api/views.py
from hashlib import sha1
from rest_framework import generics
from rest_framework_extensions.cache.decorators import cache_response
from api.serializers import EventListSerializers, EventDetailSerializer
from api.processors import get_approved_events, get_event_detail
from api.serializers import ScoreboardSerializer
from web.processors.event import count_approved_events_for_country

    def calculate_cache_key(self, view_instance, view_method, request, args, kwargs):
        return sha1('-'.join([
                repr(request.GET),
                repr(args),
                repr(kwargs),
            ])).hexdigest()


class EventListApi(CachedListAPIView):
    """ Lists approved Events, takes the following optional GET parameters:

* limit
* order
* country_code
* past
    """
    serializer_class = EventListSerializers

    def get_queryset(self):
        params = {
            'limit': self.request.GET.get('limit', None),
            'order': self.request.GET.get('order', None),
            'country_code': self.request.GET.get('country_code', None),
            'past': self.request.GET.get('past', False)
        }

        return get_approved_events(**params)


class EventDetailApi(CachedListAPIView):

    serializer_class = EventDetailSerializer