Adding multiple email support for users on Open Event Server
The Open Event Server enables organizers to manage events from concerts to conferences and meet-ups. It offers features for events with several tracks and venues. Event managers can create invitation forms for speakers and build schedules in a drag and drop interface. The event information is stored in a database. The system provides API endpoints to fetch the data, and to modify and update it.
The Open Event Server is based on JSON 1.0 Specification and hence build on top of Flask Rest Json API (for building Rest APIs) and Marshmallow (for Schema).
In this blog, we will talk about how to add support of multiple emails for a user in Open Event Server. The focus is on model and schema creation for this support.
Model Creation
For the UserEmail, we’ll make our model as follows
from app.models import db
class UserEmail(db.Model): def __init__(self, email=None, user_id=None): def __str__(self): def __unicode__(self): |
Now, let’s try to understand the attributes of this model.
- id is most important Column required in every model to set it as primary key and to uniquely identify an UserEmail object.
- email is that attribute which is required hence should be unique and non-nullable.
- Verified attribute is used to check whether a email is verified or not (thus should be boolean)
- User_id is the attribute which specifies id of the user whose email is contained in the UserEmail object.
- Finally, a relationship with the user of id user_id and these emails (associated with the User.id == user_id) will be stored in the attribute emails in User Model.
Schema Creation
For the model UserEmail, we’ll make our schema UserEmailSchema as follows
from marshmallow_jsonapi import fields from marshmallow_jsonapi.flask import Schema, Relationshipfrom app.api.helpers.utilities import dasherizeclass UserEmailSchema(Schema): “”” API Schema for user email Model “””class Meta: “”” Meta class for user email API schema “”” type_ = ‘user-emails’ self_view = ‘v1.user_emails_detail’ self_view_kwargs = {‘id’: ‘<id>’} inflect = dasherize id = fields.Str(dump_only=True) |
- Marshmallow-jsonapi provides a simple way to produce JSON API-compliant data in any Python web framework.
Now, let’s try to understand the schema UserEmailSchema
- id : Same as in model id is used as uniquely identify an UserEmail object.
- email : Same as in model email is required thus allow_none is set to False.
- User_id : user_id is the id of user whose email is contained in a UserEmailSchema object.
- User : It tells whole attributes of the user to which this email belongs to.
So, we saw how to add multiple email support for users on Open Event Server. We just required to create a model and its schema to add this feature. Similarly, to add support for any database model in the project, we need to create Model and Schema with all the attributes as specified in the model too. This Schema creation is done with guidelines of JSONAPI 1.0 Specification using Marshmallow.
Resources
- Documentation | Marshmallow : https://marshmallow-jsonapi.readthedocs.io/en/latest/