# Path: bundestagger/bundestag/models.py
# class Politician(models.Model):
#     first_name = models.CharField(blank=True, max_length=100)
#     last_name_prefix = models.CharField(blank=True, max_length=100)
#     last_name = models.CharField(blank=True, max_length=100)
#     title = models.CharField(blank=True, max_length=100)
#     party = models.ForeignKey(Party, blank=True, null=True)
# #    party_member = models.OneToOneField(PartyMember, blank=True, null=True)
#     location = models.CharField(null=True, blank=True, max_length=255)
#     born = models.DateField(null=True, blank=True)
#     
#     objects = PoliticianManager()
# 
#     def __unicode__(self):
#         if self.party_id is not None:
#             party = Party.objects.get(id=self.party_id)
#             return u"%s (%s)" % (self.name, party)
#         else:
#             return u"%s (unbekannt)" % (self.name)
# 
#     @property
#     def slug(self):
#         return slugify(self.name)
# 
#     def get_absolute_url(self):
#         return reverse("bundestagger-bundestag-show_politician", args=(self.slug, self.id))
# 
#     @property    
#     def name(self):
#         if len(self.title):
#             title = self.title + u" "
#         else:
#             title= u""
#         if len(self.first_name):
#             first_name = self.first_name + u" "
#         else:
#             first_name = u""
#         if len(self.last_name_prefix):
#             last_name_prefix = self.last_name_prefix + u" "
#         else:
#             last_name_prefix = u""
#         return "%s%s%s%s" % (title, first_name, last_name_prefix,self.last_name)
#         
#     def all_relations(self):
#         relations = [x for x in dir(self) if x.endswith("_set")]
#         result = []
#         for relation in relations:
#             result.extend(getattr(self, relation).all())
#         return result
#         
#     def replace_with(self, real_politician):
#         relations = [x for x in dir(self) if x.endswith("_set")]
#         for relation in relations:
#             relset = getattr(self, relation).all()
#             if len(relset) > 0:
#                 politician_fields = []
#                 for obj in relset:
#                     fields = obj.__class__._meta.fields
#                     for field in fields:
#                         try:
#                             if field.rel.to.__name__ == self.__class__.__name__:
#                                 politician_fields.append(field.name)
#                         except AttributeError:
#                             pass
#                     if len(politician_fields)>0:
#                         for pf in politician_fields:
#                             if getattr(obj, pf) == self:
#                                 setattr(obj, pf, real_politician)
#                                 obj.save()
# 
# class Party(models.Model):
#     name = models.CharField(blank=True, max_length=100)
#     abbr = models.CharField(blank=True, max_length=30)
#     
#     objects = PartyManager()
#     
#     color_mapping = {
#         1 : "#000000",
#         2 : "#F1AB00",
#         3 : "#D71F1D",
#         4 : "#BE3075",
#         5 : "#78BC1B",
#         6 : "#999999"
#     }
#     
#     def __unicode__(self):
#         return self.abbr
# 
#     @property
#     def color(self):
#         return self.color_mapping[self.id]
# 
# class Event(models.Model):
#     # Zwischenruf, Buhruf, Heiterkeit, Beifall
#     context = models.ForeignKey(SpeechPart, null=True, blank=True)
#     kind = models.CharField(blank=True, max_length=100)
#     actor_politician = models.ForeignKey(Politician, null=True, blank=True)
#     actor_party = models.ForeignKey(Party, null=True, blank=True)    
#     text = models.TextField(blank=True, null=True)
# 
#     def __unicode__(self):
#         return u"%s: %s (%s - %s @ %s)" % (self.kind, self.actor, self.text, self.id, self.context_id)
#     
#     @property
#     def actor(self):
#         if self.actor_politician is not None:
#             return self.actor_politician
#         else:
#              return self.actor_party
# 
#     def display(self):
#         out = u""
#         try:
#             if self.kind != "":
#                 out+=u"[%s] " % self.kind
#             if self.actor is not None:
#                 out+=u"%s" % self.actor
#             if self.text is not None and len(self.text)>0:
#                 if self.actor is not None or self.kind !="":
#                     out+=": "
#                 out+=self.text
#         except Exception,e:
#             return e
#         return out


from django.db import models
from django.db.models import Q
from bundestagger.bundestag.models import Politician, Party, Event
import pickle
import marshal, types
import pprint
    def __unicode__(self):
        return self.description
        
    def recalculate(self, top=None):
        self.featurerank_set.all().delete()
        calc = pickle.loads(self.calculations)
        top = top or calc["top"]
        code = marshal.loads(calc["key_function"])
        func = types.FunctionType(code, globals(), "key_function")
        queryset = Q()
        queryset.query = calc["query"]        
        self.calculate_feature(None, queryset, top=top, key_function=func)
    
    def calculate_feature(self, queryset, top=10, key_function=None):
        results = queryset.all()
        func_string = marshal.dumps(key_function.func_code)
        self.calculation = pickle.dumps({"query": results.query, "top":top, "key_function":func_string})
        aggregated = {}
        count = len(results)
        i= 0
        for result in results:
            if i % 1000 == 0:
                print "%d/%d" % (i, count)
#                if i>2000: break
            i+=1
            key = key_function(result)
            aggregated.setdefault(key,0)
            aggregated[key]+=1
        sorted_aggregation =  sorted(aggregated, key=lambda x: aggregated[x], reverse=True)
        firsttop = [(key,aggregated[key]) for key in sorted_aggregation[:top]]

# Path: bundestagger/bundestag/models.py
# class Politician(models.Model):
#     first_name = models.CharField(blank=True, max_length=100)
#     last_name_prefix = models.CharField(blank=True, max_length=100)
#     last_name = models.CharField(blank=True, max_length=100)
#     title = models.CharField(blank=True, max_length=100)
#     party = models.ForeignKey(Party, blank=True, null=True)
# #    party_member = models.OneToOneField(PartyMember, blank=True, null=True)
#     location = models.CharField(null=True, blank=True, max_length=255)
#     born = models.DateField(null=True, blank=True)
#     
#     objects = PoliticianManager()
# 
#     def __unicode__(self):
#         if self.party_id is not None:
#             party = Party.objects.get(id=self.party_id)
#             return u"%s (%s)" % (self.name, party)
#         else:
#             return u"%s (unbekannt)" % (self.name)
# 
#     @property
#     def slug(self):
#         return slugify(self.name)
# 
#     def get_absolute_url(self):
#         return reverse("bundestagger-bundestag-show_politician", args=(self.slug, self.id))
# 
#     @property    
#     def name(self):
#         if len(self.title):
#             title = self.title + u" "
#         else:
#             title= u""
#         if len(self.first_name):
#             first_name = self.first_name + u" "
#         else:
#             first_name = u""
#         if len(self.last_name_prefix):
#             last_name_prefix = self.last_name_prefix + u" "
#         else:
#             last_name_prefix = u""
#         return "%s%s%s%s" % (title, first_name, last_name_prefix,self.last_name)
#         
#     def all_relations(self):
#         relations = [x for x in dir(self) if x.endswith("_set")]
#         result = []
#         for relation in relations:
#             result.extend(getattr(self, relation).all())
#         return result
#         
#     def replace_with(self, real_politician):
#         relations = [x for x in dir(self) if x.endswith("_set")]
#         for relation in relations:
#             relset = getattr(self, relation).all()
#             if len(relset) > 0:
#                 politician_fields = []
#                 for obj in relset:
#                     fields = obj.__class__._meta.fields
#                     for field in fields:
#                         try:
#                             if field.rel.to.__name__ == self.__class__.__name__:
#                                 politician_fields.append(field.name)
#                         except AttributeError:
#                             pass
#                     if len(politician_fields)>0:
#                         for pf in politician_fields:
#                             if getattr(obj, pf) == self:
#                                 setattr(obj, pf, real_politician)
#                                 obj.save()
# 
# class Party(models.Model):
#     name = models.CharField(blank=True, max_length=100)
#     abbr = models.CharField(blank=True, max_length=30)
#     
#     objects = PartyManager()
#     
#     color_mapping = {
#         1 : "#000000",
#         2 : "#F1AB00",
#         3 : "#D71F1D",
#         4 : "#BE3075",
#         5 : "#78BC1B",
#         6 : "#999999"
#     }
#     
#     def __unicode__(self):
#         return self.abbr
# 
#     @property
#     def color(self):
#         return self.color_mapping[self.id]
# 
# class Event(models.Model):
#     # Zwischenruf, Buhruf, Heiterkeit, Beifall
#     context = models.ForeignKey(SpeechPart, null=True, blank=True)
#     kind = models.CharField(blank=True, max_length=100)
#     actor_politician = models.ForeignKey(Politician, null=True, blank=True)
#     actor_party = models.ForeignKey(Party, null=True, blank=True)    
#     text = models.TextField(blank=True, null=True)
# 
#     def __unicode__(self):
#         return u"%s: %s (%s - %s @ %s)" % (self.kind, self.actor, self.text, self.id, self.context_id)
#     
#     @property
#     def actor(self):
#         if self.actor_politician is not None:
#             return self.actor_politician
#         else:
#              return self.actor_party
# 
#     def display(self):
#         out = u""
#         try:
#             if self.kind != "":
#                 out+=u"[%s] " % self.kind
#             if self.actor is not None:
#                 out+=u"%s" % self.actor
#             if self.text is not None and len(self.text)>0:
#                 if self.actor is not None or self.kind !="":
#                     out+=": "
#                 out+=self.text
#         except Exception,e:
#             return e
#         return out


# Path: bundestagger/bundesstats/models.py
from django.db import models
from django.db.models import Q
from bundestagger.bundestag.models import Politician, Party, Event
import pickle
import marshal, types
import pprint

    def __unicode__(self):
        return self.description
        
    def recalculate(self, top=None):
        self.featurerank_set.all().delete()
        calc = pickle.loads(self.calculations)
        top = top or calc["top"]
        code = marshal.loads(calc["key_function"])
        func = types.FunctionType(code, globals(), "key_function")
        queryset = Q()
        queryset.query = calc["query"]        
        self.calculate_feature(None, queryset, top=top, key_function=func)
    
    def calculate_feature(self, queryset, top=10, key_function=None):
        results = queryset.all()
        func_string = marshal.dumps(key_function.func_code)
        self.calculation = pickle.dumps({"query": results.query, "top":top, "key_function":func_string})
        aggregated = {}
        count = len(results)
        i= 0
        for result in results:
            if i % 1000 == 0:
                print "%d/%d" % (i, count)
#                if i>2000: break
            i+=1
            key = key_function(result)
            aggregated.setdefault(key,0)
            aggregated[key]+=1
        sorted_aggregation =  sorted(aggregated, key=lambda x: aggregated[x], reverse=True)
        firsttop = [(key,aggregated[key]) for key in sorted_aggregation[:top]]

        sorted_aggregation = sorted_aggregation[top:]