130 lines
4.8 KiB
Python
130 lines
4.8 KiB
Python
"""轮播 / 公告 / 弹窗按俱乐部读取与写入。"""
|
|
from jituan.constants import CLUB_ID_DEFAULT, DATA_SCOPE_ALL
|
|
from jituan.services.club_context import filter_queryset_by_club, resolve_club_id_from_request, resolve_club_scope
|
|
from rest_framework.response import Response
|
|
|
|
# 定稿 B 方案 page_key
|
|
LUNBO_PAGE_ORDER_POOL = 'order_pool'
|
|
LUNBO_PAGE_ACCEPT_ORDER = 'accept_order'
|
|
LUNBO_PAGE_MERCHANT_HOME = 'merchant_home'
|
|
LUNBO_PAGE_DASHOU_CENTER = 'dashou_center'
|
|
|
|
LUNBO_PAGE_OPTIONS = [
|
|
(LUNBO_PAGE_ORDER_POOL, '抢单池'),
|
|
(LUNBO_PAGE_ACCEPT_ORDER, '点单页'),
|
|
(LUNBO_PAGE_MERCHANT_HOME, '商家首页'),
|
|
(LUNBO_PAGE_DASHOU_CENTER, '打手个人中心背景'),
|
|
]
|
|
|
|
|
|
def normalize_page_key(page_key, image_type=1):
|
|
key = (page_key or '').strip()
|
|
if image_type == 2:
|
|
return LUNBO_PAGE_DASHOU_CENTER
|
|
if key in {LUNBO_PAGE_ORDER_POOL, LUNBO_PAGE_ACCEPT_ORDER, LUNBO_PAGE_MERCHANT_HOME}:
|
|
return key
|
|
return LUNBO_PAGE_ORDER_POOL
|
|
|
|
|
|
def forbid_display_write_in_all_scope(request):
|
|
if resolve_club_scope(request) == DATA_SCOPE_ALL:
|
|
return Response({'code': 403, 'msg': '请在子公司视图下修改展示配置'})
|
|
return None
|
|
|
|
|
|
def filter_popup_page_by_request(qs, request):
|
|
return filter_queryset_by_club(qs, request, club_field='club_id')
|
|
|
|
|
|
def forbid_popup_page_out_of_scope(request, page):
|
|
if resolve_club_scope(request) == DATA_SCOPE_ALL:
|
|
return None
|
|
club_id = resolve_club_id_from_request(request)
|
|
page_club = getattr(page, 'club_id', None) or CLUB_ID_DEFAULT
|
|
if page_club != club_id:
|
|
return Response({'code': 403, 'msg': '该弹窗配置不属于当前俱乐部'})
|
|
return None
|
|
|
|
|
|
def get_gonggao_content(request, notice_type=1):
|
|
from config.models import Gonggao
|
|
club_id = resolve_club_id_from_request(request)
|
|
obj = Gonggao.query.filter(club_id=club_id, NoticeType=notice_type).first()
|
|
return obj.Content if obj and obj.Content else ''
|
|
|
|
|
|
def get_lunbo_urls(request, page_key=None, image_type=1):
|
|
from config.models import Lunbo
|
|
club_id = resolve_club_id_from_request(request)
|
|
pk = normalize_page_key(page_key, image_type=image_type)
|
|
qs = Lunbo.query.filter(club_id=club_id, ImageType=image_type, page_key=pk)
|
|
return [row.ImageURL for row in qs.order_by('id') if row.ImageURL]
|
|
|
|
|
|
def list_response_meta(request):
|
|
from jituan.services.club_user_access import list_response_meta as _meta
|
|
return _meta(request)
|
|
|
|
|
|
def copy_display_config(template_club_id, new_club_id):
|
|
"""从模板俱乐部复制轮播/公告/弹窗/图片配置到新俱乐部。"""
|
|
from config.models import Gonggao, Lunbo, PopupConfig, PopupImage, PopupPage, Tupianpeizhi
|
|
|
|
for row in Lunbo.query.filter(club_id=template_club_id):
|
|
Lunbo.query.create(
|
|
club_id=new_club_id,
|
|
page_key=row.page_key or 'order_pool',
|
|
ImageURL=row.ImageURL,
|
|
ImageType=row.ImageType,
|
|
)
|
|
|
|
for row in Gonggao.query.filter(club_id=template_club_id):
|
|
Gonggao.query.create(
|
|
club_id=new_club_id,
|
|
NoticeType=row.NoticeType,
|
|
Content=row.Content,
|
|
)
|
|
|
|
for row in Tupianpeizhi.query.filter(club_id=template_club_id):
|
|
if Tupianpeizhi.query.filter(club_id=new_club_id, ImageType=row.ImageType).exists():
|
|
continue
|
|
Tupianpeizhi.query.create(
|
|
club_id=new_club_id,
|
|
ImageURL=row.ImageURL,
|
|
ImageType=row.ImageType,
|
|
AccessCount=0,
|
|
)
|
|
|
|
for page in PopupPage.query.filter(club_id=template_club_id).prefetch_related('popups__images'):
|
|
new_page = PopupPage.query.create(
|
|
club_id=new_club_id,
|
|
page_key=page.page_key,
|
|
name=page.name,
|
|
description=page.description,
|
|
is_active=page.is_active,
|
|
ignore_user_mute=page.ignore_user_mute,
|
|
)
|
|
for popup in page.popups.all():
|
|
new_popup = PopupConfig.query.create(
|
|
page=new_page,
|
|
popup_id=popup.popup_id,
|
|
title=popup.title,
|
|
description=popup.description,
|
|
strategy_type=popup.strategy_type,
|
|
max_count=popup.max_count,
|
|
reset_interval=popup.reset_interval,
|
|
duration=popup.duration,
|
|
force_even_muted=popup.force_even_muted,
|
|
sort_order=popup.sort_order,
|
|
is_active=popup.is_active,
|
|
start_time=popup.start_time,
|
|
end_time=popup.end_time,
|
|
)
|
|
for img in popup.images.all():
|
|
PopupImage.query.create(
|
|
popup_config=new_popup,
|
|
image_url=img.image_url,
|
|
text=img.text,
|
|
sort_order=img.sort_order,
|
|
)
|