get_payments.py 8.89 KB
#-*- coding:utf-8 -*-

#Copyright (c) 2012, Bartłomiej Nitoń
#All rights reserved.

#Redistribution and use in source and binary forms, with or without modification, are permitted provided 
#that the following conditions are met:

#    Redistributions of source code must retain the above copyright notice, this list of conditions and 
#    the following disclaimer.
#    Redistributions in binary form must reproduce the above copyright notice, this list of conditions 
#    and the following disclaimer in the documentation and/or other materials provided with the distribution.

# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED 
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
# POSSIBILITY OF SUCH DAMAGE.

import codecs

from django.core.management.base import BaseCommand
from django.db.models import Q
from django.contrib.auth.models import User

from dictionary.models import Lemma, Vocabulary
from accounts.models import RealizedLemma
from django.db.models import Sum, Count, Max

class Command(BaseCommand):
    args = 'none'
    help = """
    Complete RealizedLemmas objects about information 
    how many frames where done (ready also are counted), and gives users money
    for ready flat frames.
    """

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

def get_payments():
    payments_path = 'data/payments.csv'
    payments_file = codecs.open(payments_path, 'wt', 'utf-8')
    users = User.objects.all()
    all_made_lemmas_count = 0
    all_made_lemmas_ready = 0
    all_made_lemmas_checked = 0
    all_frames_count_bef = 0
    all_frames_count_after = 0
    all_checked_lemmas_count = 0
    for user in users:
        #realized_lemmas = RealizedLemma.objects.filter(user_stats__user=user, 
        #                                               paid=False,
        #                                               date__range=["2010-01-01", 
        #                                                            "2013-05-01"])
        realized_lemmas = RealizedLemma.objects.filter(user_stats__user=user, 
                                                       paid=False,
                                                       date__range=["2012-05-01", 
                                                                    "2014-06-30"])
        cash_to_pay = realized_lemmas.aggregate(Sum('cash'))['cash__sum']
        
        lemmas_after_date = RealizedLemma.objects.filter(user_stats__user=user, 
                                                         paid=False,
                                                         date__range=["2013-07-01", 
                                                                      "2014-04-30"])
        cash_after_daterange = lemmas_after_date.aggregate(Sum('cash'))['cash__sum']
        if cash_to_pay and cash_after_daterange:
            payments_file.write(u'\nUżytkownik' + ';' + user.username + ';' +
                                u'Wynagrodzenie' + ';' + str(round(cash_to_pay, 2)) + ';' +
                                str(round(cash_after_daterange, 2)) + '\n')
            payments_file.write(';' + u'Słownik' + ';' +
                                u'Zrealizowane hasła' + ';' +
                                u'Zrealizowane hasła (gotowe)' + ';' +
                                u'Zrealizowane hasła (sprawdzone)' + ';' +
                                u'Ramki (przed sprawdzeniem)' + ';' +
                                u'Ramki (po sprawdzeniu)' + ';' +
                                u'Sprawdzone hasła' + '\n') 
        elif cash_to_pay:
            payments_file.write(u'\nUżytkownik' + ';' + user.username + ';' +
                                u'Wynagrodzenie' + ';' + str(round(cash_to_pay, 2)) + '\n')
        
            payments_file.write(';' + u'Słownik' + ';' +
                                u'Zrealizowane hasła' + ';' +
                                u'Zrealizowane hasła (gotowe)' + ';' +
                                u'Zrealizowane hasła (sprawdzone)' + ';' +
                                u'Ramki (przed sprawdzeniem)' + ';' +
                                u'Ramki (po sprawdzeniu)' + ';' +
                                u'Sprawdzone hasła' + '\n') 
        user_made_lemmas_count = 0
        user_made_lemmas_ready = 0
        user_made_lemmas_checked = 0
        user_frames_count_bef = 0
        user_frames_count_after = 0        
        user_checked_lemmas_count = 0
        for vocabulary in Vocabulary.objects.all():
            made_lemmas = realized_lemmas.filter(made_frames__gt=0, 
                                                 lemma__vocabulary=vocabulary)
            voc_frames_count_bef = 0
            voc_frames_count_after = 0  
            voc_made_lemmas_ready = 0
            voc_made_lemmas_checked = 0          
            made_lemmas_count = made_lemmas.count()
            user_made_lemmas_count += made_lemmas_count
            all_made_lemmas_count += made_lemmas_count
            for real_lemma in made_lemmas:
                lemma = Lemma.objects.get(entry=real_lemma.lemma.entry, old=False,
                                          vocabulary=vocabulary)
                if lemma.status.status == 'gotowe':
                    user_made_lemmas_ready += 1
                    all_made_lemmas_ready += 1
                    voc_made_lemmas_ready += 1
                elif lemma.status.status == 'sprawdzone':
                    user_made_lemmas_checked += 1
                    all_made_lemmas_checked += 1
                    voc_made_lemmas_checked += 1
                for frame in lemma.frames.all():   
                    voc_frames_count_after += frame.positions.annotate(num_args=Count('arguments')).aggregate(Max('num_args'))['num_args__max']
                    user_frames_count_after += frame.positions.annotate(num_args=Count('arguments')).aggregate(Max('num_args'))['num_args__max']
                    all_frames_count_after += frame.positions.annotate(num_args=Count('arguments')).aggregate(Max('num_args'))['num_args__max']
                    
                lemma = real_lemma.lemma
                for frame in lemma.frames.all():   
                    voc_frames_count_bef += frame.positions.annotate(num_args=Count('arguments')).aggregate(Max('num_args'))['num_args__max']
                    user_frames_count_bef += frame.positions.annotate(num_args=Count('arguments')).aggregate(Max('num_args'))['num_args__max']
                    all_frames_count_bef += frame.positions.annotate(num_args=Count('arguments')).aggregate(Max('num_args'))['num_args__max']
                    
            checked_lemmas = realized_lemmas.filter(lemma__vocabulary=
                                                    vocabulary).filter(Q(corr_frames__gt=0) | 
                                                                       Q(ncorr_frames__gt=0)).count()
            user_checked_lemmas_count += checked_lemmas
            all_checked_lemmas_count += checked_lemmas
            if checked_lemmas == 0 and made_lemmas_count == 0:
                continue
            payments_file.write(';' + vocabulary.name + ';' + str(made_lemmas_count) + ';'
                                + str(voc_made_lemmas_ready) + ';'
                                + str(voc_made_lemmas_checked) + ';'
                                + str(voc_frames_count_bef) + ';'
                                + str(voc_frames_count_after) + ';'
                                 + str(checked_lemmas) + '\n')   
        if user_checked_lemmas_count != 0 or user_made_lemmas_count != 0:
            payments_file.write(';Suma;' + str(user_made_lemmas_count) + ';'
                                + str(user_made_lemmas_ready) + ';'
                                + str(user_made_lemmas_checked) + ';'
                                + str(user_frames_count_bef) + ';'
                                + str(user_frames_count_after) + ';'
                                + str(user_checked_lemmas_count) + '\n')    
    payments_file.write('\n\n;Suma;' + str(all_made_lemmas_count) + ';'
                        + str(all_made_lemmas_ready) + ';'
                        + str(all_made_lemmas_checked) + ';'
                        + str(all_frames_count_bef) + ';'
                        + str(all_frames_count_after) + ';'
                        + str(all_checked_lemmas_count) + '\n')                                                                 
    payments_file.close()