implement mongodb post and getall todo: get single
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
import pymongo
|
||||
|
||||
class MongoManager:
|
||||
__instance = None
|
||||
|
||||
@staticmethod
|
||||
def getInstance():
|
||||
if MongoManager.__instance == None:
|
||||
MongoManager()
|
||||
return MongoManager.__instance
|
||||
|
||||
def __init__(self):
|
||||
if MongoManager.__instance != None:
|
||||
raise Exception("This class is a singleton!")
|
||||
else:
|
||||
MongoManager.__instance = pymongo.MongoClient('127.0.0.1', 27017)
|
||||
db = MongoManager.__instance.AIC
|
||||
coll = db.metadata
|
||||
coll.create_index("filename",unique=True)
|
||||
@@ -2,6 +2,9 @@ import logging
|
||||
import json
|
||||
import base64
|
||||
import dropbox
|
||||
import pymongo
|
||||
|
||||
from app_be.views.mongo_db import MongoManager
|
||||
|
||||
from django.http import JsonResponse
|
||||
from django.http import HttpRequest
|
||||
@@ -25,12 +28,43 @@ class ImageEndpoint:
|
||||
@api_view(['GET'])
|
||||
def image_api_get_all(request):
|
||||
logger.debug('Image GET all call: {}'.format(request))
|
||||
|
||||
instance = MongoManager.getInstance()
|
||||
|
||||
db = instance.AIC
|
||||
col = db.metadata
|
||||
|
||||
print("hier")
|
||||
|
||||
|
||||
try:
|
||||
for resp in col.find():
|
||||
print(resp)
|
||||
|
||||
except:
|
||||
print("Could not find Metadata")
|
||||
|
||||
return JsonResponse({'Result': 'success1'}, safe=False)
|
||||
|
||||
|
||||
@staticmethod
|
||||
@api_view(['GET'])
|
||||
def image_api_get_single(request, id):
|
||||
logger.debug('Image GET single call: {}'.format(request))
|
||||
|
||||
print("im single")
|
||||
|
||||
instance = MongoManager.getInstance()
|
||||
|
||||
db = instance.AIC
|
||||
col = db.metadata
|
||||
|
||||
try:
|
||||
resp = col.find({"filename": id+".jpg"})
|
||||
print(resp[0]);
|
||||
except:
|
||||
print("Could not find Metadata")
|
||||
|
||||
return JsonResponse({'Result': 'success1', 'id': id}, safe=False)
|
||||
|
||||
@staticmethod
|
||||
@@ -39,9 +73,22 @@ class ImageEndpoint:
|
||||
logger.debug('Image POST call: {}'.format(request))
|
||||
payload = json.loads(request.body.decode('utf-8'))
|
||||
b64encoded_image = payload['image_data']
|
||||
filename = payload['filename']
|
||||
filename = payload['id']
|
||||
metadata = payload['metadata']
|
||||
|
||||
instance = MongoManager.getInstance()
|
||||
|
||||
db = instance.AIC
|
||||
col = db.metadata
|
||||
|
||||
|
||||
try:
|
||||
resp = col.insert_one(metadata)
|
||||
except:
|
||||
print("Could not insert Metadata")
|
||||
|
||||
decoded_image = base64.b64decode(b64encoded_image)
|
||||
dbx = dropbox.Dropbox(settings.DROPBOX_OAUTH2_ACCESS_TOKEN)
|
||||
dbx.files_upload(decoded_image,'/Apps/AIC Federated Storage Infrastructure/' + filename)
|
||||
return JsonResponse({'Result': 'success2', 'received file': filename, 'metadata': metadata}, safe=False)
|
||||
dbx.files_upload(decoded_image, '/Apps/AIC Federated Storage Infrastructure/' + filename)
|
||||
return JsonResponse({'Result': 'success2', 'received file': filename},
|
||||
safe=False) # 'metadata': metadata response beinhaltet ObjectId welche nicht serializable is?
|
||||
|
||||
Reference in New Issue
Block a user