活动发布
All checks were successful
Deploy to Server / deploy (push) Successful in 1m44s

This commit is contained in:
jeremygan2021
2026-03-04 13:07:54 +08:00
parent 96ebd781b9
commit d3dbaaa090
23 changed files with 160 additions and 409 deletions

View File

@@ -411,3 +411,66 @@ class AnnouncementViewSet(viewsets.ReadOnlyModelViewSet):
# Filter by end_time (if set, must be >= now)
qs = qs.filter(models.Q(end_time__isnull=True) | models.Q(end_time__gte=now))
return qs.order_by('-is_pinned', '-priority', '-created_at')
class AdminPublishViewSet(viewsets.ViewSet):
"""
管理员/API发布接口
"""
permission_classes = []
authentication_classes = []
def check_api_key(self, request):
key = request.headers.get('X-API-KEY') or request.query_params.get('apikey')
if key != '123quant-speed':
return False
return True
def get_admin_user_by_phone(self, phone):
if not phone:
return None
# Find WeChatUser by phone
user = WeChatUser.objects.filter(phone_number=phone).first()
if not user:
return None
# Check if linked to a system user and has admin privileges (is_staff)
if user.user and user.user.is_staff:
return user
return None
@extend_schema(summary="API发布活动")
@action(detail=False, methods=['post'])
def publish_activity(self, request):
if not self.check_api_key(request):
return Response({'error': 'Invalid API Key'}, status=403)
phone = request.data.get('phone_number')
user = self.get_admin_user_by_phone(phone)
if not user:
return Response({'error': 'Admin user not found with this phone number'}, status=404)
data = request.data.copy()
serializer = ActivitySerializer(data=data)
if serializer.is_valid():
activity = serializer.save(author=user)
return Response(serializer.data, status=201)
return Response(serializer.errors, status=400)
@extend_schema(summary="API发布帖子")
@action(detail=False, methods=['post'])
def publish_topic(self, request):
if not self.check_api_key(request):
return Response({'error': 'Invalid API Key'}, status=403)
phone = request.data.get('phone_number')
user = self.get_admin_user_by_phone(phone)
if not user:
return Response({'error': 'Admin user not found with this phone number'}, status=404)
data = request.data.copy()
serializer = TopicSerializer(data=data)
if serializer.is_valid():
topic = serializer.save(author=user, status='published')
return Response(serializer.data, status=201)
return Response(serializer.errors, status=400)