0% found this document useful (0 votes)
6 views

python and mongodb

Uploaded by

Janner Pareja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

python and mongodb

Uploaded by

Janner Pareja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

App.

py

from functions import home, errorPage


from register import register
from login import login
from search import search
from view import view

# global variables
user_up = ''
passw_up = ''
user = ''
passw = ''

def main():
global user_up, passw_up, user, passw

# salto de linea
print("\n")
nav = str(input("Home(h) | Login(l) | Register(r) | View(v) | Serch(s): "))
nav = nav.lower()

if nav == 'h' or nav == 'l' or nav == 'r' or nav == 'v' or nav == 's':
if nav == 'h':
home(main=main)
elif nav == 'l':
login(user, passw, main=main)
elif nav == 'r':
register(user_up, passw_up, main=main)
elif nav == 'v':
view(main=main)
elif nav == 's':
search(main=main)
elif nav != 'h' or nav != 'l' or nav != 'r' or nav != 'v' or nav != 's':
errorPage(main=main)
main()
Register.py

from termcolor import colored


from functions import back, validarDato
# Get the database using the method we defined in pymongo_test_insert file
from pymongo_get_database import get_database
from dateutil import parser

dbname = get_database()
collection_name = dbname["users_list"]

def register(user_up, passw_up, main):


msg = "REGISTER USER"
print("\n")
print(msg.center(50, " "))
print("\n")
user_up = validarDato(info='nombre')

tot = collection_name.count_documents({})
if tot > 0:
item_details = collection_name.find()
for user in item_details:
if user_up == user['user_name']:
print(colored('This user already exists!', 'red'))
back(main)

passw_up = validarDato(info='passw')
passw_re = validarDato(info='repass')

if passw_up != passw_re:
print("\n")
print(colored('Passwords do not match!', 'red'))
back(main)

expiry_date = '2023-07-13T00:00:00.000Z'
expiry = parser.parse(expiry_date)

#new dictionary
user_dict = {"user_name": user_up, "user_pass": passw_up, "expiry_date": expiry}
#insert
collection_name.insert_one(user_dict)
print("\n")
print(colored('Registered user successfully!', 'green'))
back(main)
login.py

from termcolor import colored


from functions import back, clear
# Get the database using the method we defined in pymongo_test_insert file
from pymongo_get_database import get_database
from dateutil import parser

dbname = get_database()
collection_name = dbname["users_list"]

def login(user, passw, main):


msg = "ENTER USER DATA"
print("\n")
print(msg.center(50, " "))
print("\n")
user = str(input("Your User: "))
user = user.lower()

tot = collection_name.count_documents({})
if tot > 0:
item_details = collection_name.find()
for item in item_details:
if user == item['user_name']:
passw = str(input("Your Password: "))

pass_conf = item['user_pass']

if passw == pass_conf:
clear()
print("Home | Login | Register | View | Search")
# salto de linea
print("\n")
wellcome = "WELLCOME TO MY PYTHON APP"
hello = "Hello " + user
print("\n")
print(wellcome.center(50, "="))
print("\n")
print(hello.center(50, " "))
back(main)
else:
print("\n")
print(colored('Your password is wrong!', 'red'))
back(main)

print("\n")
print(colored('This username does not exist!', 'red'))
back(main)
else:
print("\n")
print(colored('Not Users!', 'red'))
back(main)

view.py

from termcolor import colored


from functions import back
# Get the database using the method we defined in pymongo_test_insert file
from pymongo_get_database import get_database
dbname = get_database()

# Create a new collection


collection_name = dbname["users_list"]

def view(main):
msg = "LIST USERS"
print("\n")
print(msg.center(50, " "))
tot = collection_name.count_documents({})

if tot > 0:
print("\n")
item_details = collection_name.find()
init= 0
while init < tot:
print(colored('Name: ', 'green'), item_details[init]['user_name'], colored('
Password: ', 'yellow'), item_details[init]['user_pass'])
init = init+1
back(main)
else:
print("\n")
print(colored('No data to display!', 'red'))
back(main)
search.py

from termcolor import colored


from functions import back
# Get the database using the method we defined in pymongo_test_insert file
from pymongo_get_database import get_database
dbname = get_database()

# Create a new collection


collection_name = dbname["users_list"]

def search(main):
msg = "SEARCH USER"
print("\n")
print(msg.center(50, " "))

tot = collection_name.count_documents({})
if tot > 0:
print("\n")
search = str(input("Find User: "))
search = search.lower()

item_details = collection_name.find()
for user in item_details:
if search == user['user_name']:
print("\n")
print(colored('Name: ', 'green'), user['user_name'], colored(' Password: ',
'yellow'), user['user_pass'])
back(main)
else:
print("\n")
print(colored('This User does not exist!', 'red'))
back(main)
else:
print("\n")
print(colored('No data to display!', 'red'))
back(main)
Functions.py

import os
import sys
from termcolor import colored

def clear():
# Linux
if sys.platform.startswith('linux'):
os.system('clear')
# Windows
elif sys.platform.startswith('win32'):
os.system('cls')

def back(main):
print("\n")
back = str(input("Do you want back? (y/n): "))
back = back.lower()
if back == "y":
clear()
return main()
else:
clear()
quit()

def home(main):
home = "HOME PAGE"
print("\n")
print(home.center(50, " "))
contact_you = "Please, contac you with the admin!"
print("\n")
print(contact_you.center(50, " "))
back(main)

def errorPage(main):
error_page = "Oups! Page not found!"
er_404 = "404"
print("\n")
print(colored(error_page.center(50, " "), 'red'))
print("\n")
print(er_404.center(50, " "))
back(main)

def validarDato(info):
error_dato = True
msg_dato = 'ok'
while error_dato == True:
if msg_dato == 'ok':
if info == 'nombre':
data = str(input("Your User: "))
data = data.lower()
if info == 'passw':
data = str(input("Your Password: "))
if info == 'repass':
data = str(input("Repeat Password: "))
else:
data = str(input(msg_dato))

if not data:
msg_dato = "Dato vacio, volver a intentar: "
else:
error_dato = False

return data

pymongo_get_database.py

from pymongo import MongoClient


def get_database():

# Provide the mongodb atlas url to connect python to mongodb using pymongo
CONNECTION_STRING = "mongodb://localhost:27017"

# Create a connection using MongoClient. You can import MongoClient or use


pymongo.MongoClient
client = MongoClient(CONNECTION_STRING)

# Create the database for our example (we will use the same database throughout the
tutorial
return client['users']

# This is added so that many files can reuse the function get_database()
if __name__ == "__main__":

# Get the database


dbname = get_database()

You might also like