Commit 946ad2de by ajil.k

added env file and updated with dictionary comprehension

parent 555bf375
db=db_person
collection=
\ No newline at end of file
[MongoDB]
mongo_uri = mongodb://localhost:27017/
[DataPath]
csv_path = temp/data.csv
[Logs_Path]
log_path = temp/
log_name = logs
[fastapi_connection]
fastapi_port_no = 8000
\ No newline at end of file
# Importing ConfigParser for retrieving data from conf file
from configparser import ConfigParser
# Importing necessary libraries for reading data from env file
import os
from dotenv import load_dotenv
# Create an instance of ConfigParser and read conf file
config = ConfigParser()
config.read(f"conf/application.conf")
# Mongo URI
uri = config.get("MongoDB", "mongo_uri")
csv_path = config.get("DataPath", "csv_path")
# FastAPI Port Number
fastapi_port_no = config.get("fastapi_connection", "fastapi_port_no")
# Log file path and name
log_path = config.get("Logs_Path", "log_path")
log_filename = config.get("Logs_Path", "log_name")
# Load data from .env file
load_dotenv()
db_name = os.getenv("db")
collection_name = os.getenv("collection")
# EndPoints
class APIEndpoints:
# FastAPI endpoints
root = "/"
upload_csv_and_store_in_db = "/upload_csv_and_store_in_db/"
get_details = "/get_details/"
create_new_document = "/create-new-document/"
read_document = "/read-document/{document_id}"
update_document = "/update-document/{document_id}"
delete_document = "/delete-document/{document_id}"
# create a connection
# Importing necessary libraries
from pymongo import MongoClient
from scripts.constants.app_configuration import uri
from scripts.constants.app_configuration import uri, db_name
def db_connect():
# Create a MongoDB connection
try:
# Connect to MongoDB
connection = MongoClient(uri)
# Create a database
db_name = 'db_person'
# Creates a database if there is no such db
db = connection[db_name]
print("Database connected successfully!")
return db
except Exception as e:
# Log the error occurs while making connection with db
from scripts.logging.logger import logger
logger.exception(e)
print("Error ", e)
import json
from mongoengine import connect
from scripts.constants.app_configuration import csv_path
from scripts.constants.db_connection import db_connect
from scripts.constants.mongodb_connection import db_connect
from scripts.core.handlers.insert_data_into_db import insert_into_db
from scripts.core.handlers.read_file import extract_data
from scripts.database.mongodb.models import personal_details, PersonalDetails
connect(db='db_person', host='localhost', port=27017)
from scripts.database.mongodb.models import PersonalDetails
from scripts.logging.logger import logger
from scripts.constants.app_configuration import collection_name
# Class containing all operation performed using api
class OperationsOnApi:
def __init__(self):
self.db = db_connect()
collection_name = 'personal_details'
self.collection = self.db[collection_name]
def upload_csv(self, file):
# Function to upload data
def insert_uploaded_csv_data(self, filename):
try:
data = file.file
data = extract_data(data)
# Uploaded data is being inserted to the mongodb collection
csv_file = filename.file
data = extract_data(csv_file)
insert_status = insert_into_db(self.collection, data)
# Check whether the data got inserted or not
if insert_status:
print("Data inserted from csv to database successfully!")
else:
print("Data insertion to db failed!")
except Exception as e:
logger.exception(e)
print("Data insertion error-", e)
@staticmethod
def get_data_from_db():
def get_data_from_db(self):
try:
documents = personal_details.objects().to_json()
# Read the documents using Mongodb document class
docs = list(self.collection.find({}, {"_id": 0}))
documents = json.dumps(docs, default=str)
json_data = json.loads(documents)
print(json_data)
print("\n-----------------------------Data stored on Database-----------------------------\n")
[print(each) for each in json_data]
return json_data
except Exception as e:
logger.exception(e)
print("Data Fetching error-", e)
def insert_one_document(self, details):
......@@ -44,29 +50,43 @@ class OperationsOnApi:
last_name=details.last_name,
email=details.email,
phone_no=details.phone_no)
result = self.collection.insert_one(dict(person_details))
# Check whether the data got inserted or not
if result:
print("Data inserted to the document\n", dict(person_details))
return result
except Exception as e:
logger.exception(e)
print("Data insertion error-", e)
def read_one_document(self, document_id):
try:
condition_data = {"id": document_id}
document = self.collection.find_one(condition_data)
print("Requested document details:\n", document)
return document
except Exception as e:
logger.exception(e)
print("Data fetching error-", e)
def update_one_document(self, document_id, update_details):
try:
condition_data = {"id": document_id}
# Updating fields query dict
set_data = {"$set": {}}
if update_details.first_name is not None:
set_data["$set"]["first_name"] = update_details.first_name
if update_details.last_name is not None:
set_data["$set"]["last_name"] = update_details.last_name
if update_details.email is not None:
set_data["$set"]["email"] = update_details.email
if update_details.phone_no is not None:
set_data["$set"]["phone_no"] = update_details.phone_no
fields_to_update = ["first_name", "last_name", "email", "phone_no"]
# Append updating fields and its value in set_data
set_data["$set"] = {field_name: getattr(update_details, field_name)
for field_name in fields_to_update
if getattr(update_details, field_name) is not None}
result = self.collection.update_one(condition_data, set_data)
print("Updated Data:\n", set_data["$set"])
print(f"Updated Data of document having id - {document_id}:\n", set_data["$set"])
return result
except Exception as e:
logger.exception(e)
print("Data updation error-", e)
def delete_one_document(self, document_id):
......@@ -77,4 +97,5 @@ class OperationsOnApi:
print("Deleted document details:\n", document)
return result
except Exception as e:
logger.exception(e)
print("Data deletion error-", e)
from scripts.logging.logger import logger
def insert_into_db(collection, data):
try:
status = collection.insert_many(data)
......@@ -6,4 +9,5 @@ def insert_into_db(collection, data):
else:
return False
except Exception as e:
logger.exception(e)
print("Data insertion to db error-", e)
from typing import Optional
from mongoengine import Document, StringField, ObjectIdField, IntField
from pydantic import BaseModel
......@@ -19,12 +18,3 @@ class UpdatePersonalDetails(BaseModel):
last_name: Optional[str] = None
email: Optional[str] = None
phone_no: Optional[int] = None
class personal_details(Document):
_id: ObjectIdField()
id = IntField()
first_name = StringField()
last_name = StringField()
email = StringField()
phone_no = IntField()
import logging
import os
from logging.handlers import RotatingFileHandler
from scripts.constants.app_configuration import log_filename, log_path
def get_logger():
# Creates a rotating log
__logger__ = logging.getLogger('')
__logger__.setLevel(logging.INFO)
log_formatter = '%(asctime)s - %(levelname)-6s - %(message)s'
time_format = "%Y-%m-%d %H:%M:%S"
file_path = log_path
file_name = log_filename
formatter = logging.Formatter(log_formatter, time_format)
if not os.path.exists(file_path):
os.makedirs(file_path)
log_file = os.path.join(f"{file_path}{file_name}.log")
temp_handler = RotatingFileHandler(log_file, maxBytes=1)
temp_handler.setFormatter(formatter)
__logger__.addHandler(temp_handler)
return __logger__
logger = get_logger()
......@@ -19,13 +19,13 @@ async def root():
# Upload and Insert data from csv to database
@app.post(api_endpoint.upload_csv_and_store_in_db, tags=["upload_csv_file_and_store_in_db"])
async def upload_file(file: UploadFile):
OperationsOnApi().upload_csv(file)
async def upload_and_insert(file: UploadFile):
OperationsOnApi().insert_uploaded_csv_data(file)
return {"Message": "Data inserted from csv to database successfully!"}
# Display all documents
@app.get(api_endpoint.get_details, tags=["read documents"])
@app.get(api_endpoint.get_details, tags=["read all documents"])
def get_all_details():
documents = OperationsOnApi().get_data_from_db()
return {"data": documents}
......@@ -38,6 +38,13 @@ async def create_new_document(details: PersonalDetails):
return {"inserted document id": str(result.inserted_id)}
# Delete one Document
@app.get(api_endpoint.read_document, tags=["read document"])
async def read_document(document_id: int):
OperationsOnApi().read_one_document(document_id)
return {"Message": "Document fetched Successfully!"}
# Update one Document
@app.put(api_endpoint.update_document, tags=["update document"])
async def update_document(document_id: int, update_details: UpdatePersonalDetails):
......
2023-02-09 11:45:17 - ERROR - name 'loads' is not defined
Traceback (most recent call last):
File "E:\Python\fast_api_task\scripts\core\handlers\api_functions.py", line 42, in get_data_from_db
json_data = loads(documents)
NameError: name 'loads' is not defined
2023-02-09 11:47:14 - ERROR - name 'loads' is not defined
Traceback (most recent call last):
File "E:\Python\fast_api_task\scripts\core\handlers\api_functions.py", line 42, in get_data_from_db
json_data = loads(documents)
NameError: name 'loads' is not defined
2023-02-09 15:13:11 - ERROR - You have not defined a default connection
Traceback (most recent call last):
File "E:\Python\fast_api_task\scripts\core\handlers\api_functions.py", line 41, in get_data_from_db
documents = personal_details.objects().to_json()
File "E:\Python\fast_api_task\venv\lib\site-packages\mongoengine\queryset\manager.py", line 38, in __get__
queryset = queryset_class(owner, owner._get_collection())
File "E:\Python\fast_api_task\venv\lib\site-packages\mongoengine\document.py", line 224, in _get_collection
db = cls._get_db()
File "E:\Python\fast_api_task\venv\lib\site-packages\mongoengine\document.py", line 202, in _get_db
return get_db(cls._meta.get("db_alias", DEFAULT_CONNECTION_NAME))
File "E:\Python\fast_api_task\venv\lib\site-packages\mongoengine\connection.py", line 395, in get_db
conn = get_connection(alias)
File "E:\Python\fast_api_task\venv\lib\site-packages\mongoengine\connection.py", line 296, in get_connection
raise ConnectionFailure(msg)
mongoengine.connection.ConnectionFailure: You have not defined a default connection
2023-02-09 15:14:45 - ERROR - 'Collection' object is not callable. If you meant to call the 'objects' method on a 'Collection' object it is failing because no such method exists.
Traceback (most recent call last):
File "E:\Python\fast_api_task\scripts\core\handlers\api_functions.py", line 41, in get_data_from_db
documents = self.collection.objects().to_json()
File "E:\Python\fast_api_task\venv\lib\site-packages\pymongo\collection.py", line 3213, in __call__
raise TypeError(
TypeError: 'Collection' object is not callable. If you meant to call the 'objects' method on a 'Collection' object it is failing because no such method exists.
2023-02-09 15:15:19 - ERROR - 'Collection' object is not callable. If you meant to call the 'to_json' method on a 'Collection' object it is failing because no such method exists.
Traceback (most recent call last):
File "E:\Python\fast_api_task\scripts\core\handlers\api_functions.py", line 41, in get_data_from_db
documents = self.collection.to_json()
File "E:\Python\fast_api_task\venv\lib\site-packages\pymongo\collection.py", line 3213, in __call__
raise TypeError(
TypeError: 'Collection' object is not callable. If you meant to call the 'to_json' method on a 'Collection' object it is failing because no such method exists.
2023-02-09 15:18:04 - ERROR - the JSON object must be str, bytes or bytearray, not list
Traceback (most recent call last):
File "E:\Python\fast_api_task\scripts\core\handlers\api_functions.py", line 42, in get_data_from_db
json_data = json.loads(documents)
File "C:\Users\ajil.k\AppData\Local\Programs\Python\Python39\lib\json\__init__.py", line 339, in loads
raise TypeError(f'the JSON object must be str, bytes or bytearray, '
TypeError: the JSON object must be str, bytes or bytearray, not list
2023-02-09 15:21:57 - ERROR - 'Cursor' object has no attribute 'toArray'
Traceback (most recent call last):
File "E:\Python\fast_api_task\scripts\core\handlers\api_functions.py", line 41, in get_data_from_db
documents = self.collection.find({}).toArray()
AttributeError: 'Cursor' object has no attribute 'toArray'
2023-02-09 15:23:13 - ERROR - the JSON object must be str, bytes or bytearray, not Cursor
Traceback (most recent call last):
File "E:\Python\fast_api_task\scripts\core\handlers\api_functions.py", line 43, in get_data_from_db
json_data = json.loads(documents)
File "C:\Users\ajil.k\AppData\Local\Programs\Python\Python39\lib\json\__init__.py", line 339, in loads
raise TypeError(f'the JSON object must be str, bytes or bytearray, '
TypeError: the JSON object must be str, bytes or bytearray, not Cursor
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment