from django.core.management.base import BaseCommand, CommandError
from django.db.models import F
from api import models
import sys
import urllib2, json

class Command(BaseCommand):
    can_import_settings = True

    def handle(self, *args, **options):

        print 'Get total donators'
        total_donators = str(models.UserPreferences.objects.filter(status__isnull=False).exclude(status__exact='').count())

        print 'Check the current contest'
        hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
               'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
               'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
               'Accept-Encoding': 'none',
               'Accept-Language': 'en-US,en;q=0.8',
               'Connection': 'keep-alive'}
        request = urllib2.Request('http://schoolido.lu/contest/json/current', headers=hdr)
        response = urllib2.urlopen(request)
        data = json.load(response)
        current_contest_url = '/contest/'
        current_contest_image = '/static/currentcontest_no.png'
        if 'current' in data and data['current']:
            current_contest_url = '/contest/contest'
            current_contest_image = '/static/currentcontest.png'

        print 'Get ages'
        ages = {}
        prefs = models.UserPreferences.objects.filter(birthdate__isnull=False)
        total_ages = prefs.count()
        for p in prefs:
            age = p.age
            if age > 0 and age < 88:
                if age in ages:
                    ages[age] += 1
                else:
                    ages[age] = 1

        print 'Save generated settings'
        s = '\
TOTAL_DONATORS = ' + total_donators + '\n\
CURRENT_CONTEST_URL = \'' + current_contest_url + '\'\n\
CURRENT_CONTEST_IMAGE = \'' + current_contest_image + '\'\n\
USERS_AGES = ' + unicode(ages) + '\n\
USERS_TOTAL_AGES = ' + unicode(total_ages) + '\n\
'
        print s
        f = open('schoolidolapi/generated_settings.py', 'w')
        print >> f, s
        f.close()

@@ -113,16 +113,52 @@
  models%0A
+from collections import OrderedDict%0A
 import s
@@ -1406,16 +1406,71 @@
 es = %7B%7D%0A
+        for i in range(10,30):%0A            ages%5Bi%5D = 0%0A
         
@@ -1782,16 +1782,65 @@
 age%5D = 1
+%0A        ages = OrderedDict(sorted(ages.items()))
 %0A%0A      
@@ -1888,16 +1888,55 @@
  s = '%5C%0A
+from collections import OrderedDict%5Cn%5C%0A
 TOTAL_DO

Order ages stats + 0 values between 10-30