示例#1
0
def fromParams(req):
    if not req['date']:
        raise ValidationError(_('Enter a valid date.'))
        
    amount = req['amount']
    if re.match('^[-\+\d\.]+$',amount):
        try:
            amount = str(eval(amount))
        except Exception, e:
            logger.error(e)
示例#2
0
def report(request):
    if RequestUtils.param_exist("date", request.REQUEST):
        date = DateService.parseDate(request.REQUEST['date'])
    else:
        date = DateService.addMonth(DateService.todayDate(),-1)
    fromDate = DateService.midNight(DateService.firstDateOfMonth(date))
    toDate = DateService.midNight(DateService.lastDateOfMonth(date),True)
    try:
        q = Expense.objects.filter(date__gte=fromDate, date__lte=toDate)
        filedata = StringIO.StringIO()
        filedata.write('"pk","date","text","amount","paymentTypePk","paymentTypeName","categoryPk","categoryName","subCategoryPk","subCategoryName"\n')
        for expense in q:
            d = {'pk': expense.pk,
                'date': DateService.invert(expense.date),
                'text': expense.text,
                'amount': expense.amount,
                'paymentTypePk': expense.paymentType.pk,
                'paymentTypeName': expense.paymentType.name,
                'categoryPk': expense.subCategory.category.pk,
                'categoryName': expense.subCategory.category.name,
                'subCategoryPk': expense.subCategory.pk,
                'subCategoryName': expense.subCategory.name}
            filedata.write(smart_str('%(pk)s,%(date)s,"%(text)s",%(amount)s,%(paymentTypePk)s,"%(paymentTypeName)s",%(categoryPk)s,"%(categoryName)s",%(subCategoryPk)s,"%(subCategoryName)s"\n' % d, 'latin1'))
        
        filename = "expensereport_%s.csv" % fromDate.strftime("%Y%m")
        
        if RequestUtils.param_exist("gz", request.REQUEST):
            filezip = StringIO.StringIO()
            zipped = gzip.GzipFile(filename, 'wb', fileobj=filezip)
            zipped.write(filedata)
            zipped.close()
            st = StorageService()
            st.file_put(filezip.getvalue(), filename + ".gz")
        else:
            st = StorageService()
            st.file_put(filedata.getvalue(), filename)
        error = ''
    except Exception, e:
        logger.error(str(e))
        send_mail("EXPORT ERROR", 'Processing %s.\n\nError: %s' % (fromDate.strftime("%Y-%m"), str(e)))
        error=str(e)
示例#3
0
def backup(request):
    if RequestUtils.param_exist("date", request.REQUEST):
        today = datetime.datetime.strptime(request.REQUEST['date'],"%Y%m%d")
    else:
        today = datetime.date.today()
    fromDate = DateService.midNight(today - datetime.timedelta(days=7))
    toDate = DateService.midNight(today)
    try:
        records = SyncRecord.objects.filter(created__gte=fromDate, created__lt=toDate)
        data = serializers.serialize("xml", records);
        filename = "%s_cashbackup.xml" % today.strftime("%Y%m%d")
        filedata = StringIO.StringIO()
        zipped = gzip.GzipFile(filename, 'wb', fileobj=filedata)
        zipped.write(data)
        zipped.close()
        st = StorageService()
        st.file_put(filedata.getvalue(), filename + ".gz")
        error = ''
    except Exception, e:
        logger.error(str(e))
        send_mail("EXPORT ERROR", 'Processing %s.\n\nError: %s' % (today.strftime("%Y-%m-%d"), str(e)))
        error=str(e)