Skip to content

Commit

Permalink
git first add
Browse files Browse the repository at this point in the history
  • Loading branch information
ujjawalpoudel committed Apr 8, 2023
1 parent 6789e63 commit e11e44b
Show file tree
Hide file tree
Showing 35 changed files with 917 additions and 0 deletions.
17 changes: 17 additions & 0 deletions app/controllers/chatbotCRUD/create_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import json

from service.response import response
from service.pydanticDecorator import pydantic_validation
from app.validators.chatbot.createChatbotUserValidators import CreateUserModel
from app.models.chatbotDbModel import User


@pydantic_validation(CreateUserModel)
def create_user_main(event, context):
data = json.loads(event["body"])
user = User(**data).save()
body = {
"data": json.loads(user.to_json()),
"msg": "Create User successfully",
}
return response(200, body)
37 changes: 37 additions & 0 deletions app/models/chatbotDbModel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import datetime

# import mongoengine_goodjson as gj
from mongoengine import Document
from mongoengine.fields import DateTimeField, StringField, IntField, EmailField
from mongoengine import ValidationError

# Connect to MongoDB database
# from config import db_connected


def not_null(name):
if len(name) < 2:
raise ValidationError("Name must have at least 2 characters")


class DefaultAttributes:
meta = {"allow_inheritance": True}
creation_date = DateTimeField()
modified_date = DateTimeField(default=datetime.datetime.now)

def save(self, *args, **kwargs):
if not self.creation_date:
self.creation_date = datetime.datetime.now()
self.modified_date = datetime.datetime.now()
return super(DefaultAttributes, self).save(*args, **kwargs)

def update(self, *args, **kwargs):
self.modified_date = datetime.datetime.now()
return super(DefaultAttributes, self).save(*args, **kwargs)


class User(DefaultAttributes, Document):
name = StringField(max_length=200, required=True, validation=not_null)
age = IntField(max_value=100, min_value=0, required=True)
email = EmailField(required=True)
address = StringField()
58 changes: 58 additions & 0 deletions app/routes/chatbotCRUD.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from flask import Blueprint, request
import json

import json

from service.response import response
from service.pydanticDecorator import pydantic_validation
from app.validators.chatbot.createChatbotUserValidators import CreateUserModel
from app.models.chatbotDbModel import User

chatbot_user_module = Blueprint("chatbot_user_module", __name__)


@chatbot_user_module.route("/")
def welcome_chatbot():
return "<h1>Welcome to Medical Chatbot</h1>"


@chatbot_user_module.route("/createUser", methods=["POST"])
# @pydantic_validation(CreateUserModel)
def create_user_main():
data = json.loads(request.data)
print("Data:", data)
user = User(**data).save()
print("yaha gayo ke nae")
body = {
"data": json.loads(user.to_json()),
"msg": "Create User successfully",
}
return response(200, body)


# @chatbot_user_module.route("/updateUser", methods=["PUT"])
# def update_chatbot_user():
# req = json.loads(request.data)
# # summary=budgetSummary(req)
# return json.dumps()


# @chatbot_user_module.route("/deleteUser", methods=["DELETE"])
# def delete_chatbot_user():
# req = json.loads(request.data)
# # summary=excelbudget(req)
# return json.dumps()


# @chatbot_user_module.route("/readUsers", methods=["GET"])
# def get_chatbot_users():
# req = json.loads(request.data)
# # summary=excelbudget(req)
# return json.dumps()


# @chatbot_user_module.route("/readUser", methods=["GET"])
# def get_chatbot_user():
# req = json.loads(request.data)
# # summary=excelbudget(req)
# return json.dumps()
5 changes: 5 additions & 0 deletions app/validators/chatbot/createChatbotUserValidators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from app.validators.models.chatbotUserValidators import UserModel


class CreateUserModel(UserModel):
pass
41 changes: 41 additions & 0 deletions app/validators/models/chatbotUserValidators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import pydantic
from typing import Optional

from utils.validationCheck import email_check, bson_id_check
from app.models.chatbotDbModel import User


class UserIdModel(pydantic.BaseModel, extra=pydantic.Extra.forbid):
userId: str

@pydantic.validator("userId")
@classmethod
def id_valid_check(cls, user_id) -> None:
if bson_id_check(user_id):
try:
User.objects.get(id=user_id)
return user_id
except User.DoesNotExist:
message = "Given user_id ({0}) does not exits in database.".format(
user_id
)
raise ValueError(message)
else:
message = "Given user_id ({0}) is not valid object user_id.".format(user_id)
raise ValueError(message)


class UserModel(pydantic.BaseModel, extra=pydantic.Extra.forbid):
name: str
age: int
email: str
address: Optional[str]

@pydantic.validator("email")
@classmethod
def email_valid_check(cls, email_address) -> None:
if email_check(email_address):
return email_address
else:
message = "Given email ({0}) is not valid.".format(email_address)
raise ValueError(message)
Loading

0 comments on commit e11e44b

Please sign in to comment.