Skip to content

Commit

Permalink
改进上一个PR里面的抽奖管理界面 (#839)
Browse files Browse the repository at this point in the history
+ 功能细化为两个action,“立即抽奖”和“立即停止并抽奖”
+ 加入了一些给管理界面用户的提示,改善用户体验
+ 在已经停止的奖池上调用“立即停止并抽奖”action时,不再认为这是一个错误
  • Loading branch information
Deophius committed Sep 1, 2024
1 parent df3027e commit 78d753c
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions app/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -810,20 +810,38 @@ class PoolAdmin(admin.ModelAdmin):
inlines = [PoolItemInline]
actions = []

@as_action('立即结束', actions, 'change', update = True)
def terminate_pool(self, request, queryset: QuerySet['Pool']):
if queryset.filter(end__isnull = False, end__lt = datetime.now()).exists():
raise ValueError('请不要在已结束的奖池上调用!')
queryset.update(end = datetime.now())
# Immediately get the results of the lottery pools
def _do_draw_lots(self, request, queryset: QuerySet['Pool']):
'''对queryset中所有未完成抽奖的抽奖奖池进行奖品分配。
这个函数假定queryset已经被select_for_update锁定,所以可以安全地查找“奖池记录”中与该奖池有关的行。
'''
lottery_pool_ids = list(queryset.filter(type = Pool.Type.LOTTERY).values_list('id', flat = True))
for pool_id in lottery_pool_ids:
pool_title = Pool.objects.get(id = pool_id).title
if PoolRecord.objects.filter(
pool__id = pool_id
).exclude(
status = PoolRecord.Status.LOTTERING
).exists():
self.message_user(request, "奖池【" + pool_title + "】在调用前已完成抽奖", 'warning')
continue
run_lottery(pool_id)
self.message_user(request, "奖池【" + pool_title + "】抽奖已完成")

@as_action('立即抽奖', actions, 'change', update = True)
def draw_lots(self, request, queryset: QuerySet['Pool']):
self._do_draw_lots(request, queryset)

@as_action('立即停止并抽奖', actions, 'change', update = True)
def stop_and_draw(self, request, queryset: QuerySet['Pool']):
queryset.update(end = datetime.now())
self.message_user(request, "已将选中奖池全部停止")
self._do_draw_lots(request, queryset)


@admin.register(PoolRecord)
class PoolRecordAdmin(admin.ModelAdmin):
list_display = ['user_display', 'status', 'prize', 'time']
list_display = ['user_display', 'pool', 'status', 'prize', 'time']
search_fields = ['user__name']
list_filter = [
'status', 'prize', 'time',
Expand Down

0 comments on commit 78d753c

Please sign in to comment.