Completed
Push — master ( dd8a68...0bb740 )
by Mathias
02:58 queued 01:17
created

pyfreebill.management.commands.Command.handle()   B

Complexity

Conditions 6

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 25
rs 7.5385
cc 6
1
# Copyright 2013 Mathias WOLFF
2
# This file is part of pyfreebilling.
3
# 
4
# pyfreebilling is free software: you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation, either version 3 of the License, or
7
# (at your option) any later version.
8
# 
9
# pyfreebilling is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
# 
14
# You should have received a copy of the GNU General Public License
15
# along with pyfreebilling.  If not, see <http://www.gnu.org/licenses/>
16
17
from django.core.management.base import BaseCommand, CommandError
18
from pyfreebill.models import CDR
19
import datetime
20
from django.db import connection
21
from django.utils import timezone
22
from pprint import pprint
23
#import dse
24
import math
25
import decimal
26
import pytz
27
28
class Command(BaseCommand):
29
    args = '<date>'
30
    help = 'delete old cdr from database - failed : delete failed cdr older than 7 days -'
31
32
    def handle(self, *args, **options):
33
        for var in args:
34
            try:
35
36
                current_tz = pytz.utc
37
                if var == "failed":
38
                    dt = datetime.datetime.now()
39
                    today = datetime.datetime(dt.year, dt.month, dt.day, 00, 00, 00).replace(tzinfo=current_tz)
40
                    yesterday = today - datetime.timedelta(days=7)
41
                elif var == "past":
42
                    today = datetime.datetime(2013, 7, 8, 00, 00, 00).replace(tzinfo=current_tz)
43
                    yesterday = today - datetime.timedelta(days=1)
44
                elif var == "custom":
45
                    pass
46
                else:
47
                    return
48
49
# Query construction and delete
50
                qs_delete_cdr = CDR.objects.all().filter(start_stamp__lt=yesterday).filter(effective_duration="0").delete()
51
52
#                pprint(connection.queries)   
53
            except CDR.DoesNotExist:
54
                raise CommandError('cdr does not exist')
55
56
            self.stdout.write('Successfully operations ')
57