def pay(request): req = request.REQUEST e = Tax.objects.get(pk=req['id']) if e: if param_exist("nextExpire",req): e.expire = DateService.invert(req['nextExpire']) else: e.expire = e.nextExpire if param_exist("nextExpire2",req): e.nextExpire = DateService.invert(req['nextExpire2']) else: e.nextExpire = None e.amount = req['amount'] e.lastPay = DateService.todayDate() if e.account=="": service = e.service else: service = "%s (%s)" % (e.service, e.account) expense = Expense(date=DateService.todayDate(), text=service, amount=e.amount, subCategory=e.subCategory, paymentType=e.paymentType) data = '{"success":true}' safe = True try: e.save() except _mysql_exceptions.Warning: pass except Exception, e1: safe = False transaction.rollback() data = '{"success":false, "msg": "%s"}' % (e1.args) if safe: try: expense.save() transaction.commit() except _mysql_exceptions.Warning: transaction.commit() except Exception, e2: safe = False transaction.rollback() data = '{"success":false, "msg": "%s"}' % (e2.args)
from cash.models import Expense, SubCategory, PaymentType from django.conf import settings class SyncServiceClass(object): def __init__(self): self.server = xmlrpclib.ServerProxy(settings.RPC_HOST).app def sync_expenses(self): try: data = self.server.getData() except Exception, e: raise else: if data: for e in data: expense = Expense() s = SubCategory.objects.get(pk=e['subCategory']) p = PaymentType.objects.get(pk=e['paymentType']) if e['text']: expense.text = e['text'] else: expense.text = s.name expense.subCategory=s expense.paymentType=p expense.date=e['date'] expense.amount=e['amount'] expense.save() return len(data) else: return 0