-3"> 3
+from django.conf import settings
4
+from django.db import models
5
+from django.utils.translation import ugettext_lazy as _
6
+
7
+from pai2.basemodels import CreateUpdateMixin
8
+
9
+
10
+class UserMessageInfo(CreateUpdateMixin):
11
+    SYSTEM = 'system'
12
+    COMMENT = 'comment'
13
+    THUMBUP = 'thumbup'
14
+
15
+    MESSAGE_TYPE = (
16
+        (SYSTEM, u'系统'),
17
+        (COMMENT, u'评论'),
18
+        (THUMBUP, u'点赞'),
19
+    )
20
+
21
+    MESSAGE_TYPE_INFO = [
22
+        {
23
+            'msg_type': SYSTEM,
24
+            'msg_avatar': settings.SYSTEM_MESSAGE_AVATAR
25
+        }, {
26
+            'msg_type': COMMENT,
27
+            'msg_avatar': settings.COMMENT_MESSAGE_AVATAR
28
+        }, {
29
+            'msg_type': THUMBUP,
30
+            'msg_avatar': settings.THUMBUP_MESSAGE_AVATAR
31
+        }
32
+    ]
33
+
34
+    from_uid = models.CharField(_(u'from_uid'), max_length=255, blank=True, null=True, help_text=u'发送消息用户唯一标识', db_index=True)
35
+    from_nickname = models.CharField(_(u'from_nickname'), max_length=255, blank=True, null=True, help_text=u'发送消息用户昵称')
36
+    from_avatar = models.CharField(_(u'from_avatar'), max_length=255, blank=True, null=True, help_text=u'发送消息用户头像')
37
+
38
+    to_uid = models.CharField(_(u'to_uid'), max_length=255, blank=True, null=True, help_text=u'接收消息用户唯一标识', db_index=True)
39
+
40
+    group_id = models.CharField(_(u'group_id'), max_length=255, blank=True, null=True, help_text=u'群组唯一标识')
41
+    photo_id = models.CharField(_(u'photo_id'), max_length=255, blank=True, null=True, help_text=u'飞图唯一标识')
42
+
43
+    msg_type = models.CharField(_(u'msg_type'), max_length=255, default='system', help_text=u'消息类型', db_index=True)
44
+    msg_title = models.CharField(_(u'msg_title'), max_length=255, blank=True, null=True, help_text=u'消息标题')
45
+    msg_content = models.TextField(_(u'msg_content'), blank=True, null=True, help_text=u'消息内容')
46
+    read = models.BooleanField(_(u'read'), default=False, help_text=u'消息是否已读')
47
+
48
+    class Meta:
49
+        verbose_name = _('usermessageinfo')
50
+        verbose_name_plural = _('usermessageinfo')
51
+
52
+    def __unicode__(self):
53
+        return u'{0.title}'.format(self)
54
+
55
+    @property
56
+    def msg_info(self):
57
+        return {
58
+            'pk': self.pk,
59
+            'from_uid': self.from_uid,
60
+            'from_nickname': self.from_nickname,
61
+            'from_avatar': self.from_avatar,
62
+            'group_id': self.group_id,
63
+            'photo_id': self.photo_id,
64
+            'msg_title': self.msg_title,
65
+            'msg_content': self.msg_content,
66
+            'read': self.read,
67
+            'created_at': self.created_at,
68
+        }

+ 3 - 0
message/tests.py

@@ -0,0 +1,3 @@
1
+from django.test import TestCase
2
+
3
+# Create your tests here.

+ 73 - 0
message/views.py

@@ -0,0 +1,73 @@
1
+# -*- coding: utf-8 -*-
2
+
3
+from django.conf import settings
4
+from django.http import JsonResponse
5
+
6
+from message.models import UserMessageInfo
7
+
8
+from utils.page_utils import pagination
9
+
10
+
11
+def message_list_api(request):
12
+    messages = UserMessageInfo.MESSAGE_TYPE_INFO
13
+
14
+    final_messages = []
15
+    for message in messages:
16
+        type_messages = UserMessageInfo.objects.filter(
17
+            msg_type=message['msg_type']
18
+        ).order_by(
19
+            '-updated_at'
20
+        )[:settings.MESSAGE_NUM_PER_PAGE]
21
+        type_messages = [msg.msg_info for msg in type_messages]
22
+        message['msg_list'] = type_messages
23
+        final_messages.append(message)
24
+
25
+    return JsonResponse({
26
+        'status': 200,
27
+        'message': u'获取消息列表成功',
28
+        'data': {
29
+            'messages': final_messages,
30
+        },
31
+    })
32
+
33
+
34
+def message_type_list_api(request, msg_type):
35
+    page = int(request.GET.get('page', 1))
36
+    num = int(request.GET.get('num', settings.MESSAGE_NUM_PER_PAGE))
37
+
38
+    type_messages = UserMessageInfo.objects.filter(
39
+        msg_type=msg_type
40
+    ).order_by(
41
+        '-updated_at'
42
+    )
43
+    type_messages, left = pagination(type_messages, page, num)
44
+    type_messages = [msg.msg_info for msg in type_messages]
45
+
46
+    return JsonResponse({
47
+        'status': 200,
48
+        'message': u'获取消息列表成功',
49
+        'data': {
50
+            'messages': type_messages,
51
+            'left': left,
52
+        },
53
+    })
54
+
55
+
56
+def message_read_api(request):
57
+    pk = int(request.GET.get('pk', -1))
58
+
59
+    try:
60
+        message = UserMessageInfo.objects.get(pk=pk)
61
+    except UserMessageInfo.DoesNotExist:
62
+        return JsonResponse({
63
+            'status': 4091,
64
+            'message': u'该消息不存在'
65
+        })
66
+
67
+    message.read = True
68
+    message.save()
69
+
70
+    return JsonResponse({
71
+        'status': 200,
72
+        'message': u'已读消息成功',
73
+    })

+ 11 - 1
pai2/settings.py

@@ -44,8 +44,9 @@ INSTALLED_APPS = (
44 44
     'api',
45 45
     'account',
46 46
     'group',
47
-    'photo',
47
+    'message',
48 48
     'operation',
49
+    'photo',
49 50
 )
50 51
 
51 52
 INSTALLED_APPS += ('multidomain', )
@@ -172,6 +173,15 @@ THUMBNAIL_MAX_WIDTH = 360
172 173
 DOMAIN = 'http://pai.ai'
173 174
 IMG_DOMAIN = 'http://img.pai.ai'
174 175
 
176
+# 消息图片设置
177
+PAI2_LOGO_URL = DOMAIN + '/static/pai2/img/paiai_96_96.png'
178
+
179
+SYSTEM_MESSAGE_AVATAR = PAI2_LOGO_URL
180
+COMMENT_MESSAGE_AVATAR = PAI2_LOGO_URL
181
+THUMBUP_MESSAGE_AVATAR = PAI2_LOGO_URL
182
+
183
+MESSAGE_NUM_PER_PAGE = 10
184
+
175 185
 try:
176 186
     from local_settings import *
177 187
 except ImportError:

+ 15 - 0
utils/page_utils.py

@@ -0,0 +1,15 @@
1
+# -*- coding: utf-8 -*-
2
+
3
+from django.db.models.query import QuerySet
4
+
5
+
6
+def pagination(queryset, page, num=10):
7
+    """
8
+    DIY Pagination Funciton
9
+    :param queryset:
10
+    :param page:
11
+    :param num: the number of query for one page
12
+    :return: the query of the page, the number of query left after the page
13
+    """
14
+    start, end, total = num * (page - 1), num * page, queryset.count() if isinstance(queryset, QuerySet) else len(queryset)
15
+    return queryset[start: end], max(total - end, 0)

kodo - Gogs: Go Git Service

No Description

huangqimin001: 96c4bec605 :art: Add api activity_group_share 5 years ago
..
0001_initial.py df1df69fe3 add api upgrade/splash 10 years ago
0002_auto_20160120_1830.py a121b75ff2 add db_index=True for status field 10 years ago
0003_feedbackinfo.py ebaa78d5ad add api feedback_api 10 years ago
0004_guestentrancecontrolinfo.py 63eaee0951 modify guest_login_api 10 years ago
0005_auto_20160509_1907.py 2f29afecbd modify version in operation 10 years ago
0006_feedbackinfo_src.py a71f3c569b add src for feedback 10 years ago
0007_auto_20160907_1740.py 27d3ec8fb1 add src for LatestAppInfo/SplashInfo 9 years ago
0008_appsettingsinfo.py dd00e3b1a9 add api online_api 9 years ago
0009_auto_20161220_1354.py ead8d009b7 Add src PAIAI_TOURGUIDE 9 years ago
0010_auto_20170308_2242.py 4f87058e1c Add patch_api 9 years ago
0011_auto_20170315_2243.py 2eba3ed7f9 Add guest login for tourguide 9 years ago
0012_boxprogramversioninfo.py afd07e5389 Add api box_program_version_api 9 years ago
0013_auto_20170418_1451.py 24dcbc8f3a Add srv_sha1 & proc_sha1 9 years ago
0014_auto_20170814_2001.py 65db8b1bc1 Add Src PAIAI_LENSMAN2 9 years ago
0015_auto_20170814_2004.py c3970eff70 Add Src PAIAI_LENSMAN2 9 years ago
0016_auto_20180101_2220.py 9bb56c50cc Makemigrations 8 years ago
0017_auto_20180103_0446.py a7cbbf15a7 Update max_length for CharField 8 years ago
0018_auto_20180114_2314.py 917f2df489 Add api api/upgrade 8 years ago
0019_auto_20201130_0131.py 96c4bec605 :art: Add api activity_group_share 5 years ago
__init__.py df1df69fe3 add api upgrade/splash 10 years ago