def post(self, request: WSGIRequest, albumId: str = None): """ 新增photo時 根據_id去新增相簿的photo 會檢查photo是否獨一無二 Args: request: 裡面需要有_id和photoId Returns: 更改過後的photo """ # album_id = request.data["_id"] album_photo = request.data["albumPhoto"] photoId = {'photoId': album_photo, 'isDeleted': False} try: update_rows = Album.objects(_id=albumId).update( add_to_set__albumPhoto=photoId) print(f'Album/View: AlbumPhotoView.post, db:{update_rows} rows') except Exception as e: print('error: ', e) return Response(simpleMessage("Post/AlbumPhotoView: error"), status=status.HTTP_500_INTERNAL_SERVER_ERROR) return Response(simpleMessage('Post/AlbumPhotoView'), status=status.HTTP_201_CREATED)
def delete(self, request: WSGIRequest, albumId: str = None): """ 刪除tag 根據_id和albumTag刪掉指定的albumTag tag不存在或是成功刪除都會還傳成功 Args: request: 裡面需要有_id和albumTag Returns: 剩下的tag """ album_tag = request.query_params["albumTag"] try: result = Album.objects( _id=albumId, albumTag__match={ 'tag': album_tag, 'isDeleted': False }).update_one(set__albumTag__S__isDeleted=True) print(result) except Exception as e: print(e) return Response(simpleMessage("DELETE/AlbumTagView: error"), status=status.HTTP_500_INTERNAL_SERVER_ERROR) return Response(simpleMessage('DELETE/AlbumTagView'), status=status.HTTP_201_CREATED)
def post(self, request: WSGIRequest, albumId: str = None): """ 新增tag時 根據_id去新增相簿的tag 會檢查tag是否獨一無二 Args: request: 裡面需要有_id和albumTag Returns: 更改過後的tag """ # album_id = request.data["_id"] album_tag = request.data["albumTag"] tag = {'tag': album_tag, 'isDeleted': False} try: update_rows = Album.objects(_id=albumId).update( add_to_set__albumTag=tag) print(f'Album/View: TagView.post, db:{update_rows} rows') except Exception as e: print('error: ', e) return Response(simpleMessage("Post/AlbumTagView: error"), status=status.HTTP_500_INTERNAL_SERVER_ERROR) return Response(simpleMessage('Post/AlbumTagView'), status=status.HTTP_201_CREATED)
def delete(self, request, photoId=None): """ 單張照片,編輯頁面,刪除tag 根據photoId和custom_tag刪掉指定的custom_tag tag不存在或是成功刪除都會還傳成功 Args: request: 裡面需要有photoId和custom_tag Returns: 剩下的tag """ # lancode = request.headers.get('Language-Code',None) if photoId: try: custom_tag = request.query_params.get("custom_tag", None) photo = Photo.objects(photoId=photoId, tag__custom_tag__match={'tag': custom_tag, 'is_deleted': False}).first() photos = photo.tag.custom_tag for single_tag in photos: if single_tag.tag == custom_tag: single_tag.is_deleted = True photo.save() loconto = get_location(custom_tag) if loconto: for i in loconto: Photo.objects(photoId=photoId).update(pull_all__tag__cust_location_onto=loconto) return Response({}, status=status.HTTP_200_OK) except Exception as e: print(e) return Response(simpleMessage("DELETE/TagView: error"), status=status.HTTP_500_INTERNAL_SERVER_ERROR) else: return Response({'message': 'No such photo'}, status=status.HTTP_400_BAD_REQUEST)
def put(self, request, photoId=None): """ 單張照片,編輯頁面,新增tag時 根據photoId去新增照片的客製化tag 會檢查tag是否獨一無二 Args: request: 裡面需要有photoId和custom_tag Returns: 更改過後的tag """ lancode = request.headers.get('Language-Code',None) if photoId: custom_tag = request.data["customTag"] custom_tag = custom_tag.strip() tag = Custom_tag(tag=custom_tag) try: update_rows = Photo.objects(photoId__exact=photoId).update(add_to_set__tag__custom_tag=tag) p = Photo.objects(photoId__exact=photoId).get() loconto = get_location(tag) if loconto: for i in loconto: p.cust_location_onto.append(i) p.save() return Response({}, status=status.HTTP_200_OK) except Exception as e: print('error: ', e) return Response(simpleMessage("Put/TagViewError"), status=status.HTTP_500_INTERNAL_SERVER_ERROR) else: return Response({'message': 'No such photo'}, status=status.HTTP_400_BAD_REQUEST)
def get(self, request, photoId): """ 取得相片的custom_tag 根據photoId去更改資料庫的emotion欄位 要過濾掉is_delete=true Args: request: photoId Returns: 該photo全部的custom_tag """ lancode = request.headers.get('Language-Code',None) if photoId: try: photo = Photo.objects(photoId=photoId).get() array_field = photo.tag.custom_tag custom_tag_array = [] for single_tag in array_field: if single_tag.is_deleted == False: custom_tag_array.append(single_tag.tag) return Response({"custom_tag": custom_tag_array}, status=status.HTTP_200_OK) except Exception as e: print(e) return Response(simpleMessage("GetTagViewError"), status=status.HTTP_500_INTERNAL_SERVER_ERROR) else: return Response({'message': 'No such photo'}, status=status.HTTP_400_BAD_REQUEST)
def post(self, request: WSGIRequest, userId: str = None): album = Album(coverPhotoId=request.data['coverPhotoId'], albumName=request.data['albumName'], userId=userId) for p in request.data['albumPhoto']: album.albumPhoto.append(AlbumPhoto(photoId=p)) for t in request.data['albumTag']: album.albumTag.append(AlbumTag(tag=t)) album.save() return Response(simpleMessage('POST/AlbumView'), status=status.HTTP_201_CREATED)
def get(self, request: WSGIRequest, albumId: str = None): """ 取得相簿的albumTag 根據_id去更改資料庫的albumTag欄位 要過濾掉is_delete=true Args: request: _id Returns: 該album全部的albumTag """ # album_id = request.data["_id"] # album_id = request.query_params["_id"] album_tag_array = [] try: album = Album.objects(_id=albumId).get() array_field = album.albumTag if len(array_field) == 0: return Response(simpleMessage("zero"), status=status.HTTP_200_OK) for single_tag in array_field: if single_tag.isDeleted == False: album_tag_array.append(single_tag.tag) except Exception as e: print(e) return Response(simpleMessage("Get/AlbumTagView: error"), status=status.HTTP_500_INTERNAL_SERVER_ERROR) res = {"album_tag": album_tag_array} # response_str = json.dumps({"result": "Get/AlbumTagView", # "album_tag": album_tag_array}) return Response(res, status=status.HTTP_201_CREATED)
def delete(self, request: WSGIRequest, albumId: str = None): """ 刪除相簿中的相片 根據_id和photoId刪掉指定的相片 photo不存在或是成功刪除都會還傳成功 Args: request: 裡面需要有_id和photoId Returns: 相簿剩下的photo """ # album_id = request.query_params["_id"] album_photo = request.data["albumPhoto"] try: album = Album.objects(_id=albumId, albumPhoto__match={ 'photoId': album_photo, 'isDeleted': False }).first() for single_photo in album.albumPhoto: if single_photo.photoId == album_photo: single_photo.isDeleted = True album.save() except Exception as e: print(e) return Response(simpleMessage("DELETE/AlbumPhotoView: error"), status=status.HTTP_500_INTERNAL_SERVER_ERROR) return Response(simpleMessage('DELETE/AlbumPhotoView'), status=status.HTTP_200_OK)
def put(self, request: WSGIRequest, albumId: str = None): # _id = request.data["_id"] albumName = request.data["albumName"] try: update_rows = Album.objects(_id=albumId).update( albumName=albumName) print(f'Album/View: AlbumView.put, db:{update_rows} rows') except Exception as e: print(e) return Response("AlbumViewError", status=status.HTTP_500_INTERNAL_SERVER_ERROR) return Response(simpleMessage('PUT/AlbumView'), status=status.HTTP_200_OK)
def delete(self, request: WSGIRequest, albumId: str = None): """ 刪除相簿 把_id的is_delete欄位改成true Args: request: 裡面需要有_id Returns: None """ # _id = request.data["_id"] try: album = Album.objects(_id=albumId).get() album.isDeleted = True for tag in album.albumTag: tag.isDeleted = True album.save() print("Album Deleted") except Exception as e: print(e) return Response("AlbumViewError", status=status.HTTP_500_INTERNAL_SERVER_ERROR) return Response(simpleMessage('DELETE/AlbumView'), status=status.HTTP_200_OK)