def test_equator_study_fine_calc_with_activity(self): account = Account.objects.get(party=1, middle_name='Middle') lab_fine = LAB_PASS_NEEDED_EQUATOR * LAB_PENALTY # arithmetic progression obl_fine = (INITIAL_STEP_OBL_STD * 2 + ( (OBL_STUDY_NEEDED_EQUATOR - 2) * STEP_OBL_STD)) * (OBL_STUDY_NEEDED_EQUATOR - 1) / 2 user2 = User.objects.get(username='******') user = User.objects.get(username='******') TransactionState.objects.create(name=States.created.value) TransactionType.objects.create(name=TransactionTypeEnum.seminar.value) AttendanceType.objects.create(name=AttendanceTypeEnum.seminar_attend.value) transaction = Transaction.new_transaction( user2, TransactionType.objects.get(name=TransactionTypeEnum.seminar.value), {}) attendance = Attendance.new_attendance( user, 1, AttendanceType.objects.get( name=AttendanceTypeEnum.seminar_attend.value), 'desc', '1111-11-11', transaction) attendance.counted = True attendance.save() fine_with_zero_activity = lab_fine + obl_fine self.assertEqual(account.get_obl_study_fine_equator(), obl_fine) self.assertEqual(account.get_lab_fine_equator(), lab_fine) self.assertEqual(account.get_final_study_fine(), fine_with_zero_activity)
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 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 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 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.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 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)
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 setUp(self): user_creator = User.objects.create_user(first_name='Creator', last_name='Last', username='******', password='******') user_creator.save() Account.objects.create(user=user_creator, middle_name='Middle', grade=10, party=1) user_receiver = User.objects.create_user(first_name='Receiver', last_name='Last', username='******', password='******') user_receiver.save() Account.objects.create(user=user_receiver, middle_name='Middle', grade=10, party=1) AttendanceBlock.objects.create(name='10-11', start_time='10:00:00', end_time='11:00:00') AttendanceBlock.objects.create(name='10-12', start_time='10:00:00', end_time='12:00:00') AttendanceBlock.objects.create(name='11-12', start_time='11:00:00', end_time='12:00:00') AttendanceBlock.objects.create(name='11-13', start_time='11:00:00', end_time='13:00:00') self.creator = user_creator self.receiver = user_receiver self.att_type = AttendanceType.objects.get( name=AttendanceTypeEnum.seminar_attend.value) transaction_type = \ TransactionType.objects.get(name=TransactionTypeEnum.seminar.value) self.transaction = Transaction.new_transaction(self.creator, transaction_type, {})
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_attend.value), formset_data, update_of) 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.fac_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 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