def get_transaction_from_form_data(formset_data, update_of): first_form = formset_data[0] creator = User.objects.get(username=first_form['creator_username']) new_transaction = Transaction.new_transaction( creator, TransactionType.objects.get( name=TransactionTypeEnum.workout.value), formset_data, update_of) attendance_block_name = AttendanceBlockEnum.workout.value attendees_count = sum([a['attended'] for a in formset_data]) reward = WorkoutTransactionController._get_reward(attendees_count) for atomic_data in formset_data: attended = atomic_data['attended'] if attended: receiver = User.objects.get( username=atomic_data['receiver_username']) Attendance.new_attendance( receiver, 1, AttendanceType.objects.get( name=AttendanceTypeEnum.workout.value), first_form['description'], first_form['date'], new_transaction, attendance_block_name) Money.new_money( receiver, reward, MoneyType.objects.get(name=MoneyTypeEnum.workout.value), first_form['description'], new_transaction) return new_transaction
def get_transaction_from_form_data(formset_data, update_of): first_form = formset_data[0] creator = User.objects.get(username=first_form['creator_username']) new_transaction = Transaction.new_transaction( creator, TransactionType.objects.get( name=TransactionTypeEnum.fac_pass.value), formset_data, update_of) for atomic_data in formset_data: value = atomic_data['value'] if value: receiver = User.objects.get( username=atomic_data['receiver_username']) Money.new_money( receiver, atomic_data['value'], MoneyType.objects.get(name=MoneyTypeEnum.fac_pass.value), first_form['description'], new_transaction) Attendance.new_attendance( receiver, 1, AttendanceType.objects.get( name=AttendanceTypeEnum.fac_pass.value), first_form['description'], first_form['date'], new_transaction) return new_transaction
def build_transaction_from_api_request(api_request_body): creator = User.objects.get(username=api_request_body.get('creator')) receiver_username = api_request_body.get('money')[0].get('receiver') value = api_request_body.get('money')[0].get('value') description = api_request_body.get('description') formset_data = [{ 'value': value, 'receiver_username': receiver_username, 'creator_username': creator.username, 'description': description }] # validation if value < 1 or value > creator.account.balance: raise P2PIllegalAmount(value) if creator.username == receiver_username: raise SelfMoneyTransfer() if description == '': raise EmptyDescriptionError() receiver_q = User.objects.filter(username=receiver_username) if receiver_q.count() != 1: raise UserDoesNotExist(receiver_username) receiver = User.objects.get(username=receiver_username) new_transaction = Transaction.new_transaction( creator, TransactionType.objects.get(name=TransactionTypeEnum.p2p.value), formset_data) Money.new_money(receiver, value, MoneyType.objects.get(name=MoneyTypeEnum.p2p.value), description, new_transaction) return new_transaction
def get_transaction_from_form_data(formset_data, update_of): first_form = formset_data[0] creator = User.objects.get(username=first_form['creator_username']) new_transaction = Transaction.new_transaction( creator, TransactionType.objects.get( name=TransactionTypeEnum.purchase.value), formset_data, update_of) money_type = MoneyType.objects.get(name=first_form['money_type']) attendance_type = AttendanceType.objects.get( name=AttendanceTypeEnum.book_certificate.value) for atomic_data in formset_data: value = atomic_data['value'] if value: money_value = value certificate_value = 0 receiver = User.objects.get( username=atomic_data['receiver_username']) if money_type.name == MoneyTypeEnum.books.value: money_value, certificate_value = PurchaseTransactionController._get_certificate_split( receiver, value) if money_value: Money.new_money(receiver, -money_value, money_type, first_form['description'], new_transaction) if certificate_value: Attendance.new_attendance(receiver, -certificate_value, attendance_type, first_form['description'], datetime.date.today(), new_transaction) return new_transaction
def get_transaction_from_form_data(formset_data, update_of): first_form = formset_data[0] creator = User.objects.get(username=first_form['creator_username']) new_transaction = Transaction.new_transaction( creator, TransactionType.objects.get( name=TransactionTypeEnum.lecture.value), formset_data, update_of) attendance_block_name = AttendanceBlockEnum.lecture.value money_type = MoneyType.objects.get( name=MoneyTypeEnum.fine_lecture.value) for atomic_data in formset_data: attended = atomic_data['attended'] receiver = User.objects.get( username=atomic_data['receiver_username']) if attended: attendance_type_name = AttendanceTypeEnum.lecture_attend.value else: attendance_type_name = AttendanceTypeEnum.lecture_miss.value fine_value = receiver.account.get_next_missed_lec_penalty() Money.new_money(receiver, -fine_value, money_type, first_form['description'], new_transaction) Attendance.new_attendance( receiver, 1, AttendanceType.objects.get(name=attendance_type_name), first_form['description'], first_form['date'], new_transaction, attendance_block_name=attendance_block_name) return new_transaction
def add_initial_money(group_name): creator = User.objects.get(username=BANKIR_USERNAME) t_type = TransactionType.objects.get( name=TransactionTypeEnum.general_money.value) money_type = MoneyType.objects.get(name=MoneyTypeEnum.initial.value) new_transaction = Transaction.new_transaction(creator, t_type, {}) for student in User.objects.filter(groups__name=group_name): Money.new_money(student, INITIAL_MONEY, money_type, INITIAL_MONEY_DESC, new_transaction) new_transaction.process()
def get_transaction_from_form_data(formset_data, update_of): first_form = formset_data[0] creator = User.objects.get(username=first_form['creator_username']) new_transaction = Transaction.new_transaction( creator, TransactionType.objects.get(name=TransactionTypeEnum.p2p.value), formset_data, update_of) receiver = User.objects.get(username=first_form['receiver_username']) Money.new_money(receiver, first_form['value'], MoneyType.objects.get(name=MoneyTypeEnum.p2p.value), first_form['description'], new_transaction) return new_transaction
def get_transaction_from_form_data(formset_data, update_of): first_form = formset_data[0] creator = User.objects.get(username=first_form['creator_username']) new_transaction = Transaction.new_transaction( creator, TransactionType.objects.get(name=TransactionTypeEnum.fine.value), formset_data, update_of) money_type = MoneyType.objects.get(name=first_form['money_type']) for atomic_data in formset_data: value = atomic_data['value'] if value: value = -value receiver = User.objects.get(username=atomic_data['receiver_username']) Money.new_money(receiver, value, money_type, first_form['description'], new_transaction) return new_transaction
def get_transaction_from_form_data(formset_data, update_of): first_form = formset_data[0] mark = sum([ int(first_form[key]) for key in SeminarTransactionController._get_mark_keys() ]) money_reward = SeminarTransactionController._get_reward_from_mark(mark) creator = User.objects.get(username=first_form['creator_username']) new_transaction = Transaction.new_transaction( creator, TransactionType.objects.get( name=TransactionTypeEnum.seminar.value), formset_data, update_of) reader = User.objects.get(username=first_form['receiver']) Attendance.new_attendance( reader, 1, AttendanceType.objects.get( name=AttendanceTypeEnum.seminar_pass.value), first_form['description'], first_form['date'], new_transaction, first_form['block']) Money.new_money( reader, money_reward, MoneyType.objects.get(name=MoneyTypeEnum.seminar_pass.value), first_form['description'], new_transaction) for atomic_data in formset_data: attended = atomic_data['attended'] if attended: receiver = User.objects.get( username=atomic_data['receiver_username']) Attendance.new_attendance( receiver, 1, AttendanceType.objects.get( name=AttendanceTypeEnum.seminar_attend.value), first_form['description'], first_form['date'], new_transaction, first_form['block']) return new_transaction
def charge_tax(): day = get_session_day() tax_value = get_tax_from_session_day(day) print("Current day {}, tax: {}".format(day, tax_value)) if tax_value <= 0: print('Tax is < 0 transactions not created') return creator = User.objects.get(username=BANKIR_USERNAME) t_type = TransactionType.objects.get( name=TransactionTypeEnum.tax.value) money_type = MoneyType.objects.get(name=MoneyTypeEnum.tax.value) new_transaction = Transaction.new_transaction(creator, t_type, {}) for student in User.objects.filter( groups__name=UserGroups.student.value): Money.new_money(student, -tax_value, money_type, "", new_transaction) new_transaction.process() for ad in new_transaction.related_money_atomics.all(): print(ad.receiver.account.long_name(), ad.receiver.account.get_balance())
def test_undo_non_applied_transaction_raises(self): creator = User.objects.get(username='******') receiver = User.objects.get(username='******') money_type = MoneyType.objects.get(name=MoneyTypeEnum.radio_help.value) transaction_type = \ TransactionType.objects.get(name=TransactionTypeEnum.seminar.value) transaction = Transaction.new_transaction(creator, transaction_type, {}) money = Money.new_money(receiver, 10, money_type, 'description', transaction) with self.assertRaises(AttributeError): money.undo()
def get_transaction_from_form_data(formset_data, update_of): first_form = formset_data[0] creator = User.objects.get(username=first_form['creator_username']) new_transaction = Transaction.new_transaction( creator, TransactionType.objects.get(name=TransactionTypeEnum.ds.value), formset_data, update_of) attendees_count = sum([a['attended'] for a in formset_data]) if attendees_count: money_type = MoneyType.objects.get(name=first_form['money_type']) reward = DSTransactionController._get_reward( attendees_count, money_type) for atomic_data in formset_data: attended = atomic_data['attended'] if attended: receiver = User.objects.get( username=atomic_data['receiver_username']) Money.new_money(receiver, reward, money_type, first_form['description'], new_transaction) return new_transaction
def get_transaction_from_form_data(formset_data, update_of): first_form = formset_data[0] creator = User.objects.get(username=first_form['creator_username']) money_type = MoneyType.objects.get(name=first_form['money_type']) print(money_type) print(first_form) new_transaction = Transaction.new_transaction( creator, TransactionType.objects.get( name=TransactionTypeEnum.activity.value), formset_data, update_of) places = { key: list(filter(lambda t, k=key: t['place'] == str(k), formset_data)) for key in range(1, 5) } for place in places: if not places[place]: continue reward = ActivityTransactionController._get_reward_for_place( place, money_type, len(places[place])) for form in places[place]: receiver = User.objects.get(username=form['receiver_username']) if reward: Money.new_money(receiver, reward, money_type, first_form['description'], new_transaction) if form['cert'] and place == 1: Attendance.new_attendance( receiver, BOOK_CERTIFICATE_VALUE, AttendanceType.objects.get(name=AttendanceTypeEnum. book_certificate.value), first_form['description'], form['date'], new_transaction) return new_transaction
def get_transaction_from_form_data(formset_data, update_of): first_form = formset_data[0] creator = User.objects.get(username=first_form['creator_username']) new_transaction = Transaction.new_transaction( creator, TransactionType.objects.get(name=TransactionTypeEnum.lab.value), formset_data, update_of) money_type = MoneyType.objects.get(name=MoneyTypeEnum.lab_pass.value) att_type = AttendanceType.objects.get( name=AttendanceTypeEnum.lab_pass.value) receiver_1 = User.objects.get( username=first_form['receiver_username_1']) receiver_2 = User.objects.get( username=first_form['receiver_username_2']) description = first_form['description'] description_1 = '{} \nПартнер: {}'.format( description, receiver_2.account.long_name()) description_2 = '{} \nПартнер: {}'.format( description, receiver_1.account.long_name()) Money.new_money(receiver_1, first_form['value_1'], money_type, description_1, new_transaction) Money.new_money(receiver_2, first_form['value_2'], money_type, description_2, new_transaction) Attendance.new_attendance(receiver_1, 1, att_type, description_1, first_form['date'], new_transaction) Attendance.new_attendance(receiver_2, 1, att_type, description_2, first_form['date'], new_transaction) return new_transaction
def get_transaction_from_form_data(formset_data, update_of): first_form = formset_data[0] creator = User.objects.get(username=first_form['creator_username']) new_transaction = Transaction.new_transaction( creator, TransactionType.objects.get(name=TransactionTypeEnum.exam.value), formset_data, update_of) scores = list( filter(None.__ne__, [data['value'] for data in formset_data])) money_type = MoneyType.objects.get(name=MoneyTypeEnum.exam.value) for atomic_data in formset_data: value = atomic_data['value'] if value: reward = ExamTransactionController._get_exam_reward( value, scores) receiver = User.objects.get( username=atomic_data['receiver_username']) Money.new_money(receiver, reward, money_type, first_form['description'], new_transaction) return new_transaction
def test_undo_changes_ballances(self): creator = User.objects.get(username='******') receiver = User.objects.get(username='******') creator_balance = creator.account.balance receiver_balance = receiver.account.balance money_type = MoneyType.objects.get(name=MoneyTypeEnum.radio_help.value) transaction_type = \ TransactionType.objects.get(name=TransactionTypeEnum.seminar.value) transaction = Transaction.new_transaction(creator, transaction_type, {}) money = Money.new_money(receiver, 10, money_type, 'description', transaction) money.apply() money.undo() self.assertEqual(creator.account.balance, creator_balance) self.assertEqual(receiver.account.balance, receiver_balance)