Spaces:
Sleeping
Sleeping
finish the idea of micro learning
Browse files- data/content.py +28 -0
data/content.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# In data/content.py
|
| 2 |
+
import firebase_admin
|
| 3 |
+
from firebase_admin import credentials
|
| 4 |
+
from firebase_admin import firestore
|
| 5 |
+
|
| 6 |
+
class ContentManager:
|
| 7 |
+
def __init__(self):
|
| 8 |
+
# Initialize Firebase
|
| 9 |
+
if not firebase_admin._apps:
|
| 10 |
+
cred = credentials.Certificate("path/to/serviceAccountKey.json")
|
| 11 |
+
firebase_admin.initialize_app(cred)
|
| 12 |
+
self.db = firestore.client()
|
| 13 |
+
self.collection = self.db.collection('content')
|
| 14 |
+
|
| 15 |
+
def get_by_id(self, content_id):
|
| 16 |
+
"""Retrieve content by ID"""
|
| 17 |
+
doc = self.collection.document(content_id).get()
|
| 18 |
+
return doc.to_dict() if doc.exists else None
|
| 19 |
+
|
| 20 |
+
def save_content(self, content_data):
|
| 21 |
+
"""Save new content"""
|
| 22 |
+
doc_ref = self.collection.document()
|
| 23 |
+
doc_ref.set(content_data)
|
| 24 |
+
return doc_ref.id
|
| 25 |
+
|
| 26 |
+
def update_content(self, content_id, updates):
|
| 27 |
+
"""Update existing content"""
|
| 28 |
+
self.collection.document(content_id).update(updates)
|