|
@@ -0,0 +1,260 @@
|
|
|
|
+import time
|
|
|
|
+
|
|
|
|
+import oss2
|
|
|
|
+import boto3
|
|
|
|
+import botocore.client
|
|
|
|
+
|
|
|
|
+from django.core.paginator import Paginator
|
|
|
|
+from django.db.models import Value
|
|
|
|
+from django.db.models.functions import Coalesce
|
|
|
|
+from django.views import View
|
|
|
|
+from obs import ObsClient
|
|
|
|
+from Ansjer.pushconfig import OSS_STS_ACCESS_SECRET, OSS_STS_ACCESS_KEY
|
|
|
|
+from Ansjer.config import HUAWEICLOUD_AK, HUAWEICLOUD_SK, HUAWEICLOUD_OBS_SERVER, HUAWEICLOUD_PUSH_BUKET, \
|
|
|
|
+ AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, SERVER_TYPE, CONFIG_EUR, CONFIG_US
|
|
|
|
+from Object.RedisObject import RedisObject
|
|
|
|
+
|
|
|
|
+from Object.ResponseObject import ResponseObject
|
|
|
|
+from Object.OCIObjectStorage import OCIObjectStorage
|
|
|
|
+
|
|
|
|
+from Roomumy.models import TimeAlbum, AlbumMedia, TimeDiary
|
|
|
|
+from Service.CommonService import CommonService
|
|
|
|
+from Controller.DetectControllerV2 import DetectControllerViewV2
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+class TimeAlbumView(View):
|
|
|
|
+ def get(self, request, *args, **kwargs):
|
|
|
|
+ request.encoding = 'utf-8'
|
|
|
|
+ operation = kwargs.get('operation')
|
|
|
|
+ return self.validation(request.GET, request, operation)
|
|
|
|
+
|
|
|
|
+ def post(self, request, *args, **kwargs):
|
|
|
|
+ request.encoding = 'utf-8'
|
|
|
|
+ operation = kwargs.get('operation')
|
|
|
|
+ return self.validation(request.POST, request, operation)
|
|
|
|
+
|
|
|
|
+ def validation(self, request_dict, request, operation):
|
|
|
|
+ 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(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, request_dict, response):
|
|
|
|
+ uid = request_dict.get('uid', None)
|
|
|
|
+ start_time = request_dict.get('startTime', None)
|
|
|
|
+ end_time = request_dict.get('endTime', None)
|
|
|
|
+ page = request_dict.get('page', 1) # 当前页码,默认为1
|
|
|
|
+ page_size = request_dict.get('pageSize', 10) # 每页显示的记录数,默认为10
|
|
|
|
+
|
|
|
|
+ # 检查是否提供了uid,如果没有提供返回错误
|
|
|
|
+ if uid is None:
|
|
|
|
+ return response.json(444)
|
|
|
|
+ try:
|
|
|
|
+ # 查询TimeAlbum表,按uid过滤,且如果提供了album_date,则进一步按album_date过滤
|
|
|
|
+ time_album_qs = TimeAlbum.objects.filter(uid=uid).order_by('-album_date')
|
|
|
|
+
|
|
|
|
+ if start_time and end_time:
|
|
|
|
+ time_album_qs = time_album_qs.filter(album_date__gt=start_time, album_date__lt=end_time)
|
|
|
|
+
|
|
|
|
+ # 如果没有找到符合条件的TimeAlbum记录,返回错误
|
|
|
|
+ if not time_album_qs.exists():
|
|
|
|
+ return response.json(0, {})
|
|
|
|
+
|
|
|
|
+ # 对TimeAlbum结果进行分页
|
|
|
|
+ paginator = Paginator(time_album_qs, page_size)
|
|
|
|
+ time_albums = paginator.page(page) # 获取当前页的数据
|
|
|
|
+
|
|
|
|
+ time_album_list = []
|
|
|
|
+ # 对每个TimeAlbum,查询相关的AlbumMedia
|
|
|
|
+ for time_album in time_albums:
|
|
|
|
+ # 查询与当前time_album_id关联的AlbumMedia记录
|
|
|
|
+ time_album_info = []
|
|
|
|
+ album_media_qs = AlbumMedia.objects.filter(time_album_id=time_album.id).values('id', 'image', 'video',
|
|
|
|
+ 'thumbnail', 'status',
|
|
|
|
+ 'storage_location',
|
|
|
|
+ 'time_diary__liked_status')
|
|
|
|
+
|
|
|
|
+ if album_media_qs.exists():
|
|
|
|
+ for album_media in album_media_qs:
|
|
|
|
+ thumbnail = self.media_url(album_media['storage_location'], album_media['thumbnail'], uid) if \
|
|
|
|
+ album_media['thumbnail'] else ""
|
|
|
|
+ image = self.media_url(album_media['storage_location'], album_media['image'], uid) if \
|
|
|
|
+ album_media['image'] else ""
|
|
|
|
+ video = self.media_url(album_media['storage_location'], album_media['video'], uid) if \
|
|
|
|
+ album_media['video'] else ""
|
|
|
|
+ # 判断是否喜欢图片
|
|
|
|
+ if album_media['status'] == 1:
|
|
|
|
+ liked_status = 0
|
|
|
|
+ else:
|
|
|
|
+ liked_status = album_media['time_diary__liked_status'] if album_media[
|
|
|
|
+ 'time_diary__liked_status'] else 0
|
|
|
|
+ time_album_info.append({
|
|
|
|
+ 'picId': album_media['id'],
|
|
|
|
+ 'image': image,
|
|
|
|
+ 'video': video,
|
|
|
|
+ 'thumbnail': thumbnail,
|
|
|
|
+ 'likedStatus': liked_status,
|
|
|
|
+ })
|
|
|
|
+ time_album_list.append({
|
|
|
|
+ 'albumDate': time_album.album_date,
|
|
|
|
+ 'albumTitle': time_album.album_title,
|
|
|
|
+ 'info': time_album_info
|
|
|
|
+ })
|
|
|
|
+
|
|
|
|
+ # 返回分页结果和数据
|
|
|
|
+ return response.json(0, {
|
|
|
|
+ 'data': time_album_list,
|
|
|
|
+ 'total': paginator.count, # 总记录数
|
|
|
|
+ })
|
|
|
|
+ except Exception as e:
|
|
|
|
+ return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
|
+
|
|
|
|
+ def get_time_diary(self, user_id, request_dict, response):
|
|
|
|
+ start_time = request_dict.get('startTime', None)
|
|
|
|
+ end_time = request_dict.get('endTime', None)
|
|
|
|
+ page = request_dict.get('page', 1) # 当前页码,默认为1
|
|
|
|
+ page_size = request_dict.get('pageSize', 10) # 每页显示的记录数,默认为10
|
|
|
|
+
|
|
|
|
+ try:
|
|
|
|
+ # 查询TimeAlbum表,按uid过滤,且如果提供了album_date,则进一步按album_date过滤
|
|
|
|
+ time_album_qs = TimeAlbum.objects.filter(user_id=user_id).order_by('-album_date')
|
|
|
|
+
|
|
|
|
+ if start_time and end_time:
|
|
|
|
+ time_album_qs = time_album_qs.filter(album_date__gt=start_time, album_date__lt=end_time)
|
|
|
|
+
|
|
|
|
+ # 如果没有找到符合条件的TimeAlbum记录,返回错误
|
|
|
|
+ if not time_album_qs.exists():
|
|
|
|
+ return response.json(0, {})
|
|
|
|
+
|
|
|
|
+ # 对TimeAlbum结果进行分页
|
|
|
|
+ paginator = Paginator(time_album_qs, page_size)
|
|
|
|
+ time_albums = paginator.page(page) # 获取当前页的数据
|
|
|
|
+
|
|
|
|
+ time_album_list = []
|
|
|
|
+ # 对每个TimeAlbum,查询相关的AlbumMedia
|
|
|
|
+ for time_album in time_albums:
|
|
|
|
+ # 查询与当前time_album_id关联的AlbumMedia记录
|
|
|
|
+ time_album_info = []
|
|
|
|
+ album_media_qs = AlbumMedia.objects.filter(time_album_id=time_album.id, time_diary__liked_status=1)
|
|
|
|
+ if album_media_qs.exists():
|
|
|
|
+ uid = time_album.uid
|
|
|
|
+ for album_media in album_media_qs.values('id', 'image', 'video', 'thumbnail', 'status',
|
|
|
|
+ 'storage_location', 'time_diary__liked_status'):
|
|
|
|
+ thumbnail = self.media_url(album_media['storage_location'], album_media['thumbnail'], uid) if \
|
|
|
|
+ album_media['thumbnail'] else ""
|
|
|
|
+ image = self.media_url(album_media['storage_location'], album_media['image'], uid) if \
|
|
|
|
+ album_media['image'] else ""
|
|
|
|
+ video = self.media_url(album_media['storage_location'], album_media['video'], uid) if \
|
|
|
|
+ album_media['video'] else ""
|
|
|
|
+ time_album_info.append({
|
|
|
|
+ 'picId': album_media['id'],
|
|
|
|
+ 'image': image,
|
|
|
|
+ 'video': video,
|
|
|
|
+ 'thumbnail': thumbnail,
|
|
|
|
+ 'likedStatus': album_media['time_diary__liked_status'],
|
|
|
|
+ })
|
|
|
|
+ time_album_list.append({
|
|
|
|
+ 'albumDate': time_album.album_date,
|
|
|
|
+ 'info': time_album_info
|
|
|
|
+ })
|
|
|
|
+
|
|
|
|
+ # 返回分页结果和数据
|
|
|
|
+ return response.json(0, {
|
|
|
|
+ 'data': time_album_list,
|
|
|
|
+ 'total': paginator.count, # 总记录数
|
|
|
|
+ })
|
|
|
|
+ except Exception as e:
|
|
|
|
+ return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
|
+
|
|
|
|
+ @staticmethod
|
|
|
|
+ def pic_liked(user_id, request_dict, response):
|
|
|
|
+ pic_id = request_dict.get('picId', None)
|
|
|
|
+ liked_status = request_dict.get('likedStatus', None)
|
|
|
|
+ if not all([pic_id, liked_status]):
|
|
|
|
+ return response.json(444)
|
|
|
|
+ liked_status = int(liked_status)
|
|
|
|
+ try:
|
|
|
|
+ now_time = int(time.time())
|
|
|
|
+ album_media = AlbumMedia.objects.filter(id=pic_id).first()
|
|
|
|
+ if not album_media:
|
|
|
|
+ return response.json(173)
|
|
|
|
+
|
|
|
|
+ if album_media.time_diary_id:
|
|
|
|
+ time_diary_qs = TimeDiary.objects.filter(id=album_media.time_diary_id)
|
|
|
|
+ if time_diary_qs.exists():
|
|
|
|
+ time_diary_qs.update(liked_status=liked_status)
|
|
|
|
+ else:
|
|
|
|
+ album_media.time_diary = TimeDiary.objects.create(user_id=user_id, liked_status=liked_status)
|
|
|
|
+ else:
|
|
|
|
+ album_media.time_diary = TimeDiary.objects.create(user_id=user_id, liked_status=liked_status)
|
|
|
|
+ # 更新 album_media
|
|
|
|
+ album_media.status = 2
|
|
|
|
+ album_media.updated_time = now_time
|
|
|
|
+ album_media.save()
|
|
|
|
+ return response.json(0)
|
|
|
|
+ except Exception as e:
|
|
|
|
+ return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
|
+
|
|
|
|
+ @staticmethod
|
|
|
|
+ def media_url(storage_location, obj_name, uid=""):
|
|
|
|
+ if storage_location == 1: # 阿里云oss
|
|
|
|
+ auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
|
|
|
|
+ oss_img_bucket = oss2.Bucket(auth, 'oss-cn-shenzhen.aliyuncs.com', 'apg')
|
|
|
|
+ media_url = oss_img_bucket.sign_url('GET', obj_name, 300)
|
|
|
|
+
|
|
|
|
+ elif storage_location == 2:
|
|
|
|
+ if SERVER_TYPE == 'Ansjer.cn_config.formal_settings' or SERVER_TYPE == 'Ansjer.cn_config.test_settings':
|
|
|
|
+ region = 2
|
|
|
|
+ else:
|
|
|
|
+ region = 1
|
|
|
|
+ params = {'Key': obj_name}
|
|
|
|
+ if region == 1: # 国外AWS
|
|
|
|
+ aws_s3 = boto3.client(
|
|
|
|
+ 's3',
|
|
|
|
+ aws_access_key_id=AWS_ACCESS_KEY_ID[1],
|
|
|
|
+ aws_secret_access_key=AWS_SECRET_ACCESS_KEY[1],
|
|
|
|
+ config=botocore.client.Config(signature_version='s3v4'),
|
|
|
|
+ region_name='us-east-1'
|
|
|
|
+ )
|
|
|
|
+ params['Bucket'] = 'foreignpush'
|
|
|
|
+ media_url = aws_s3.generate_presigned_url(
|
|
|
|
+ 'get_object', Params=params, ExpiresIn=300)
|
|
|
|
+ else: # 国内AWS
|
|
|
|
+ aws_s3_cn = boto3.client(
|
|
|
|
+ 's3',
|
|
|
|
+ aws_access_key_id=AWS_ACCESS_KEY_ID[0],
|
|
|
|
+ aws_secret_access_key=AWS_SECRET_ACCESS_KEY[0],
|
|
|
|
+ config=botocore.client.Config(signature_version='s3v4'),
|
|
|
|
+ region_name='cn-northwest-1'
|
|
|
|
+ )
|
|
|
|
+ params['Bucket'] = 'push'
|
|
|
|
+ media_url = aws_s3_cn.generate_presigned_url(
|
|
|
|
+ 'get_object', Params=params, ExpiresIn=300)
|
|
|
|
+
|
|
|
|
+ elif storage_location in [3, 4]: # 国外OCI云
|
|
|
|
+ prefix_name = obj_name
|
|
|
|
+ oci_eur = OCIObjectStorage(CONFIG_EUR)
|
|
|
|
+ oci_us = OCIObjectStorage(CONFIG_US)
|
|
|
|
+ oci = oci_eur if storage_location == 4 else oci_us
|
|
|
|
+ redis_obj = RedisObject(3)
|
|
|
|
+ media_url = DetectControllerViewV2.oci_object_url(oci, redis_obj, uid, prefix_name)
|
|
|
|
+ if media_url:
|
|
|
|
+ media_url = media_url + obj_name
|
|
|
|
+
|
|
|
|
+ elif storage_location == 5: # 华为云
|
|
|
|
+ obs_client = ObsClient(
|
|
|
|
+ access_key_id=HUAWEICLOUD_AK, secret_access_key=HUAWEICLOUD_SK, server=HUAWEICLOUD_OBS_SERVER)
|
|
|
|
+ create_res = obs_client.createSignedUrl(
|
|
|
|
+ method='GET', bucketName=HUAWEICLOUD_PUSH_BUKET, objectKey=obj_name, expires=300)
|
|
|
|
+ media_url = create_res.signedUrl
|
|
|
|
+
|
|
|
|
+ else:
|
|
|
|
+ media_url = ''
|
|
|
|
+ return media_url
|