Skip to content

Commit

Permalink
Merge pull request #191 from CSSAInformationDepartment/fix-event-api
Browse files Browse the repository at this point in the history
Fix event api
  • Loading branch information
Liu233w authored Nov 20, 2021
2 parents d2e62f3 + 9591be0 commit cdf7f86
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 26 deletions.
9 changes: 6 additions & 3 deletions src/EventAPI/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ class Meta:
'DisplayArticleType': _("选择活动介绍所要显示的文章来源"),
'relatedArticles': _("若文章来源选择为Blog,则此项必填"),
'WechatArticleUrl': _("若文章来源选择为WeChat,则此项必填"),
'WechatQRcode': _("若文章来源选择为WeChat,则必须提供文章对应的微信公众号"),

'WechatQRcode': _("若文章来源选择为WeChat,可提供文章对应的微信公众号的二维码"),

'pastEventLink': _("可以为空"),
'recentEventLink': _("可以为空"),
'pastEventPoster': _("可以为空"),
'recentEventPoster': _("可以为空"),
}
widgets = {
'eventInfo': forms.Textarea(attrs={'rows': 3}),
Expand All @@ -42,7 +45,7 @@ def clean(self, *args, **kwargs):
display_article_type = self.cleaned_data.get('DisplayArticleType')
wechat_qr_code = self.cleaned_data.get('WechatQRcode')

if (display_article_type == 'WeChat' and (wechat_article_url == None or wechat_qr_code == None)) or (display_article_type == 'Blog' and related_articles == None):
if (display_article_type == 'WeChat' and (wechat_article_url == None)) or (display_article_type == 'Blog' and related_articles == None):
errors.append(ValidationError(_(mark_safe('<li>文章显示设置不正确,请检查相关设置项</li>')), code='invalid article display setting'))

event_actualSt_time = self.cleaned_data.get("eventActualStTime")
Expand Down
33 changes: 33 additions & 0 deletions src/EventAPI/migrations/0020_auto_20211120_2339.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Generated by Django 3.1.12 on 2021-11-20 12:39

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('EventAPI', '0019_auto_20190509_0759'),
]

operations = [
migrations.AddField(
model_name='event',
name='pastEventLink',
field=models.URLField(default=None, null=True, verbose_name='往期活动链接'),
),
migrations.AddField(
model_name='event',
name='pastEventPoster',
field=models.ImageField(default=None, null=True, upload_to='uploads/usrImage/eventPastVisual', verbose_name='往期活动海报'),
),
migrations.AddField(
model_name='event',
name='recentEventLink',
field=models.URLField(default=None, null=True, verbose_name='近期活动链接'),
),
migrations.AddField(
model_name='event',
name='recentEventPoster',
field=models.ImageField(default=None, null=True, upload_to='uploads/usrImage/eventRecentVisual', verbose_name='近期活动海报'),
),
]
7 changes: 7 additions & 0 deletions src/EventAPI/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ class Event (models.Model):
WechatArticleUrl = models.CharField(verbose_name=_("微信公众号文章链接"), max_length=500, default=None, null=True, blank=True)
WechatQRcode = models.ImageField(verbose_name=_("微信二维码"),upload_to='uploads/usrImage/eventWechatQRcode',default=None ,null=True, blank=True)

# 近期、往期活动的链接和海报
pastEventLink = models.URLField(verbose_name=_("往期活动链接"), default=None, null=True)
recentEventLink = models.URLField(verbose_name=_("近期活动链接"), default=None, null=True)

pastEventPoster = models.ImageField(verbose_name=_("往期活动海报"), upload_to='uploads/usrImage/eventPastVisual', default=None, null=True)
recentEventPoster = models.ImageField(verbose_name=_("近期活动海报"), upload_to='uploads/usrImage/eventRecentVisual', default=None, null=True)

disabled = models.BooleanField(default=False)

def __str__(self):
Expand Down
8 changes: 7 additions & 1 deletion src/EventAPI/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@

class EventsSerializer(serializers.ModelSerializer):

def get_eventTaker(self, instance):
return instance.eventBy.eventTakerName if instance.eventBy else None

def get_eventType(self, instance):
return instance.eventTypes.typeName if instance.eventTypes else None

class Meta:
model = eventModels
fields = ('eventID','eventName','eventInfo','mainVisual','address','venue','isFree','price',
'eventBy','eventTypes')
'pastEventLink', 'pastEventPoster', 'recentEventLink', 'recentEventPoster',)

2 changes: 1 addition & 1 deletion src/EventAPI/templates/EventAPI/add_event.html
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@

<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-fileinput/4.5.1/themes/fa/theme.min.js"></script>
<script>
$('#id_mainVisual, #id_WechatQRcode').fileinput({
$('#id_mainVisual, #id_WechatQRcode, #id_pastEventPoster, #id_recentEventPoster').fileinput({
theme: "fa",
allowedFileExtensions: ['jpeg','jpg', 'png'],
overwriteInitial: false,
Expand Down
37 changes: 16 additions & 21 deletions src/EventAPI/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from django.shortcuts import render, get_object_or_404, Http404
from django.db.models import Q
from rest_framework import permissions
from rest_framework.decorators import permission_classes

from .models import *
from .forms import *
Expand Down Expand Up @@ -268,41 +270,34 @@ def filter_queryset(self, qs):
########Start###################### Event API for mobile App ##################Start###################
from rest_framework.views import APIView
from .serializers import EventsSerializer
import base64, json
from django.core.serializers.json import DjangoJSONEncoder
# from rest_framework.response import Response
# from django.core import serializers
from rest_framework.response import Response as RestResponse

def query_events():
return Event.objects \
.select_related('eventBy', 'eventTypes') \
.order_by("eventActualStTime")

class MobilePastEventAPI(APIView):
permission_classes = [permissions.AllowAny]

def get(self, request, format=None):
sys_time.activate('Australia/Melbourne')
now_time = sys_time.now()
eventsPast= Event.objects.filter(eventActualStTime__lt=now_time).order_by("eventActualStTime")
eventsPast= query_events().filter(eventActualStTime__lt=now_time)

# queryset是实例集合,需要加 many=True ,如果是单个实例,可以不用加 many=True
serializer = EventsSerializer(eventsPast, many = True)

# data = serializers.serialize("json",serializer.eventsPast) # 直接序列化成json形式

# 两种返回方法都行,下面一种需要设置,设定已标注
# In order to allow non-dict objects to be serialized set the safe parameter to False
data = json.dumps(serializer.data, cls=DjangoJSONEncoder)
# 这里需要加encode()才能通过base64进行编码
data_encode = base64.b64encode(data.encode())
# return JsonResponse(serializer.data, safe=False)
return HttpResponse(data_encode, content_type="text/plain")

return RestResponse(serializer.data)

class MobileFutureEventAPI(APIView):
permission_classes = [permissions.AllowAny]

#原理同上
def get(self, request, format=None):
sys_time.activate('Australia/Melbourne')
now_time = sys_time.now()
eventsFuture = Event.objects.filter(eventActualStTime__gt=now_time).order_by("eventActualStTime")
eventsFuture = query_events().filter(eventActualStTime__gt=now_time)
serializer = EventsSerializer(eventsFuture, many = True)
data = json.dumps(serializer.data, cls=DjangoJSONEncoder)
data_encode = base64.b64encode(data.encode())
return HttpResponse(data_encode, content_type="text/plain")

return RestResponse(serializer.data)

########End######################## Event API for mobile App ######################End##############
2 changes: 2 additions & 0 deletions src/PublicSite/templates/PublicSite/eventDetails.html
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,14 @@ <h5 class="card-title"><strong>{{events.eventName}}</strong></h5>
</div> -->
<div class="row justify-content-center text-center">
<p>详情请见: <a href="{{events.WechatArticleUrl}}" class="text-primary">&nbsp&nbsp&nbsp<u> {{events.WechatArticleUrl}}</u></a>
{% if events.WechatQRcode %}
<br>
报名后请扫小助手二维码哦!
<br>
小助手二维码
<br>
<img src="{{events.WechatQRcode.url}}" alt="{{events.eventName}}" class="img-thumbnail rounded mx-auto d-bloc" style="min-width:150px; max-width:250px; margin-top:10px">
{% endif %}
</p>
</div>

Expand Down

0 comments on commit cdf7f86

Please sign in to comment.