def test_add_sheet_schema(dbsession, pyramid_request, user, company): import colander from autonomie.models.expense.sheet import ExpenseSheet from autonomie.forms.expense import get_add_edit_sheet_schema sheet = ExpenseSheet(month=1, year=2017, user_id=user.id, company_id=company.id) dbsession.add(sheet) dbsession.flush() schema = get_add_edit_sheet_schema() pyramid_request.context = company pyramid_request.user = user schema = schema.bind(request=pyramid_request) result = schema.deserialize({'month': 2, 'year': 2016}) assert 'month' in result with pytest.raises(colander.Invalid): schema.deserialize({'month': 2}) with pytest.raises(colander.Invalid): schema.deserialize({'month': 22, 'year': 2017}) with pytest.raises(colander.Invalid): schema.deserialize({'month': 2, 'year': -1}) with pytest.raises(colander.Invalid): schema.deserialize({'month': 1, 'year': 2017})
def test_duplicate( config, dbsession, get_csrf_request_with_db, company, user, full_expense_sheet, mk_expense_type, ): from autonomie.views.expenses.expense import ExpenseSheetDuplicateView config.add_route('/expenses/{id}', "/{id}") request = get_csrf_request_with_db(post={ 'month': '10', 'year': '2017', 'submit': 'submit' }) # https://github.com/CroissanceCommune/autonomie/issues/774 mk_expense_type(label='KM', code='KM', amount=0.184, year=2017) request.context = full_expense_sheet view = ExpenseSheetDuplicateView(request) result = view.__call__() assert result.location != "/{id}".format(id=full_expense_sheet.id) from autonomie.models.expense.sheet import ExpenseSheet id = int(result.location[1:]) new_sheet = ExpenseSheet.get(id) assert new_sheet.month == 10 assert new_sheet.year == 2017 assert new_sheet.company_id == company.id assert new_sheet.user_id == user.id assert len(new_sheet.lines) == len(full_expense_sheet.lines) assert len(new_sheet.kmlines) == len(full_expense_sheet.kmlines)
def duplicate(self): """ Duplicate an expense line to an existing ExpenseSheet """ logger.info(u"Duplicate ExpenseKmLine") sheet_id = self.request.json_body.get('sheet_id') sheet = ExpenseSheet.get(sheet_id) if sheet is None: return RestError(["Wrong sheet_id"]) if not self.request.has_permission('edit.expensesheet'): logger.error(u"Unauthorized action : possible break in attempt") raise HTTPForbidden() new_line = self.context.duplicate(sheet=sheet) if new_line.type_object is None: return RestError([ u"Aucun type de frais kilométriques correspondant n'a pu être " u"retrouvé sur l'année {0}".format(sheet.year) ], code=403) new_line.sheet_id = sheet.id self.request.dbsession.add(new_line) self.request.dbsession.flush() return new_line
def test_duplicate( config, dbsession, get_csrf_request_with_db, company, user, full_expense_sheet, mk_expense_type, ): from autonomie.views.expenses.expense import ExpenseSheetDuplicateView config.add_route('/expenses/{id}', "/{id}") request = get_csrf_request_with_db( post={'month': '10', 'year': '2017', 'submit': 'submit'} ) # https://github.com/CroissanceCommune/autonomie/issues/774 mk_expense_type(label='KM', code='KM', amount=0.184, year=2017) request.context = full_expense_sheet view = ExpenseSheetDuplicateView(request) result = view.__call__() assert result.location != "/{id}".format(id=full_expense_sheet.id) from autonomie.models.expense.sheet import ExpenseSheet id = int(result.location[1:]) new_sheet = ExpenseSheet.get(id) assert new_sheet.month == 10 assert new_sheet.year == 2017 assert new_sheet.company_id == company.id assert new_sheet.user_id == user.id assert len(new_sheet.lines) == len(full_expense_sheet.lines) assert len(new_sheet.kmlines) == len(full_expense_sheet.kmlines)
def test_duplicate( config, dbsession, get_csrf_request_with_db, company, user, full_expense_sheet, ): from autonomie.views.expenses.expense import ExpenseSheetDuplicateView config.add_route('/expenses/{id}', "/{id}") request = get_csrf_request_with_db(post={ 'month': '10', 'year': '2017', 'submit': 'submit' }) request.context = full_expense_sheet view = ExpenseSheetDuplicateView(request) result = view.__call__() assert result.location != "/{id}".format(id=full_expense_sheet.id) from autonomie.models.expense.sheet import ExpenseSheet id = int(result.location[1:]) new_sheet = ExpenseSheet.get(id) assert new_sheet.month == 10 assert new_sheet.year == 2017 assert new_sheet.company_id == company.id assert new_sheet.user_id == user.id assert len(new_sheet.lines) == len(full_expense_sheet.lines) assert len(new_sheet.kmlines) == len(full_expense_sheet.kmlines)
def _get_existing_expenses_options(self): """ Return other existing expenses available for expense line duplication """ result = [{ "label": u"{month_label} / {year} (feuille courante)".format( month_label=strings.month_name(self.context.month), year=self.context.year, ), "id": self.context.id, }] all_expenses = ExpenseSheet.query().filter_by( user_id=self.context.user_id) all_expenses = all_expenses.filter_by( company_id=self.context.company_id) all_expenses = all_expenses.filter(ExpenseSheet.id != self.context.id) all_expenses = all_expenses.filter( ExpenseSheet.status.in_(['draft', 'invalid'])) all_expenses = all_expenses.order_by( ExpenseSheet.year.desc()).order_by(ExpenseSheet.month.desc()) result.extend([{ "label": u"{month_label} / {year}".format(month_label=strings.month_name( e.month), year=e.year), "id": e.id } for e in all_expenses]) return result
def test_expensekm_duplicate_ref_7774(mk_expense_type): kmtype = mk_expense_type(amount=1, year=2018) line = ExpenseKmLine(description="", km="", type_object=kmtype) sheet = ExpenseSheet(year=2019, month=1) assert line.duplicate(sheet) is None kmtype2019 = mk_expense_type(amount=1, year=2019) assert line.duplicate(sheet).type_object == kmtype2019
def get_expense_sheet(year, month, cid, uid): """ Return the expense sheet for the given 4-uple """ return ExpenseSheet.query()\ .filter(ExpenseSheet.year == year)\ .filter(ExpenseSheet.month == month)\ .filter(ExpenseSheet.company_id == cid)\ .filter(ExpenseSheet.user_id == uid).first()
def duplicate(self, year, month): sheet = ExpenseSheet() sheet.month = month sheet.year = year sheet.user_id = self.user_id sheet.company_id = self.company_id sheet.lines = [line.duplicate(sheet) for line in self.lines] sheet.kmlines = [line.duplicate(sheet) for line in self.kmlines] return sheet
def expense_sheet( dbsession, company, user, ): from autonomie.models.expense.sheet import ExpenseSheet item = ExpenseSheet( month=10, year=2015, company_id=company.id, user_id=user.id, ) dbsession.add(item) dbsession.flush() return item
def query(self, appstruct, form_name): """ Base Query for expenses :param appstruct: params passed in the query for expense export :param str form_name: The submitted form's name """ query = ExpenseSheet.query() query = query.filter(ExpenseSheet.status == 'valid') if form_name.endswith('_id_form'): query = self._filter_by_sheet_id(query, appstruct) elif form_name.endswith('_main_form'): query = self._filter_by_period(query, appstruct) query = self._filter_by_user(query, appstruct) query = self._filter_by_exported(query, appstruct) return query
def duplicate(self): """ Duplicate an expense line to an existing ExpenseSheet """ logger.info(u"Duplicate ExpenseLine") sheet_id = self.request.json_body.get('sheet_id') sheet = ExpenseSheet.get(sheet_id) if sheet is None: return RestError(["Wrong sheet_id"]) if not self.request.has_permission('edit.expensesheet'): logger.error(u"Unauthorized action : possible break in attempt") raise HTTPForbidden() new_line = self.context.duplicate(sheet=sheet) new_line.sheet_id = sheet.id self.request.dbsession.add(new_line) self.request.dbsession.flush() return new_line
def manage(request): """ The manage view """ estimations = Task.get_waiting_estimations().all() invoices = Task.get_waiting_invoices().all() for item in estimations: item.url = request.route_path('/estimations/{id}', id=item.id) for item in invoices: item.url = request.route_path('/%ss/{id}' % item.type_, id=item.id) expenses = ExpenseSheet.query()\ .filter(ExpenseSheet.status == 'wait')\ .order_by(ExpenseSheet.month)\ .order_by(ExpenseSheet.status_date).all() for expense in expenses: expense.url = request.route_path("/expenses/{id}", id=expense.id) user_id = request.user.id query = Activity.query() query = query.join(Activity.conseillers) query = query.filter( Activity.conseillers.any(User.id == user_id) ) query = query.filter(Activity.status == 'planned') query = query.order_by(Activity.datetime).limit(10) activities = query.all() for activity in activities: activity.url = request.route_path("activity", id=activity.id) return dict( title=u"Mon tableau de bord", invoices=invoices, estimations=estimations, expenses=expenses, activities=activities, )
def validator(node, value): """ The validator """ month = value['month'] year = value['year'] query = ExpenseSheet.query().filter_by(month=month) query = query.filter_by(year=year) query = query.filter_by(user_id=user_id) query = query.filter_by(company_id=company_id) if query.count() > 0: exc = colander.Invalid( node, u"Une note de dépense pour la période {0} {1} existe " u"déjà".format( strings.month_name(month), year, )) exc['month'] = u"Une note de dépense existe" exc['year'] = u"Une note de dépense existe" raise exc
def get_new_expense_sheet(year, month, cid, uid): """ Return a new expense sheet for the given 4-uple """ expense = ExpenseSheet() expense.name = get_expense_sheet_name(month, year) expense.year = year expense.month = month expense.company_id = cid expense.user_id = uid query = ExpenseTelType.query() query = query.filter(ExpenseTelType.active == True) teltypes = query.filter(ExpenseTelType.initialize == True) for type_ in teltypes: line = ExpenseLine(type_id=type_.id, ht=0, tva=0, description=type_.label) expense.lines.append(line) return expense
def validator(node, value): """ The validator """ month = value['month'] year = value['year'] query = ExpenseSheet.query().filter_by(month=month) query = query.filter_by(year=year) query = query.filter_by(user_id=user_id) query = query.filter_by(company_id=company_id) if query.count() > 0: exc = colander.Invalid( node, u"Une note de dépense pour la période {0} {1} existe " u"déjà".format( strings.month_name(month), year, ) ) exc['month'] = u"Une note de dépense existe" exc['year'] = u"Une note de dépense existe" raise exc
def _get_existing_expenses_options(self): """ Return other existing expenses available for expense line duplication """ result = [{ "label": u"{month_label} / {year} (feuille courante)".format( month_label=strings.month_name(self.context.month), year=self.context.year, ), "id": self.context.id, }] all_expenses = ExpenseSheet.query().filter_by( user_id=self.context.user_id ) all_expenses = all_expenses.filter_by( company_id=self.context.company_id ) all_expenses = all_expenses.filter(ExpenseSheet.id != self.context.id) all_expenses = all_expenses.filter( ExpenseSheet.status.in_(['draft', 'invalid']) ) all_expenses = all_expenses.order_by( ExpenseSheet.year.desc() ).order_by( ExpenseSheet.month.desc() ) result.extend([ { "label": u"{month_label} / {year}".format( month_label=strings.month_name(e.month), year=e.year ), "id": e.id } for e in all_expenses ]) return result
def get_new_expense_sheet(year, month, cid, uid): """ Return a new expense sheet for the given 4-uple """ expense = ExpenseSheet() expense.name = get_expense_sheet_name(month, year) expense.year = year expense.month = month expense.company_id = cid expense.user_id = uid query = ExpenseTelType.query() query = query.filter(ExpenseTelType.active == True) teltypes = query.filter(ExpenseTelType.initialize == True) for type_ in teltypes: line = ExpenseLine( type_id=type_.id, ht=0, tva=0, description=type_.label ) expense.lines.append(line) return expense
def query(self): query = ExpenseSheet.query().distinct().outerjoin(ExpenseSheet.user) return query