Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use enums (e.g. class roles, group status, group action type) #826

Open
knrafto opened this issue Aug 28, 2016 · 1 comment
Open

Use enums (e.g. class roles, group status, group action type) #826

knrafto opened this issue Aug 28, 2016 · 1 comment

Comments

@knrafto
Copy link
Contributor

knrafto commented Aug 28, 2016

We have a lot of stringly-typed enums, and this can be error-prone. We should use enums instead.

Using enums could have other benefits, such as a method that makes a human-readable string of the enum (e.g. Role.lab_assistant -> "lab assistant").

Here's how to hook it up to SQLAlchemy (stolen from the OH app):

class EnumType(db.TypeDecorator):
    impl = db.String(255)

    def __init__(self, enum_class):
        super(EnumType, self).__init__(self)
        self.enum_class = enum_class

    def process_bind_param(self, enum_value, dialect):
        return enum_value.name

    def process_result_value(self, name, dialect):
        return self.enum_class[name]

    @property
    def python_type(self):
        return self.enum_class

...

Role = enum.Enum('Role', ['student', 'lab assistant', ...])

class MyModel(db.Model):
    my_enum_column = db.Column(EnumType(Role), nullable=False, index=True)
@Sumukh
Copy link
Member

Sumukh commented Aug 28, 2016

This also looks easier if we need to update the table/run a migration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants