implement mongodb post and getall todo: get single

This commit is contained in:
Manuel Hude 2020-11-28 20:30:13 +01:00
parent c52f031845
commit 39f0beb87e
7 changed files with 132 additions and 11 deletions

View File

@ -38,6 +38,34 @@ def send_image(identifier, image_path, metadata_payload):
return True
def get_image(identifier):
print("Getting image with identifier "+identifier)
baseurl = "http://127.0.0.1:8000"
get_url = "/image/get?id="+identifier
try:
response = requests.get(baseurl + get_url)
print_response(response)
except os.error:
print("Error sending request")
return True
def get_all():
print("Getting all images")
baseurl = "http://127.0.0.1:8000"
get_url = "/image/get"
try:
response = requests.get(baseurl + get_url)
print_response(response)
except os.error:
print("Error sending request")
return True
metadata = None
index = 0
metadata_folder = "." + os.path.sep
@ -85,6 +113,8 @@ while (command.lower() not in ["exit", "quit", "end"]):
print("imagefolder <path> - selected new image folder")
print("show - opens image next in line in default os program")
print("trigger - next image in line is sent to backend")
print("fetch - gets the next image from database if exists")
print("fetchall - get all images")
print("rapid <#amount> - next <amound> images in line are sent to backend in 2 second intervals")
print("skip [<#amount>] - skips the next <amount> number of images in line or 1 if no <amount> is given")
print("set <#index> - sets image on given <index> as next in line")
@ -157,6 +187,29 @@ while (command.lower() not in ["exit", "quit", "end"]):
send_image(filename[:-4], image_folder + os.path.sep + filename, meta_payload)
index = index + 1
if command.lower() == "fetch":
if metadata is None:
print("No metadata loaded")
continue
if image_folder is None:
print("No image folder selected")
continue
meta_payload = metadata[index]
filename = meta_payload['filename']
get_image(filename[:-4])
index = index + 1
if command.lower() == "fetchall":
if metadata is None:
print("No metadata loaded")
continue
if image_folder is None:
print("No image folder selected")
continue
get_all()
index = index + 1
if command.lower() == "rapid":
if metadata is None:
print("No metadata loaded")

View File

@ -1,3 +1,5 @@
from django.db import models
# Create your models here.

View File

@ -11,12 +11,10 @@ import os
# set the websocket routing module location here
ASGI_APPLICATION = 'app_be.routing.application'
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/
@ -37,7 +35,7 @@ INSTALLED_APPS = [
'django.contrib.messages',
'django.contrib.staticfiles',
'channels',
'rest_framework'
'rest_framework',
]
MIDDLEWARE = [
@ -49,6 +47,7 @@ MIDDLEWARE = [
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
"""
@ -67,7 +66,6 @@ REST_FRAMEWORK = {
)
}
# configuration of jason web token authentication
JWT_AUTH = {
'JWT_VERIFY': True,
@ -117,15 +115,16 @@ TEMPLATES = [
WSGI_APPLICATION = 'app_be.wsgi.application'
# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
AUTH_PASSWORD_VALIDATORS = [
@ -219,4 +218,4 @@ LOGGING = {
# increase the maximum upload size of files
DATA_UPLOAD_MAX_MEMORY_SIZE = 104857600
DROPBOX_OAUTH2_ACCESS_TOKEN = 'SDt1aqMQg7EAAAAAAAAAARV4CNnOSTjYLc05W2YAxIArG93DnaK9Si9VbwE-aBbQ'
DROPBOX_OAUTH2_ACCESS_TOKEN = 'SDt1aqMQg7EAAAAAAAAAARV4CNnOSTjYLc05W2YAxIArG93DnaK9Si9VbwE-aBbQ'

View File

@ -24,7 +24,7 @@ urlpatterns = [
path('admin/', admin.site.urls),
url(r'^test/', TestApiClass.test_api),
url(r'^image/get$', ImageEndpoint.image_api_get_all),
url(r'^image/get/(?P<id>[0-9]+)$', ImageEndpoint.image_api_get_single),
url(r'^image/get(?P<id>[-|a-zA-Z0-9]+)$', ImageEndpoint.image_api_get_single),
url(r'^image/post$', ImageEndpoint.image_api_post)
]

View File

@ -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)

View File

@ -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?

View File

@ -18,7 +18,8 @@ setup(
'channels_redis==3.2.0',
'whitenoise==5.2.0',
'Pillow==8.0.1',
'dropbox==10.10.0'
'dropbox==10.10.0',
'pymongo=3.11.1',
],
license='BSD License', # example license
description='DESCRIPTION'