Repository navigation
Expand file tree
/
Copy pathgallery.py
More file actions
271 lines (236 loc) · 10.1 KB
/
Copy pathgallery.py
File metadata and controls
271 lines (236 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
from dataclasses import dataclass, field
from typing import Optional, List, Tuple
from datetime import datetime
import uuid
import json
import bcrypt
from db_connection import MongoDBConnection, MongoDBPermissions, mongodb_permissions
# Define a module-level constant for the collection name.
GALLERY_COLLECTION = "galleries"
@dataclass
class Gallery:
creation_time: datetime
expiration_time: datetime
images: List[str] = field(default_factory=list)
pin_hash: Optional[str] = None
pin_salt: Optional[str] = None
_id: str = field(default_factory=lambda: f"GAL-{uuid.uuid4()}")
# Collection name for MongoDB
COLLECTION_NAME: str = GALLERY_COLLECTION
@property
def id(self) -> str:
return self._id
def to_dict(self) -> dict:
"""Convert the object to a dictionary."""
return {
"_id": self._id, # MongoDB expects the primary key field to be '_id'
"creation_time": self.creation_time,
"expiration_time": self.expiration_time,
"images": self.images,
"pin_hash": self.pin_hash,
"pin_salt": self.pin_salt
}
def __str__(self) -> str:
"""Return a JSON representation of the object."""
return json.dumps({
"id": self._id,
"creation_time": self.creation_time.strftime("%Y-%m-%d %H:%M:%S"),
"expiration_time": self.expiration_time.strftime("%Y-%m-%d %H:%M:%S"),
"images": self.images,
"pin_hash": self.pin_hash,
"pin_salt": self.pin_salt
}, indent=4)
def __repr__(self) -> str:
return self.__str__()
def __eq__(self, o: object) -> bool:
if isinstance(o, Gallery):
return self.id == o.id
return False
def __hash__(self) -> int:
return hash(self.id)
@classmethod
@mongodb_permissions(collection=GALLERY_COLLECTION, actions=[MongoDBPermissions.CREATE_COLLECTION], roles=["boss"])
def db_create_collection(cls, db_c: MongoDBConnection) -> None:
"""
Create the MongoDB collection for galleries with validation.
"""
schema = {
"validator": {
"$jsonSchema": {
"bsonType": "object",
"required": ["_id", "creation_time", "expiration_time", "images", "pin_hash", "pin_salt"],
"properties": {
"_id": {
"bsonType": "string",
"description": "Unique identifier for the gallery, required and acts as primary key"
},
"creation_time": {
"bsonType": "date",
"description": "The time the gallery was created"
},
"expiration_time": {
"bsonType": "date",
"description": "The time the gallery will be deleted"
},
"images": {
"bsonType": "array",
"description": "List of image IDs in the gallery",
"items": {
"bsonType": "string"
}
},
"pin_hash": {
"bsonType": ["string", "null"],
"description": "Hash of the PIN for the gallery"
},
"pin_salt": {
"bsonType": ["string", "null"],
"description": "Salt used to hash the PIN"
}
}
}
},
"validationLevel": "strict",
"validationAction": "error"
}
# Get existing collections
collections = db_c.db.list_collection_names()
if cls.COLLECTION_NAME in collections:
# skip
return
db_c.db.create_collection(
name=cls.COLLECTION_NAME,
validator=schema["validator"],
validationLevel=schema["validationLevel"],
validationAction=schema["validationAction"]
)
@staticmethod
def hash_pin(password: str, salt: Optional[str] = None) -> Tuple[str, str]:
"""
Hash the given password using bcrypt. Generates a new salt if not provided.
Returns a tuple of (hashed_password, salt).
"""
if salt is None:
salt_bytes = bcrypt.gensalt()
salt = salt_bytes.decode()
hashed = bcrypt.hashpw(password.encode(), salt.encode()).decode()
return hashed, salt
def validate_pin(self, pin: str) -> bool:
"""
Validate the given PIN against the stored hash and salt.
"""
pin_to_check, _ = self.hash_pin(pin, self.pin_salt)
return self.pin_hash == pin_to_check
@classmethod
@mongodb_permissions(collection=GALLERY_COLLECTION, actions=[MongoDBPermissions.DROP_COLLECTION], roles=["boss"])
def db_drop_collection(cls, db_c: MongoDBConnection) -> None:
"""
Drop the MongoDB collection for galleries.
"""
db_c.db.drop_collection(cls.COLLECTION_NAME)
@mongodb_permissions(collection=GALLERY_COLLECTION, actions=[MongoDBPermissions.INSERT], roles=["boss", "photo_booth"])
def db_save(self, db_c: MongoDBConnection) -> None:
"""
Save the Gallery object to MongoDB.
"""
collection = db_c.db[self.COLLECTION_NAME]
data = self.to_dict()
collection.insert_one(data)
@classmethod
def _db_load(cls, data: dict) -> 'Gallery':
"""
Load a Gallery object from a dictionary (as retrieved from MongoDB).
Ensures that 'creation_time' and 'expiration_time' are converted to datetime.
"""
creation_time_raw = data.get("creation_time")
expiration_time_raw = data.get("expiration_time")
# Ensure proper datetime conversion
creation_time = (
datetime.fromisoformat(creation_time_raw)
if isinstance(creation_time_raw, str)
else creation_time_raw
) or datetime.now() # Default to now if None
expiration_time = (
datetime.fromisoformat(expiration_time_raw)
if isinstance(expiration_time_raw, str)
else expiration_time_raw
) or datetime.now() # Default to now if None
pin_hash = data.get("pin_hash")
pin_salt = data.get("pin_salt")
_id = str(data.get("_id")) if data.get("_id") is not None else f"GAL-{uuid.uuid4()}"
return Gallery(
creation_time=creation_time,
expiration_time=expiration_time,
images=data.get("images", []),
_id=_id,
pin_hash=pin_hash,
pin_salt=pin_salt
)
@classmethod
@mongodb_permissions(collection=GALLERY_COLLECTION, actions=[MongoDBPermissions.FIND], roles=["boss", "photo_booth", "img_viewer"])
def db_find(cls, db_c: MongoDBConnection, _id: str) -> Optional['Gallery']:
"""
Find a Gallery object in the database by _id.
Returns a Gallery instance if found, else None.
"""
collection = db_c.db[cls.COLLECTION_NAME]
data = collection.find_one({"_id": _id})
if data:
return cls._db_load(data)
return None
@mongodb_permissions(collection=GALLERY_COLLECTION, actions=[MongoDBPermissions.UPDATE], roles=["boss"])
def db_update(self, db_c: MongoDBConnection) -> None:
"""
Update the Gallery object in the database.
"""
collection = db_c.db[self.COLLECTION_NAME]
data = self.to_dict()
collection.update_one({"_id": self._id}, {"$set": data})
@mongodb_permissions(collection=GALLERY_COLLECTION, actions=[MongoDBPermissions.UPDATE], roles=["boss", "photo_booth"])
def db_set_pin(self, db_c: MongoDBConnection, pin: Optional[str]) -> None:
"""
Set a PIN for the gallery. Hashes the PIN and stores the hash and salt.
"""
pin_hash = None
pin_salt = None
if pin is None:
self.pin_hash = None
self.pin_salt = None
else:
pin_hash, pin_salt = self.hash_pin(pin)
self.pin_hash = pin_hash
self.pin_salt = pin_salt
collection = db_c.db[self.COLLECTION_NAME]
collection.update_one({"_id": self._id}, {"$set": {"pin_hash": pin_hash, "pin_salt": pin_salt}})
@mongodb_permissions(collection=GALLERY_COLLECTION, actions=[MongoDBPermissions.UPDATE], roles=["boss", "photo_booth"])
def db_add_image(self, db_c: MongoDBConnection, img_id: str) -> None:
"""
Add an image to the gallery.
"""
self.images.append(img_id)
collection = db_c.db[self.COLLECTION_NAME]
collection.update_one({"_id": self._id}, {"$set": {"images": self.images}})
@mongodb_permissions(collection=GALLERY_COLLECTION, actions=[MongoDBPermissions.UPDATE], roles=["boss"])
def db_remove_image(self, db_c: MongoDBConnection, img_id: str) -> None:
"""
Remove an image from the gallery.
"""
self.images.remove(img_id)
collection = db_c.db[self.COLLECTION_NAME]
collection.update_one({"_id": self._id}, {"$set": {"images": self.images}})
@mongodb_permissions(collection=GALLERY_COLLECTION, actions=[MongoDBPermissions.REMOVE], roles=["boss", "old_img_eraser", "img_viewer"])
def db_delete(self, db_c: MongoDBConnection) -> None:
"""
Delete the Gallery object from the database.
"""
collection = db_c.db[self.COLLECTION_NAME]
collection.delete_one({"_id": self._id})
@classmethod
@mongodb_permissions(collection=GALLERY_COLLECTION, actions=[MongoDBPermissions.FIND], roles=["boss", "img_viewer", "old_img_eraser"])
def db_find_all(cls, db_c: MongoDBConnection) -> List['Gallery']:
"""
Retrieve all Gallery objects from the database.
"""
collection = db_c.db[cls.COLLECTION_NAME]
docs = collection.find()
return [cls._db_load(doc) for doc in docs]