python calendar to calculate month backwards

Posted by Suhail on Stack Overflow See other posts from Stack Overflow or by Suhail
Published on 2010-04-15T06:22:31Z Indexed on 2010/04/15 6:33 UTC
Read the original article Hit count: 372

Filed under:
|
|
|

Hi, we are trying to create a calendar function in python. we have created a small content management system, the requirement is, there will be a drop down list on the top right hand corner of the website, which will give the options - Months - 1 month, 2 months, 3 months and so on..., if the user selects 8 months then it should show the postscount for the last 8 months. the issue is we tried to write a small code which would do the month calculations, but the bug is that it does not consider the months beyond the current year, it shows the postscount only for months of the current year.

for example: if the user selects 3 months, it will show the count for the l 3 months i.e present month and the previous 2 months, but if the user selects option more than 4 months, it does not consider the months from previous year, it still shows the month of the present year only.

I am pasting the code below:-


def __getSpecifiedMailCount__(request, value):

    dbconnector= DBConnector()
    CdateList= "select cdate from mail_records"

    DateNow= datetime.datetime.today()
    DateNow= DateNow.strftime("%Y-%m")
    DateYear= datetime.datetime.today()
    DateYear= DateYear.strftime("%Y")
    DateMonth= datetime.datetime.today()
    DateMonth= DateMonth.strftime("%m")

    #print DateMonth


    def getMonth(value):
        valueDic= {"01": "Jan", "02": "Feb", "03": "Mar", "04": "Apr", "05": "May", "06": "Jun", "07": "Jul", "08": "Aug", "09": "Sep", "10": "Oct", "11": "Nov", "12": "Dec"}
        return valueDic[value]


    def getMonthYearandCount(yearmonth):
        MailCount= "select count(*) as mailcount from mail_records where cdate like '%s%s'" % (yearmonth, "%")
        MailCountResult= MailCount[0]['mailcount']
        return MailCountResult


    MailCountList= []    
    MCOUNT= getMonthYearandCount(DateNow)
    MONTH= getMonth(DateMonth)
    MailCountDict= {}
    MailCountDict['monthyear']= MONTH + ' ' + DateYear
    MailCountDict['mailcount']= MCOUNT
    var_monthyear= MONTH + ' ' + DateYear
    var_mailcount= MCOUNT
    MailCountList.append(MailCountDict)


    i=1
    k= int(value)
    hereMONTH= int(DateMonth)

    while (i < k):
        hereMONTH= int(hereMONTH) - 1
        if (hereMONTH < 10):
            hereMONTH = '0' + str(hereMONTH)
        if (hereMONTH == '00') or (hereMONTH == '0-1'):
            break
        else:
            PMONTH= getMonth(hereMONTH)
            hereDateNow= DateYear + '-' + PMONTH
            hereDateNowNum= DateYear + '-' + hereMONTH
            PMCOUNT= getMonthYearandCount(hereDateNowNum)
            MailCountDict= {}
            MailCountDict['monthyear']= PMONTH + ' ' + DateYear
            MailCountDict['mailcount']= PMCOUNT
            var_monthyear= PMONTH + ' ' + DateYear
            var_mailcount= PMCOUNT
            MailCountList.append(MailCountDict)
        i = i + 1


    #print MailCountList                

    MailCountDict= {'monthmailcount': MailCountList}    
    reportdata = MailCountDict['monthmailcount']
    #print reportdata
    return render_to_response('test.html', locals())

© Stack Overflow or respective owner

Related posts about python

Related posts about calendar