matiboy / test_contact_app

test application using Python-Flask-SQLAlchemy
0 stars 0 forks source link

Backend endpoints #4

Open matiboy opened 7 years ago

matiboy commented 7 years ago
nidhinbose89 commented 7 years ago

Validation of phone by sqlalchemy.orm.validates

from coaster.sqlalchemy import BaseMixin  # id, created_at, updated_at
from sqlalchemy.orm import validates
from sqlalchemy import (
    Column, String, Text
)
from database import Base

class MyContact(BaseMixin, Base):
    __tablename__ = 'my_contact'
    default_name = "just-a-name"

    name = Column(String(32), nullable=False)
    number = Column(String(32), nullable=False)
    about = Column(Text(64), nullable=False)

    @validates('number')
    def validate_email(self, key, number):
        # add validation logic here
        if 8 <= len(str(number)) <= 12:
            return number
        raise Exception("Number not valid length")

Various HTTP methods used for CRUD are GET, POST, PUT, DELETE and PATCH. There are other methods too like CONNECT, OPTIONS, TRACE.

I used POST instead of DELETE since it was a form submit via HTML and HTML forms didn't support setting method as DELETE but only GET or POST.