frepple_createdates.py :  » ERP » frePPLe » frepple-0.8.0 » contrib » django » freppledb » execute » management » commands » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » ERP » frePPLe 
frePPLe » frepple 0.8.0 » contrib » django » freppledb » execute » management » commands » frepple_createdates.py
#
# Copyright (C) 2007 by Johan De Taeye
#
# This library is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
# General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#

#  file     : $URL: https://frepple.svn.sourceforge.net/svnroot/frepple/tags/0.8.0/contrib/django/freppledb/execute/management/commands/frepple_createdates.py $
#  revision : $LastChangedRevision: 1200 $  $LastChangedBy: jdetaeye $
#  date     : $LastChangedDate: 2010-03-07 21:09:41 +0100 (Sun, 07 Mar 2010) $

import random
from optparse import make_option
from datetime import timedelta,datetime,date

from django.core.management.base import BaseCommand,CommandError
from django.db import connection,transaction
from django.conf import settings
from django.utils.translation import ugettext

from common.models import *
from execute.models import log


class Command(BaseCommand):

  help = '''
  This command initiliazes the date bucketization table in the database.
  '''

  option_list = BaseCommand.option_list + (
      make_option('--start', dest='start', type='string',
          help='Start date in YYYY-MM-DD format'),
      make_option('--end', dest='end', type='string',
          help='End date in YYYY-MM-DD format'),
      make_option('--user', dest='user', type='string',
          help='User running the command'),
      make_option('--nonfatal', action="store_true", dest='nonfatal', 
        default=False, help='Dont abort the execution upon an error'),
  )

  requires_model_validation = False

  def get_version(self):
    return settings.FREPPLE_VERSION


  @transaction.commit_manually
  def handle(self, **options):
    # Make sure the debug flag is not set!
    # When it is set, the django database wrapper collects a list of all sql
    # statements executed and their timings. This consumes plenty of memory
    # and cpu time.
    tmp_debug = settings.DEBUG
    settings.DEBUG = False

    # Pick up the options
    if 'start' in options: start = options['start'] or '2008-1-1'
    else: start = '2008-1-1'
    if 'end' in options: end = options['end'] or '2012-1-1'
    else: end = '2012-1-1'
    if 'user' in options: user = options['user'] or ''
    else: user = ''
    nonfatal = False
    if 'nonfatal' in options: nonfatal = options['nonfatal']

    # Validate the date arguments
    try:
      curdate = datetime.strptime(start,'%Y-%m-%d')
      end = datetime.strptime(end,'%Y-%m-%d')
    except Exception, e:
      raise CommandError("Date is not matching format YYYY-MM-DD")

    try:
      # Logging the action
      log( category='CREATE', theuser=user,
        message = _('Start initializing dates')).save()

      # Delete the previous set of records
      connection.cursor().execute('DELETE FROM dates')
      transaction.commit()

      # Loop over all days in the chosen horizon
      while curdate < end:
        month = int(curdate.strftime("%m"))  # an integer in the range 1 - 12
        quarter = (month-1) / 3 + 1          # an integer in the range 1 - 4
        year = int(curdate.strftime("%Y"))
        dayofweek = int(curdate.strftime("%w")) # day of the week, 0 = sunday, 1 = monday, ...
        year_start = date(year,1,1)
        year_end = date(year+1,1,1)
        week_start = curdate - timedelta((dayofweek+6)%7)
        week_end = curdate - timedelta((dayofweek+6)%7-7)
        if week_start.date() < year_start: week_start = year_start
        if week_end.date() > year_end: week_end = year_end
        
        # Main entry
        Dates(
          day = str(curdate.date()),
          day_start = curdate,
          day_end = curdate + timedelta(1),
          dayofweek = dayofweek,
          week = curdate.strftime("%y W%W"),     # Weeks are starting on monday
          week_start = week_start,
          week_end = week_end,
          month =  curdate.strftime("%b %y"),
          month_start = date(year, month, 1),
          month_end = date(year+month/12, month+1-12*(month/12), 1),
          quarter = "%02d Q%s" % (year-2000,quarter),
          quarter_start = date(year, quarter*3-2, 1),
          quarter_end = date(year+quarter/4, quarter*3+1-12*(quarter/4), 1),
          year = curdate.strftime("%Y"),
          year_start = year_start,
          year_end = year_end,
          ).save()

        # Next date
        curdate = curdate + timedelta(1)

      # Log success
      log(category='CREATE', theuser=user,
        message=_('Finished initializing dates')).save()

    except Exception, e:
      # Log failure and rethrow exception
      try: log(category='CREATE', theuser=user,
        message=u'%s: %s' % (_('Failure initializing dates'),e)).save()
      except: pass
      if nonfatal: raise e
      else: raise CommandError(e)
      
    finally:
      # Commit it all, even in case of exceptions
      try: transaction.commit()
      except: pass
      settings.DEBUG = tmp_debug
ww__w.__jav___a___2_s.c__om | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.