forked from ujjawalpoudel/chatbot-with-flask-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6789e63
commit e11e44b
Showing
35 changed files
with
917 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Oops, something went wrong.