def tracker_edit(id): tracker = Tracker.get_tracker(id) if tracker is None: flash('tracker with id {} does not exist'.format(id)) return redirect( url_for('trackerapp.trackerlist', filter_tracker='open')) form = EditTrackerForm(obj=tracker) form.project.choices = [(p.id, p.name) for p in TrkProject.get_all()] form.user_assigned.choices = [(u.username, u.username) for u in User.get_list_enabled_users()] if not form.project.choices: flash('No Projects exist!') return redirect(url_for('projects.projectlist')) if form.validate_on_submit(): tracker.title = form.title.data tracker.description = form.description.data tracker.priority = int(form.priority.data) tracker.status = int(form.status.data) tracker.project = form.project.data tracker.user_assigned = form.user_assigned.data tracker.prefix = TrkProject.get_project(tracker.project).prefix tracker.categories = form.categories.data files = form.images.data action = 'EDITED' for f in files: if f is not None and not f == '': filename = secure_filename(f.filename) if allowed_file(filename): if filename not in tracker.filenames: tracker.filenames.append(filename) if not os.path.exists( os.path.join(current_app.config['UPLOAD_FOLDER'], tracker.id)): os.mkdir( os.path.join(current_app.config['UPLOAD_FOLDER'], tracker.id)) f.save( os.path.join(current_app.config['UPLOAD_FOLDER'], tracker.id, filename)) action = 'ADDED IMAGES' tracker.user_assigned = current_user.username if tracker.status == 0: tracker.status = 1 #working else: flash('File {} skipped because not image'.format(filename)) tracker.update_tracker_by_form() flash('Tracker {} updated'.format(tracker.number)) TrkChanges.create_change(current_user.username, tracker, tracker.project, action) return redirect( url_for('trackerapp.trackerlist', filter_tracker='open')) else: return render_template('trackerapp/tracker_edit.html', form=form, tracker=tracker, title=Tracker.title)
def test_should_return_an_empty_dictionary_if_no_expenses_are_available_for_a_specific_month( self): tracker = Tracker() expense1 = Expense("cibo", 40, datetime.date(2021, 2, 14)) expense2 = Expense("spesa cibo", 12, datetime.date(2021, 2, 23)) expense3 = Expense("spesa cibo", 12, datetime.date(2021, 3, 3)) tracker.add_expense(expense1) tracker.add_expense(expense2) tracker.add_expense(expense3) expenses = tracker.get_expenses_by_month_and_year(4, 2021) self.assertEqual(True, len(expenses.keys()) == 0)
def test_should_retrieve_expenses_for_a_specific_month_number_3(self): tracker = Tracker() expense1 = Expense("cibo", 40, datetime.date(2021, 2, 14)) expense2 = Expense("spesa cibo", 12, datetime.date(2021, 2, 23)) expense3 = Expense("spesa cibo", 12, datetime.date(2021, 3, 3)) tracker.add_expense(expense1) tracker.add_expense(expense2) tracker.add_expense(expense3) expenses = tracker.get_expenses_by_month_and_year(3, 2021) self.assertEqual(False, expense1 in expenses.values()) self.assertEqual(False, expense2 in expenses.values()) self.assertEqual(True, expense3 in expenses.values())
def test_should_group_expenses_by_category(self): tracker = Tracker() expense1 = Expense("spesa cibo", 40, datetime.date(2021, 2, 14)) expense2 = Expense("spesa cibo", 12, datetime.date(2021, 2, 23)) expense3 = Expense("benzina", 20, datetime.date(2021, 2, 26)) tracker.add_expense(expense1) tracker.add_expense(expense2) tracker.add_expense(expense3) expenses = tracker.get_expenses_by_month_and_year(2, 2021) grouped_expenses = group_expenses_by_category(expenses) self.assertEqual(True, expense1 in grouped_expenses["spesa cibo"]) self.assertEqual(True, expense2 in grouped_expenses["spesa cibo"]) self.assertEqual(True, expense3 in grouped_expenses["benzina"])
def test_should_compute_the_total_for_each_category(self): tracker = Tracker() expense1 = Expense("spesa cibo", 40, datetime.date(2021, 2, 14)) expense2 = Expense("spesa cibo", 12, datetime.date(2021, 2, 23)) expense3 = Expense("benzina", 20, datetime.date(2021, 2, 26)) expense4 = Expense("benzina", 70, datetime.date(2021, 2, 28)) tracker.add_expense(expense1) tracker.add_expense(expense2) tracker.add_expense(expense3) tracker.add_expense(expense4) expenses = tracker.get_expenses_by_month_and_year(2, 2021) grouped_expenses = group_expenses_by_category(expenses) total_by_category = compute_total_by_category(grouped_expenses) self.assertEqual(52, total_by_category[0][1]) self.assertEqual(90, total_by_category[1][1])
def add_tracker(): form = AddTrackerForm() form.project.choices = [(p.id, p.name) for p in TrkProject.get_all()] form.user_assigned.choices = [(u.username, u.username) for u in User.get_list_enabled_users()] form.user_assigned.data = current_user.username if 'sessionproject' in session and session['sessionproject'] != 'ALL': form.project.data = session['sessionproject'] if not form.project.choices: flash('No Projects exist!') return redirect(url_for('projects.projectlist', filter_tracker='open')) if form.validate_on_submit(): tracker = Tracker.add_tracker(form.title.data, form.project.data) tracker.description = form.description.data tracker.priority = int(form.priority.data) tracker.project = form.project.data tracker.prefix = TrkProject.get_project(tracker.project).prefix tracker.categories = form.categories.data tracker.user_assigned = form.user_assigned.data files = form.images.data for f in files: if f is not None and not f == '': filename = secure_filename(f.filename) if allowed_file(filename): tracker.filenames.append(filename) if not os.path.exists( os.path.join(current_app.config['UPLOAD_FOLDER'], tracker.id)): os.mkdir( os.path.join(current_app.config['UPLOAD_FOLDER'], tracker.id)) f.save( os.path.join(current_app.config['UPLOAD_FOLDER'], tracker.id, filename)) else: flash('File {} skipped because not image'.format(filename)) tracker.update_tracker_by_form() TrkChanges.create_change(current_user.username, tracker, tracker.project, 'CREATED') return redirect(url_for('trackerapp.tracker_info', id=tracker.id)) return render_template('trackerapp/add_tracker.html', form=form, title='Add tracker')
def tracker_info(id): tracker = Tracker.get_tracker(id) if tracker is None: flash('tracker with id {} does not exist'.format(id)) return redirect( url_for('trackerapp.trackerlist', filter_tracker='open')) if request.method == 'POST': if request.form['action'] == 'edit': return redirect(url_for('trackerapp.tracker_edit', id=tracker.id)) elif request.form['action'] == 'back': return redirect( url_for('trackerapp.trackerlist', filter_tracker='open')) else: flash('Something strange happened here') return redirect(url_for('trackerapp.trackerlist')) else: return render_template('trackerapp/tracker_info.html', tracker=tracker, title=Tracker.title, statuses=dict(STATUSES), priorities=dict(PRIORITIES), categories=dict(CATEGORIES))
def trackerlist(filter_tracker): filtering = {} if 'sessionproject' in session: if session['sessionproject'] != 'ALL': filtering = {'project': session['sessionproject']} if filter_tracker == 'open': filtering.update({'status': {'$in': [0, 1]}}) if filter_tracker == 'closed': filtering.update({'status': {'$nin': [0, 1]}}) page = request.args.get('page', 1, type=int) trackers = Tracker.get_list_trackers( page, int(current_app.config['TRACKER_PER_PAGE']), filtering=filtering) prev_url = url_for('trackerapp.trackerlist', filter_tracker=filter_tracker, page=trackers.prev) \ if trackers.has_prev else None next_url = url_for('trackerapp.trackerlist', filter_tracker=filter_tracker, page=trackers.next) \ if trackers.has_next else None return render_template('trackerapp/trackerlist.html', title='Trackers', prev_url=prev_url, next_url=next_url, trackers=trackers.trackers, statuses=dict(STATUSES), priorities=dict(PRIORITIES), categories=dict(CATEGORIES))
def test_should_add_a_new_expense(self): tracker = Tracker() expense = Expense("spesa cibo", 12, datetime.date(2021, 2, 23)) tracker.add_expense(expense) self.assertEqual(tracker.expenses["2021-02-23"], expense)
def tracker_popup(id): tracker = Tracker.get_tracker(id) if tracker is not None: return render_template('trackerapp/tracker_popup.html', tracker=tracker) return redirect(url_for('trackerapp.trackerlist', filter_tracker='open'))
def tracker(id): tracker = Tracker.get_tracker(id) return render_template('trackerapp/tracker.html', tracker=tracker, title=Tracker.title)
def delete_tracker(id): if current_user.admin: Tracker.get_tracker(id).remove_tracker() TrkChanges.create_change(current_user.username, tracker, tracker.project, 'DELETED') return redirect(url_for('trackerapp.trackerlist', filter_tracker='open'))