def changes(self, id): model = request.environ["sqlalchemy.model"] db = request.environ["sqlalchemy.session"] ops = Operations(db, model) try: c.balance_uid = int(id) except: abort(404) user_uid = h.authenticated_user().uid if not ops.can_see_balance(user_uid, c.balance_uid): abort(403) balance_changes = db.query(model.BalanceChange).filter_by(balance_uid = c.balance_uid).all() total = len(balance_changes) c.expense_categories = db.query(model.ExpenseCategory) c.income_categories = db.query(model.IncomeCategory) try: page_nr = request.params['page_nr'] except: page_nr = 1 c.balance_changes = Page(balance_changes, item_count=total, current_page=page_nr) c.pager = c.balance_changes.pager('Strona [ ~10~ ]', ajax_id="balanceChanges", framework="yui", show_if_single_page=True) return self._render('balances/changes.jinja')
def send_invitation(self, id): model = request.environ["sqlalchemy.model"] db = request.environ["sqlalchemy.session"] ops = Operations(db, model) if not ops.can_modify_balance(h.authenticated_user().uid, id): abort(403) balance = db.query(model.Balance).filter_by(uid = id).one() emails = {} for user in balance.users: emails[user.user.email] = 1 for invitation in balance.invitations: emails[invitation.to_address] = 1 user = db.query(model.AuthenticatedUser).filter_by(uid = h.authenticated_user().uid).one() for address in self.form_result.get('addresses', []): if emails.has_key(address): continue access = model.BalanceInvitation(balance_uid = id, to_address = address, sender = user) db.save(access) db.commit() return redirect_to(h.url_for(action='share'))
def _render(self, template): """ Render template with some common variables. """ model = request.environ["sqlalchemy.model"] db = request.environ["sqlalchemy.session"] ops = Operations(db, model) c.balances = ops.get_balances_by_user(user_uid=h.authenticated_user().uid) return render_jinja(template)
def share(self, id): model = request.environ["sqlalchemy.model"] db = request.environ["sqlalchemy.session"] ops = Operations(db, model) if not ops.can_see_balance(h.authenticated_user().uid, id): abort(403) c.balance = db.query(model.Balance).filter_by(uid = id).one() c.writable = ops.can_modify_balance(h.authenticated_user().uid, id) return self._render('balances/share.jinja')
def edit(self, balance_uid): model = request.environ['sqlalchemy.model'] db = request.environ['sqlalchemy.session'] balance = db.query(model.Balance).filter_by(uid = balance_uid).first() if not balance or not balance.can_modify_balance(h.authenticated_user().uid): return { "failure": Messages.permissionDenied() } uid = int(request.params['uid']) bc = balance.changes.filter_by(uid=uid).first() if not bc: abort(404) try: with localcontext() as ctx: ctx.prec = 2 bc.amount = h.parse_decimal(request.params['amount']) except: log.error("Invalid number: %s" % request.params.get('amount'), exc_info=1) pass try: if int(request.params['is_income']) >= 0: bc.income_category_uid = int(request.params['income_category_uid']) bc.expense_category_uid = None bc.is_income = True else: bc.expense_category_uid = int(request.params['expense_category_uid']) bc.income_category_uid = None bc.is_income = False except: pass try: bc.description = request.params['description'] except: pass try: bc.occurred_on = h.parse_date(request.params['occurred_on']) except: log.error("Invalid date: %s" % request.params.get('occurred_on'), exc_info=1) pass ops = Operations(db, model) try: ops.change_tags_from_string(bc, request.params['tags']) except: pass ops.save_balance_change(bc) return { "id" : bc.uid }
def add(self, balance_uid): model = request.environ['sqlalchemy.model'] db = request.environ['sqlalchemy.session'] if not Operations(db, model).can_modify_balance(h.authenticated_user().uid, balance_uid): return { "failure": Messages.permissionDenied() } try: with localcontext() as ctx: ctx.prec = 2 try: amount = h.parse_decimal(request.params['amount']) except NumberFormatError: return { "failure": Messages.invalidCurrencyFormat() } expense_date = parser.parse(request.params['occurred_on']) income_category_uid = None expense_category_uid = None if (int(request.params['is_income']) >= 0): income_category_uid = int(request.params['income_category_uid']) is_income = True else: expense_category_uid = int(request.params['expense_category_uid']) is_income = False except: return { "failure": Messages.invalidArguments() } tags = request.params.get('tags', '') try: description = request.params['description'] except: description = "" try: bc = model.BalanceChange( amount = amount, description = description, balance_uid = balance_uid, user_uid = h.authenticated_user().uid, expense_category_uid = expense_category_uid, occurred_on = expense_date, is_income = is_income, income_category_uid = income_category_uid ) ops = Operations(db, model) ops.change_tags_from_string(bc, tags) ops.save_balance_change(bc) except IntegrityError, e: return { "failure": Messages.primaryKeyViolation() }
def add(self, balance_uid): model = request.environ['sqlalchemy.model'] db = request.environ['sqlalchemy.session'] balance = db.query(model.Balance).filter_by(uid = balance_uid).first() if not balance or not balance.can_modify_balance(h.authenticated_user().uid): return { "failure": Messages.permissionDenied() } try: with localcontext() as ctx: ctx.prec = 2 try: amount = h.parse_decimal(request.params['amount']) except: log.error("Invalid number: %s" % request.params.get('amount'), exc_info=1) return { "failure": Messages.invalidCurrencyFormat() } expense_date = h.parse_date(request.params['occurred_on']) income_category_uid = None expense_category_uid = None if (int(request.params['is_income']) >= 0): income_category_uid = int(request.params['income_category_uid']) is_income = True else: expense_category_uid = int(request.params['expense_category_uid']) is_income = False except: log.error("Failed to parse arguments %s" % str(request.params), exc_info=1) return { "failure": Messages.invalidArguments() } tags = request.params.get('tags', '') description = request.params.get('description', '') bc = model.BalanceChange( amount = amount, description = description, user_uid = h.authenticated_user().uid, expense_category_uid = expense_category_uid, occurred_on = expense_date, is_income = is_income, income_category_uid = income_category_uid ) balance.changes.append(bc) db.flush() ops = Operations(db, model) ops.change_tags_from_string(bc, tags) ops.save_balance_change(bc) return { "id" : bc.uid }
def edit(self, balance_uid): model = request.environ['sqlalchemy.model'] db = request.environ['sqlalchemy.session'] if not Operations(db, model).can_modify_balance(h.authenticated_user().uid, balance_uid): return { "failure": Messages.permissionDenied() } uid = int(request.params['uid']) bc = db.query(model.BalanceChange).filter_by(uid=uid).filter_by(balance_uid=balance_uid).one() try: with localcontext() as ctx: ctx.prec = 2 bc.amount = h.parse_decimal(request.params['amount']) except: pass try: if int(request.params['is_income']) >= 0: bc.income_category_uid = int(request.params['income_category_uid']) bc.expense_category_uid = None bc.is_income = True else: bc.expense_category_uid = int(request.params['expense_category_uid']) bc.income_category_uid = None bc.is_income = False except: pass try: bc.description = request.params['description'] except: pass try: bc.occurred_on = parser.parse(request.params['occurred_on']) except: pass ops = Operations(db, model) try: ops.change_tags_from_string(bc, request.params['tags']) except: pass ops.save_balance_change(bc) return { "id" : bc.uid }
def unshare(self, id): model = request.environ["sqlalchemy.model"] db = request.environ["sqlalchemy.session"] ops = Operations(db, model) user_uid = request.params.get('user_uid') if not ops.can_modify_balance(h.authenticated_user().uid, id) or h.authenticated_user().uid == user_uid: abort(403) if id is not None and user_uid is not None: user_balance = db.query(model.UserBalance).filter_by(user_uid = user_uid).filter_by(balance_uid = id).first() if user_balance: db.delete(user_balance) db.commit() return redirect_to(h.url_for(action='share'))
def cancel_invitation(self, id): model = request.environ["sqlalchemy.model"] db = request.environ["sqlalchemy.session"] ops = Operations(db, model) if not ops.can_modify_balance(h.authenticated_user().uid, id): abort(403) try: bi = db.query(model.BalanceInvitation).filter_by(token=request.params['token']).one() db.delete(bi) db.commit() except: # we don't care about any errors here pass return redirect_to(h.url_for(action='share'))