Commit 4530f940 by arun.uday

v 2

parents
# Default ignored files
/shelf/
/workspace.xml
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9 (task4.1)" project-jdk-type="Python SDK" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/task4.1.iml" filepath="$PROJECT_DIR$/.idea/task4.1.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/venv" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
# main application for database task
from scripts.services.access_functions import db_access_op
print("Mongo DB")
db_access_op()
# database servers
[connection]
mongodb = mongodb://localhost:27017
# accessing conf variables
import configparser
# creating configparser
configparse = configparser.ConfigParser()
configparse.read("conf/db.conf")
# db connection
client_connect = configparse.get("connection", "mongodb")
# Exception constant
exception_msg = "Exception occurred"
# connecting to mongodb server
from pymongo import MongoClient
from scripts.config.mongo_connect_config import client_connect
# connect to server
client = MongoClient(client_connect)
# reading user inputs
def read_inputs():
p_id = int(input("Enter the product id: "))
p_name = input("Enter the product name: ")
p_category = input("Enter the category: ")
retail_price = int(input("Enter the retail price: "))
discount_price = int(input("Enter the discounted price: "))
description = input("Enter the product description: ")
over_rating = input("Enter the overall rating")
return p_id, p_name, p_category, retail_price, discount_price, description, over_rating
# inserting data to mongo
def insert_data_db(db, p_id, p_name, p_category, retail_price, discount_price, description, over_rating):
try:
insert_res = db.insert_one({"Product Id": p_id, "Product Name": p_name, "Product category": p_category,
"Retail price": retail_price, "Discount Price": discount_price,
"Description": description,
"Overall rating": over_rating})
except Exception as e:
print("Exception occurred :", e)
return insert_res
# deleting data
# getting user inputs
def delete_db_input():
field_name = input("Enter the field condition: ")
del_data = int(input("Enter the data: "))
return field_name, del_data
# deleting the record
def db_delete(db, field_name, del_data):
filter_del = {field_name: del_data}
return db.delete_one(filter_del)
# updating the db
# user data
def input_update_db():
field_name = input("Enter the field condition: ")
up_data = int(input("Enter the data: "))
up_field_name = input("Enter the field to update: ")
new_data = input("Enter the new data: ")
return field_name, up_data, up_field_name, new_data
# updating the database
def db_update(db, field_name, up_data, up_field_name, new_data):
cond_data = {field_name: up_data}
update_data = {"$set": {up_field_name: new_data}}
return db.update_many(cond_data, update_data)
# view db data
# display conditions
import pandas as pd
# display conditions
def view_cond():
field_filter = input("Enter the field name: ")
data_find = int(input("Enter the data to find: "))
return field_filter, data_find
# view the data
def view_fields(db, field_filter, data_find):
filter_data = {field_filter: data_find}
view_data_cursor = db.find(filter_data, {'_id': 0})
view_cursor_count = db.count_documents(filter_data)
if view_cursor_count > 0:
print("Data collected....")
view_data = pd.json_normalize((view_data_record for view_data_record in view_data_cursor))
print(view_data)
else:
print("Data fetching failed")
# access class functions
from scripts.services.mongo_db_operations import MongoDbStart
def db_access_op():
db_class = MongoDbStart()
db_created = db_class.mongo_create()
while True:
try:
user_query = input("Enter insert/ update/ delete/ view/ quit : ")
if user_query == 'insert':
db_class.mongo_insert(db_created)
elif user_query == 'update':
db_class.mongo_update(db_created)
elif user_query == 'delete':
db_class.mongo_delete(db_created)
elif user_query == 'view':
db_class.mongo_view(db_created)
else:
print("Exiting....")
break
except Exception as e:
print("Exception occurred: ", e)
else:
choice = input("Do you want to continue (yes/no): ")
if choice == 'yes':
continue
else:
print("Thank you....")
break
# class for mongodb operations
from scripts.constants.file_constants import exception_msg
from scripts.core.engine.mongodb_connect import client
from scripts.core.handlers.data_insert import insert_data_db, read_inputs
from scripts.core.handlers.delete_db_data import delete_db_input, db_delete
from scripts.core.handlers.update_mongo_db import db_update, input_update_db
from scripts.core.handlers.view_data_db import view_fields, view_cond
class MongoDbStart:
@staticmethod
def mongo_create():
try:
client_con = client
except Exception as e:
print("Connection failed: ", e)
else:
db = client_con['storeManager']
store_manager = db['products']
return store_manager
@staticmethod
# getting input and insert into db
def mongo_insert(db):
try:
num_prod = int(input("Enter the number of products: "))
while num_prod > 0:
p_id, p_name, p_category, retail_price, discount_price, description, over_rating = read_inputs()
insert_check = insert_data_db(db, p_id, p_name,
p_category, retail_price, discount_price, description, over_rating)
if insert_check.acknowledged:
print("Data inserted....")
num_prod -= 1
else:
print("Something went wrong data cannot be inserted....")
except Exception as e:
print("Insertion failed", e)
@staticmethod
def mongo_update(db):
try:
field_name, up_data, up_field_name, new_data = input_update_db()
update_check = db_update(db, field_name, up_data, up_field_name, new_data)
if update_check.matched_count > 0:
print("Data updated")
else:
print("Data not updated")
except Exception as e:
print(f'{exception_msg}{e}')
@staticmethod
def mongo_delete(db):
try:
field_name, del_data = delete_db_input()
del_check = db_delete(db, field_name, del_data)
if del_check.deleted_count > 0:
print("Data deleted")
else:
print("Data not deleted")
except Exception as e:
print(f'{exception_msg}{e}')
@staticmethod
def mongo_view(db):
try:
field_filter, data_find = view_cond()
view_fields(db, field_filter, data_find)
except Exception as e:
print(f'{exception_msg}{e}')
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