From 026a0722357d74b143ed2d974ad2d871a56041b3 Mon Sep 17 00:00:00 2001 From: Andrew Dickinson Date: Mon, 20 Apr 2020 09:30:27 -0400 Subject: Add Project History Page (#553) Co-Authored-By: Glandos All project activity can be tracked, using SQLAlchemy-continuum. IP addresses can optionally be recorded. --- ihatemoney/utils.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'ihatemoney/utils.py') diff --git a/ihatemoney/utils.py b/ihatemoney/utils.py index 126b9de..0641d1c 100644 --- a/ihatemoney/utils.py +++ b/ihatemoney/utils.py @@ -2,6 +2,7 @@ import re import os import ast import operator +from enum import Enum from io import BytesIO, StringIO @@ -12,7 +13,6 @@ from babel import Locale from werkzeug.routing import HTTPException, RoutingException from datetime import datetime, timedelta - import csv @@ -257,3 +257,26 @@ def same_bill(bill1, bill2): if bill1[a] != bill2[a]: return False return True + + +class FormEnum(Enum): + """Extend builtin Enum class to be seamlessly compatible with WTForms""" + + @classmethod + def choices(cls): + return [(choice, choice.name) for choice in cls] + + @classmethod + def coerce(cls, item): + """Coerce a str or int representation into an Enum object""" + if isinstance(item, cls): + return item + + # If item is not already a Enum object then it must be + # a string or int corresponding to an ID (e.g. '0' or 1) + # Either int() or cls() will correctly throw a TypeError if this + # is not the case + return cls(int(item)) + + def __str__(self): + return str(self.value) -- cgit v1.1