|
@@ -26,16 +26,19 @@ class TimeAlbumView(View):
|
|
|
|
|
|
def validation(self, request_dict, request, operation):
|
|
def validation(self, request_dict, request, operation):
|
|
token_code, user_id, response = CommonService.verify_token_get_user_id(request_dict, request)
|
|
token_code, user_id, response = CommonService.verify_token_get_user_id(request_dict, request)
|
|
- if token_code != 0:
|
|
|
|
- return response.json(token_code)
|
|
|
|
- if operation == 'getAlbumPic': # 查询相册列表
|
|
|
|
- return self.get_album_pic(user_id, request_dict, response)
|
|
|
|
- elif operation == 'getTimeDiary': # 获取时光日记列表
|
|
|
|
- return self.get_time_diary(user_id, request_dict, response)
|
|
|
|
- elif operation == 'picLiked': # 喜欢图片
|
|
|
|
- return self.pic_liked(user_id, request_dict, response)
|
|
|
|
|
|
+ if operation == 'clearAlbumMedia': # 删除相册媒体
|
|
|
|
+ return self.clear_album_media(response)
|
|
else:
|
|
else:
|
|
- return response.json(414)
|
|
|
|
|
|
+ if token_code != 0:
|
|
|
|
+ return response.json(token_code)
|
|
|
|
+ if operation == 'getAlbumPic': # 查询相册列表
|
|
|
|
+ return self.get_album_pic(user_id, request_dict, response)
|
|
|
|
+ elif operation == 'getTimeDiary': # 获取时光日记列表
|
|
|
|
+ return self.get_time_diary(user_id, request_dict, response)
|
|
|
|
+ elif operation == 'picLiked': # 喜欢图片
|
|
|
|
+ return self.pic_liked(user_id, request_dict, response)
|
|
|
|
+ else:
|
|
|
|
+ return response.json(414)
|
|
|
|
|
|
def get_album_pic(self, user_id, request_dict, response):
|
|
def get_album_pic(self, user_id, request_dict, response):
|
|
device_id = request_dict.get('deviceId', None)
|
|
device_id = request_dict.get('deviceId', None)
|
|
@@ -197,7 +200,7 @@ class TimeAlbumView(View):
|
|
'updated_time': int(time.time())
|
|
'updated_time': int(time.time())
|
|
}
|
|
}
|
|
)
|
|
)
|
|
- time_diary_qs.update(status=2)
|
|
|
|
|
|
+ time_diary_qs.update(status=2, updated_time=int(time.time()))
|
|
|
|
|
|
elif liked_status == 0:
|
|
elif liked_status == 0:
|
|
# 从中间表移除
|
|
# 从中间表移除
|
|
@@ -205,7 +208,7 @@ class TimeAlbumView(View):
|
|
if DiaryAlbumMediaRelation.objects.filter(diary=time_diary).count() == 0:
|
|
if DiaryAlbumMediaRelation.objects.filter(diary=time_diary).count() == 0:
|
|
time_diary.delete()
|
|
time_diary.delete()
|
|
if not DiaryAlbumMediaRelation.objects.filter(album_media=album_media).exists():
|
|
if not DiaryAlbumMediaRelation.objects.filter(album_media=album_media).exists():
|
|
- time_diary_qs.update(status=1)
|
|
|
|
|
|
+ time_diary_qs.update(status=1, updated_time=int(time.time()))
|
|
|
|
|
|
return response.json(0)
|
|
return response.json(0)
|
|
|
|
|
|
@@ -243,3 +246,92 @@ class TimeAlbumView(View):
|
|
else:
|
|
else:
|
|
media_url = ''
|
|
media_url = ''
|
|
return media_url
|
|
return media_url
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ @staticmethod
|
|
|
|
+ def clear_album_media(response):
|
|
|
|
+ # 获取当前时间
|
|
|
|
+ current_time = int(time.time()) - 2592000
|
|
|
|
+
|
|
|
|
+ # 预加载关联的TimeAlbum对象
|
|
|
|
+ album_media_qs = AlbumMedia.objects.filter(
|
|
|
|
+ status=1,
|
|
|
|
+ event_time__lt=current_time
|
|
|
|
+ )
|
|
|
|
+
|
|
|
|
+ # 如果没有需要处理的数据,直接返回
|
|
|
|
+ if not album_media_qs.exists():
|
|
|
|
+ return response.json(0)
|
|
|
|
+
|
|
|
|
+ # 初始化 Huawei Cloud 的 ObsClient
|
|
|
|
+ obs_client = ObsClient(
|
|
|
|
+ access_key_id=HUAWEICLOUD_AK,
|
|
|
|
+ secret_access_key=HUAWEICLOUD_SK,
|
|
|
|
+ server=HUAWEICLOUD_OBS_SERVER
|
|
|
|
+ )
|
|
|
|
+
|
|
|
|
+ # 根据配置选择初始化 AWS 客户端
|
|
|
|
+ aws_client_config = {
|
|
|
|
+ 'aws_access_key_id': AWS_ACCESS_KEY_ID[0] if CONFIG_INFO in [CONFIG_CN, CONFIG_TEST] else AWS_ACCESS_KEY_ID[1],
|
|
|
|
+ 'aws_secret_access_key': AWS_SECRET_ACCESS_KEY[0] if CONFIG_INFO in [CONFIG_CN, CONFIG_TEST] else AWS_SECRET_ACCESS_KEY[1],
|
|
|
|
+ 'region_name': 'cn-northwest-1' if CONFIG_INFO in [CONFIG_CN, CONFIG_TEST] else 'us-east-1'
|
|
|
|
+ }
|
|
|
|
+ aws_client = boto3.client('s3', **aws_client_config)
|
|
|
|
+
|
|
|
|
+ # 准备批量删除的对象
|
|
|
|
+ aws_objects_to_delete = []
|
|
|
|
+ obs_objects_to_delete = []
|
|
|
|
+ time_album_ids_to_check = set()
|
|
|
|
+ album_media_ids_to_delete = []
|
|
|
|
+
|
|
|
|
+ # 收集需要删除的对象和时间相册ID
|
|
|
|
+ for album_media in album_media_qs:
|
|
|
|
+ uid = album_media.device_id
|
|
|
|
+ event_time = album_media.event_time
|
|
|
|
+ obj_name = f'roomumy/albumMedia/{uid}/{album_media.channel}/{event_time}.jpeg'
|
|
|
|
+
|
|
|
|
+ album_media_ids_to_delete.append(album_media.id)
|
|
|
|
+
|
|
|
|
+ if album_media.time_album_id:
|
|
|
|
+ time_album_ids_to_check.add(album_media.time_album_id)
|
|
|
|
+
|
|
|
|
+ if album_media.storage_location == 2:
|
|
|
|
+ aws_objects_to_delete.append({'Key': obj_name})
|
|
|
|
+ elif album_media.storage_location == 5:
|
|
|
|
+ obs_objects_to_delete.append(obj_name)
|
|
|
|
+
|
|
|
|
+ # 批量删除AWS对象
|
|
|
|
+ if aws_objects_to_delete:
|
|
|
|
+ # AWS S3支持批量删除,最多1000个对象每次
|
|
|
|
+ for i in range(0, len(aws_objects_to_delete), 1000):
|
|
|
|
+ batch = aws_objects_to_delete[i:i + 1000]
|
|
|
|
+ aws_client.delete_objects(
|
|
|
|
+ Bucket='ansjerfilemanager',
|
|
|
|
+ Delete={'Objects': batch}
|
|
|
|
+ )
|
|
|
|
+
|
|
|
|
+ # 删除华为OBS对象
|
|
|
|
+ if obs_objects_to_delete:
|
|
|
|
+ for obj_name in obs_objects_to_delete:
|
|
|
|
+ obs_client.deleteObject(bucketName="asj-app", objectKey=obj_name)
|
|
|
|
+
|
|
|
|
+ # 检查需要删除的TimeAlbum
|
|
|
|
+ if time_album_ids_to_check:
|
|
|
|
+ # 查询TimeAlbum关联的AlbumMedia
|
|
|
|
+ time_albums_with_media = AlbumMedia.objects.filter(
|
|
|
|
+ time_album_id__in=time_album_ids_to_check
|
|
|
|
+ ).exclude(
|
|
|
|
+ id__in=album_media_ids_to_delete
|
|
|
|
+ ).values_list('time_album_id', flat=True).distinct()
|
|
|
|
+
|
|
|
|
+ # 找出没有关联媒体的TimeAlbum
|
|
|
|
+ time_albums_to_delete = set(time_album_ids_to_check) - set(time_albums_with_media)
|
|
|
|
+
|
|
|
|
+ # 批量删除TimeAlbum
|
|
|
|
+ if time_albums_to_delete:
|
|
|
|
+ TimeAlbum.objects.filter(id__in=time_albums_to_delete).delete()
|
|
|
|
+
|
|
|
|
+ # 处理完所有项后,删除数据库中的记录
|
|
|
|
+ album_media_qs.delete()
|
|
|
|
+
|
|
|
|
+ return response.json(0)
|