Glider
Loading...
Searching...
No Matches
MongoConnection.py
Go to the documentation of this file.
1import os
2import ssl
3from pymongo import MongoClient
4from dotenv import load_dotenv, dotenv_values
5
6load_dotenv()
7
9 """
10 Makes mongo connection and depending of case, returns the right collection
11 """
12 def __init__(self):
13 pass
14
16 """
17 Connect with sales collection
18
19 Args:
20 None
21 Returns collection (str)
22 """
23 client = self.mongo_conn()
24 db_name = os.environ.get("DB")
25 collection_name = os.environ.get("COLLECTION")
26 if client:
27 db = client[db_name]
28 collection = db[collection_name] #collection to upload sales
29 return collection
30 raise Exception("Mongo Connection Fails.")
31
33 """
34 Connect with catalogue collection
35
36 Args:
37 None
38 Returns catalogue_collection (str)
39 """
40 client = self.mongo_conn()
41 db_name = os.environ.get("DB")
42 catalog_name = os.environ.get("AS_COLLECTION")
43 if client:
44 db = client[db_name]
45 catalogue_collection = db[catalog_name]
46 return catalogue_collection
47 raise Exception("Mongo Connection Fails.")
48
50 """
51 Connect with catalogue collection
52
53 Args:
54 None
55 Returns catalogue_collection (str)
56 """
57 client = self.mongo_conn()
58 db_name = os.environ.get("DB")
59 catalog_name = os.environ.get("CAT_COLLECTION")
60 if client:
61 db = client[db_name]
62 catalogue_collection = db[catalog_name]
63 return catalogue_collection
64 raise Exception("Mongo Connection Fails.")
65
67 """
68 Connect with snapshots collection
69
70 Args:
71 None
72 Returns collection (str)
73 """
74 client = self.mongo_conn()
75 db_name = os.environ.get("SNAPSHOTS_DB")
76 collection_name = os.environ.get("SNAPSHOTS")
77 if client:
78 db = client[db_name]
79 collection = db[collection_name]
80 return collection
81 raise Exception("Mongo Connection Fails.")
82
84 """
85 Connect with formats collection
86
87 Args:
88 None
89 Returns collection (str)
90 """
91 client = self.mongo_conn()
92 db_name = os.environ.get("SNAPSHOTS_DB")
93 formats_collection = os.environ.get("FORMATS")
94 if client:
95 db = client[db_name]
96 collection = db[formats_collection]
97 return collection
98 raise Exception("Mongo Connection Fails.")
99
100 def mongo_conn_ftp(self):
101 """
102 Connect with ftp collection
103
104 Args:
105 None
106 Returns collection (str)
107 """
108 client = self.mongo_conn()
109 db_name = os.environ.get("DB")
110 formats_collection = os.environ.get("FTP_ACCESS")
111 if client:
112 db = client[db_name]
113 collection = db[formats_collection]
114 return collection
115 raise Exception("Mongo Connection Fails.")
116
117 def mongo_conn(self):
118 """
119 Connect with the main MongoDB database
120
121 Args:
122 None
123 Returns client (mongo client)
124 """
125 uri = os.environ.get("MONGO_GLIDER")
126 if "=true&w=majority" not in uri:
127 uri+= "=true&w=majority"
128 # print("Este es el URI", uri)
129 try:
130 client = MongoClient(uri,ssl_cert_reqs=ssl.CERT_NONE)
131 except:
132 client = MongoClient(uri)
133 if client:
134 return client
135 raise Exception("Mongo Connection Fails.")
136
137# def connection_per_module()