|
@@ -1,4 +1,5 @@
|
|
import time
|
|
import time
|
|
|
|
+from datetime import datetime
|
|
|
|
|
|
import oss2
|
|
import oss2
|
|
import boto3
|
|
import boto3
|
|
@@ -17,7 +18,7 @@ from Object.RedisObject import RedisObject
|
|
from Object.ResponseObject import ResponseObject
|
|
from Object.ResponseObject import ResponseObject
|
|
from Object.OCIObjectStorage import OCIObjectStorage
|
|
from Object.OCIObjectStorage import OCIObjectStorage
|
|
|
|
|
|
-from Roomumy.models import TimeAlbum, AlbumMedia, TimeDiary
|
|
|
|
|
|
+from Roomumy.models import TimeAlbum, AlbumMedia, TimeDiary, DiaryAlbumMediaRelation
|
|
from Service.CommonService import CommonService
|
|
from Service.CommonService import CommonService
|
|
from Controller.DetectControllerV2 import DetectControllerViewV2
|
|
from Controller.DetectControllerV2 import DetectControllerViewV2
|
|
|
|
|
|
@@ -38,7 +39,7 @@ class TimeAlbumView(View):
|
|
if token_code != 0:
|
|
if token_code != 0:
|
|
return response.json(token_code)
|
|
return response.json(token_code)
|
|
if operation == 'getAlbumPic': # 查询相册列表
|
|
if operation == 'getAlbumPic': # 查询相册列表
|
|
- return self.get_album_pic(request_dict, response)
|
|
|
|
|
|
+ return self.get_album_pic(user_id, request_dict, response)
|
|
elif operation == 'getTimeDiary': # 获取时光日记列表
|
|
elif operation == 'getTimeDiary': # 获取时光日记列表
|
|
return self.get_time_diary(user_id, request_dict, response)
|
|
return self.get_time_diary(user_id, request_dict, response)
|
|
elif operation == 'picLiked': # 喜欢图片
|
|
elif operation == 'picLiked': # 喜欢图片
|
|
@@ -46,22 +47,22 @@ class TimeAlbumView(View):
|
|
else:
|
|
else:
|
|
return response.json(414)
|
|
return response.json(414)
|
|
|
|
|
|
- def get_album_pic(self, request_dict, response):
|
|
|
|
- uid = request_dict.get('uid', None)
|
|
|
|
|
|
+ def get_album_pic(self, user_id, request_dict, response):
|
|
|
|
+ device_id = request_dict.get('deviceId', None)
|
|
start_time = request_dict.get('startTime', None)
|
|
start_time = request_dict.get('startTime', None)
|
|
end_time = request_dict.get('endTime', None)
|
|
end_time = request_dict.get('endTime', None)
|
|
page = request_dict.get('page', 1) # 当前页码,默认为1
|
|
page = request_dict.get('page', 1) # 当前页码,默认为1
|
|
page_size = request_dict.get('pageSize', 10) # 每页显示的记录数,默认为10
|
|
page_size = request_dict.get('pageSize', 10) # 每页显示的记录数,默认为10
|
|
|
|
|
|
# 检查是否提供了uid,如果没有提供返回错误
|
|
# 检查是否提供了uid,如果没有提供返回错误
|
|
- if uid is None:
|
|
|
|
|
|
+ if device_id is None:
|
|
return response.json(444)
|
|
return response.json(444)
|
|
try:
|
|
try:
|
|
# 查询TimeAlbum表,按uid过滤,且如果提供了album_date,则进一步按album_date过滤
|
|
# 查询TimeAlbum表,按uid过滤,且如果提供了album_date,则进一步按album_date过滤
|
|
- time_album_qs = TimeAlbum.objects.filter(uid=uid).order_by('-album_date')
|
|
|
|
|
|
+ time_album_qs = TimeAlbum.objects.filter(device_id=device_id).order_by('-album_date')
|
|
|
|
|
|
if start_time and end_time:
|
|
if start_time and end_time:
|
|
- time_album_qs = time_album_qs.filter(album_date__gt=start_time, album_date__lt=end_time)
|
|
|
|
|
|
+ time_album_qs = time_album_qs.filter(album_date__gte=start_time, album_date__lte=end_time)
|
|
|
|
|
|
# 如果没有找到符合条件的TimeAlbum记录,返回错误
|
|
# 如果没有找到符合条件的TimeAlbum记录,返回错误
|
|
if not time_album_qs.exists():
|
|
if not time_album_qs.exists():
|
|
@@ -76,30 +77,28 @@ class TimeAlbumView(View):
|
|
for time_album in time_albums:
|
|
for time_album in time_albums:
|
|
# 查询与当前time_album_id关联的AlbumMedia记录
|
|
# 查询与当前time_album_id关联的AlbumMedia记录
|
|
time_album_info = []
|
|
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')
|
|
|
|
|
|
+ album_media_qs = AlbumMedia.objects.filter(time_album_id=time_album.id).values('id', 'device_id', 'channel',
|
|
|
|
+ 'event_time', 'status',
|
|
|
|
+ 'storage_location')
|
|
|
|
|
|
if album_media_qs.exists():
|
|
if album_media_qs.exists():
|
|
for album_media in album_media_qs:
|
|
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
|
|
|
|
|
|
+ storage_location = album_media['storage_location']
|
|
|
|
+ uid = album_media['device_id']
|
|
|
|
+ channel = album_media['channel']
|
|
|
|
+ event_time = album_media['event_time']
|
|
|
|
+ image = self.media_url(storage_location, uid, channel, event_time)
|
|
|
|
+
|
|
|
|
+ # 判断是否加入时光日记
|
|
|
|
+ liked_status = 0
|
|
|
|
+ if DiaryAlbumMediaRelation.objects.filter(
|
|
|
|
+ album_media_id=album_media['id'],
|
|
|
|
+ diary__user_id=user_id
|
|
|
|
+ ).exists():
|
|
|
|
+ liked_status = 1
|
|
time_album_info.append({
|
|
time_album_info.append({
|
|
'picId': album_media['id'],
|
|
'picId': album_media['id'],
|
|
'image': image,
|
|
'image': image,
|
|
- 'video': video,
|
|
|
|
- 'thumbnail': thumbnail,
|
|
|
|
'likedStatus': liked_status,
|
|
'likedStatus': liked_status,
|
|
})
|
|
})
|
|
time_album_list.append({
|
|
time_album_list.append({
|
|
@@ -119,57 +118,46 @@ class TimeAlbumView(View):
|
|
def get_time_diary(self, user_id, request_dict, response):
|
|
def get_time_diary(self, user_id, request_dict, response):
|
|
start_time = request_dict.get('startTime', None)
|
|
start_time = request_dict.get('startTime', None)
|
|
end_time = request_dict.get('endTime', None)
|
|
end_time = request_dict.get('endTime', None)
|
|
- page = request_dict.get('page', 1) # 当前页码,默认为1
|
|
|
|
- page_size = request_dict.get('pageSize', 10) # 每页显示的记录数,默认为10
|
|
|
|
|
|
+ page = int(request_dict.get('page', 1))
|
|
|
|
+ page_size = int(request_dict.get('pageSize', 10))
|
|
|
|
|
|
try:
|
|
try:
|
|
- # 查询TimeAlbum表,按uid过滤,且如果提供了album_date,则进一步按album_date过滤
|
|
|
|
- time_album_qs = TimeAlbum.objects.filter(user_id=user_id).order_by('-album_date')
|
|
|
|
|
|
+ # 基础查询
|
|
|
|
+ time_diary_qs = TimeDiary.objects.filter(user_id=user_id).order_by('-diary_date')
|
|
|
|
|
|
|
|
+ # 时间范围过滤
|
|
if start_time and end_time:
|
|
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
|
|
|
|
|
|
+ time_diary_qs = time_diary_qs.filter(diary_date__gte=start_time, diary_date__lte=end_time)
|
|
|
|
+
|
|
|
|
+ # 分页
|
|
|
|
+ paginator = Paginator(time_diary_qs, page_size)
|
|
|
|
+ diaries_page = paginator.get_page(page)
|
|
|
|
+
|
|
|
|
+ diary_data_list = []
|
|
|
|
+ for diary in diaries_page:
|
|
|
|
+ # 通过中间表获取关联的图片
|
|
|
|
+ relations = DiaryAlbumMediaRelation.objects.filter(diary=diary).select_related('album_media')
|
|
|
|
+
|
|
|
|
+ media_list = []
|
|
|
|
+ for relation in relations:
|
|
|
|
+ media = relation.album_media
|
|
|
|
+ media_list.append({
|
|
|
|
+ 'picId': media.id,
|
|
|
|
+ 'likeStatus': 1,
|
|
|
|
+ 'image': self.media_url(media.storage_location, media.device_id, media.channel,
|
|
|
|
+ media.event_time)
|
|
})
|
|
})
|
|
|
|
|
|
- # 返回分页结果和数据
|
|
|
|
|
|
+ diary_data_list.append({
|
|
|
|
+ 'diary_date': diary.diary_date,
|
|
|
|
+ 'info': media_list
|
|
|
|
+ })
|
|
|
|
+
|
|
return response.json(0, {
|
|
return response.json(0, {
|
|
- 'data': time_album_list,
|
|
|
|
- 'total': paginator.count, # 总记录数
|
|
|
|
|
|
+ 'data': diary_data_list,
|
|
|
|
+ 'total': paginator.count,
|
|
})
|
|
})
|
|
|
|
+
|
|
except Exception as e:
|
|
except Exception as e:
|
|
return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
|
|
|
@@ -177,84 +165,69 @@ class TimeAlbumView(View):
|
|
def pic_liked(user_id, request_dict, response):
|
|
def pic_liked(user_id, request_dict, response):
|
|
pic_id = request_dict.get('picId', None)
|
|
pic_id = request_dict.get('picId', None)
|
|
liked_status = request_dict.get('likedStatus', None)
|
|
liked_status = request_dict.get('likedStatus', None)
|
|
|
|
+
|
|
if not all([pic_id, liked_status]):
|
|
if not all([pic_id, liked_status]):
|
|
return response.json(444)
|
|
return response.json(444)
|
|
|
|
+
|
|
liked_status = int(liked_status)
|
|
liked_status = int(liked_status)
|
|
|
|
+
|
|
try:
|
|
try:
|
|
- now_time = int(time.time())
|
|
|
|
album_media = AlbumMedia.objects.filter(id=pic_id).first()
|
|
album_media = AlbumMedia.objects.filter(id=pic_id).first()
|
|
if not album_media:
|
|
if not album_media:
|
|
|
|
+ return response.json(404, 'pic not found')
|
|
|
|
+
|
|
|
|
+ # 获取相册的日期
|
|
|
|
+ time_diary_qs = AlbumMedia.objects.filter(id=pic_id).values('time_album_id', 'event_time', 'status')
|
|
|
|
+ if not time_diary_qs.exists():
|
|
return response.json(173)
|
|
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()
|
|
|
|
|
|
+ event_time = time_diary_qs[0]['event_time']
|
|
|
|
+ dt = datetime.fromtimestamp(event_time)
|
|
|
|
+ dt_zero = datetime(dt.year, dt.month, dt.day)
|
|
|
|
+
|
|
|
|
+ diary_date = int(dt_zero.timestamp())
|
|
|
|
+
|
|
|
|
+ # 查找或创建用户今天的 TimeDiary
|
|
|
|
+ time_diary, created = TimeDiary.objects.get_or_create(
|
|
|
|
+ user_id=user_id,
|
|
|
|
+ diary_date=diary_date,
|
|
|
|
+ defaults={
|
|
|
|
+ 'created_time': int(time.time()),
|
|
|
|
+ 'updated_time': int(time.time())
|
|
|
|
+ }
|
|
|
|
+ )
|
|
|
|
+
|
|
|
|
+ if liked_status == 1:
|
|
|
|
+ # 添加到 TimeDiary(中间表中插入)
|
|
|
|
+ DiaryAlbumMediaRelation.objects.get_or_create(
|
|
|
|
+ diary=time_diary,
|
|
|
|
+ album_media=album_media,
|
|
|
|
+ defaults={
|
|
|
|
+ 'created_time': int(time.time()),
|
|
|
|
+ 'updated_time': int(time.time())
|
|
|
|
+ }
|
|
|
|
+ )
|
|
|
|
+ time_diary_qs.update(status=2)
|
|
|
|
+
|
|
|
|
+ elif liked_status == 0:
|
|
|
|
+ # 从中间表移除
|
|
|
|
+ DiaryAlbumMediaRelation.objects.filter(diary=time_diary, album_media=album_media).delete()
|
|
|
|
+ if DiaryAlbumMediaRelation.objects.filter(diary=time_diary).count() == 0:
|
|
|
|
+ time_diary.delete()
|
|
|
|
+ if not DiaryAlbumMediaRelation.objects.filter(album_media=album_media).exists():
|
|
|
|
+ time_diary_qs.update(status=1)
|
|
|
|
+
|
|
return response.json(0)
|
|
return response.json(0)
|
|
|
|
+
|
|
except Exception as e:
|
|
except Exception as e:
|
|
return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
|
|
|
@staticmethod
|
|
@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 = ''
|
|
|
|
|
|
+ def media_url(storage_location, uid, channel, event_time):
|
|
|
|
+ obj_name = f'roomumy/albumMedia/{uid}/{channel}/{event_time}.jpg'
|
|
|
|
+ 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="asj-app", objectKey=obj_name, expires=600)
|
|
|
|
+ media_url = create_res.signedUrl
|
|
return media_url
|
|
return media_url
|