Example #1
0
from Alumni.DatabaseManager import UserManager
from Alumni.DatabaseManager import ActivityManager
from Alumni.DatabaseManager import UserActivityManager
from Alumni.DatabaseManager import SearchAndRecommend
from Alumni.DatabaseManager import AdminManager
from Alumni.DatabaseManager import TimeManager
from Alumni.RequestHandler import UserHandler
from Alumni.RequestHandler import ActivityHandler
from Alumni.RequestHandler import UserActivityHandler
from Alumni.RequestHandler import SearchHandler
from Alumni.RequestHandler import OtherHandler
from Alumni.RequestHandler import AdminHandler

TheSearcher = SearchAndRecommend.WhooshSearcher.Create()
print(GlobalFunctions.SetAccessToken())
print(GlobalFunctions.GetAccessToken())
AdminManager.AddAdmin("kebab", "reich")

urlpatterns = [
    #处理用户请求url
    url(r'^login$', UserHandler.LoginUser),
    url(r'^alumniCheck$', UserHandler.GetAlumniInfo),
    url(r'^qhrcallback$', UserHandler.ReceiveAlunmiInfo),
    url(r'^userData$', UserHandler.QueryUser),
    url(r'^setAvatarUrl$', UserHandler.SetAvatarURL),
    url(r'^setExtraData$', UserHandler.SetExtraData),

    #处理活动请求url
    url(r'^createActivity$', ActivityHandler.StartActivity),
    url(r'^modifyActivity$', ActivityHandler.ChangeActivity),
    url(r'^modifyActivityDescription$', ActivityHandler.ChangeActivityDetail),
def SendTimedMessage(TheActivityItem, TheUserItem, TheMessageType):
    '''
	描述:推送活动消息函数
	参数: 活动的对象,用户对象,消息类型(作为数字定义在constants.py里)
	返回:无
	'''
    Success = True
    obj = {}
    data = {}
    pagePrefix = "/pages/ActivityList/ActivityDetail/ActivityDetail?activityId="
    if Success:
        try:
            if (TheMessageType == Constants.MESSAGE_TYPE_ACTIVITY_CHANGE
                    or TheMessageType == Constants.MESSAGE_TYPE_KICK
                    or TheMessageType == Constants.MESSAGE_TYPE_ACTIVITY_CANCEL
                    or TheMessageType
                    == Constants.MESSAGE_TYPE_ACTIVITY_FORBIDDEN):
                obj["template_id"] = "LEoLo9UsF2by_4UGypKf2v7YLXeYRRGGjskOa0iJzZY"
                data["thing2"] = {"value": TheActivityItem.Name}
                data["date4"] = {
                    "value":
                    time.strftime("%Y{y}%m{m}%d{d} %H:%M",
                                  time.localtime(
                                      TheActivityItem.StartTime)).format(y='年',
                                                                         m='月',
                                                                         d='日')
                }
                data["thing5"] = {"value": TheActivityItem.Place}
                detail = None
                if (TheMessageType == Constants.MESSAGE_TYPE_KICK):
                    detail = "您被活动主办方踢出了该活动。"
                elif (TheMessageType == Constants.MESSAGE_TYPE_ACTIVITY_CANCEL
                      ):
                    detail = "活动主办方取消了该活动。"
                elif (TheMessageType ==
                      Constants.MESSAGE_TYPE_ACTIVITY_FORBIDDEN):
                    detail = "由于违规行为,该活动已被封禁。"
                elif (TheMessageType == Constants.MESSAGE_TYPE_ACTIVITY_CHANGE
                      ):
                    detail = "活动安排发生了变更。点击可查看详情。"
                data["thing6"] = {"value": detail}
            elif (TheMessageType == Constants.MESSAGE_TYPE_AUDIT_PASS
                  or TheMessageType == Constants.MESSAGE_TYPE_AUDIT_FAIL):
                obj["template_id"] = "j9EPrZx9MAQ5SjbN1aCYHImxymn6ZEziJLgQEcbuXSk"
                data["thing2"] = {"value": TheActivityItem.Name}
                data["date3"] = {
                    "value":
                    time.strftime("%Y{y}%m{m}%d{d} %H:%M",
                                  time.localtime(
                                      TheActivityItem.StartTime)).format(y='年',
                                                                         m='月',
                                                                         d='日')
                }
                if (TheMessageType == Constants.MESSAGE_TYPE_AUDIT_PASS):
                    data["thing7"] = {"value": "您的加入申请已被通过"}
                    data["phrase1"] = {"value": "通过"}
                elif (TheMessageType == Constants.MESSAGE_TYPE_AUDIT_FAIL):
                    data["thing7"] = {"value": "如有疑问请联系活动主办方。"}
                    data["phrase1"] = {"value": "不通过"}
            elif (TheMessageType == Constants.MESSAGE_TYPE_NEW_ACTIVITY_PASS or
                  TheMessageType == Constants.MESSAGE_TYPE_NEW_ACTIVITY_FAIL):
                obj["template_id"] = "j9EPrZx9MAQ5SjbN1aCYHImxymn6ZEziJLgQEcbuXSk"
                data["thing2"] = {"value": TheActivityItem.Name}
                data["date3"] = {
                    "value":
                    time.strftime("%Y{y}%m{m}%d{d} %H:%M",
                                  time.localtime(
                                      TheActivityItem.StartTime)).format(y='年',
                                                                         m='月',
                                                                         d='日')
                }
                if (TheMessageType == Constants.MESSAGE_TYPE_NEW_ACTIVITY_PASS
                    ):
                    data["thing7"] = {"value": "校友总会已审核通过您发起的活动。"}
                    data["phrase1"] = {"value": "通过"}
                elif (TheMessageType ==
                      Constants.MESSAGE_TYPE_NEW_ACTIVITY_FAIL):
                    data["thing7"] = {"value": "校友总会拒绝了您发起的活动。"}
                    data["phrase1"] = {"value": "不通过"}
            elif (TheMessageType ==
                  Constants.MESSAGE_TYPE_ACTIVITY_WILL_START_HOUR):
                obj["template_id"] = "u-UA76noUE9_9g2ZVX53W9DQKz3x-Tn1914KHphfRXM"
                data["thing7"] = {"value": "您报名的活动将在明天举行,请合理安排行程"}
                data["thing4"] = {"value": TheActivityItem.Name}
                data["date3"] = {
                    "value":
                    time.strftime("%Y{y}%m{m}%d{d} %H:%M",
                                  time.localtime(
                                      TheActivityItem.StartTime)).format(y='年',
                                                                         m='月',
                                                                         d='日')
                }
                data["thing6"] = {"value": TheActivityItem.Place}
            elif (TheMessageType ==
                  Constants.MESSAGE_TYPE_ACTIVITY_WILL_START_MINUTE):
                obj["template_id"] = "u-UA76noUE9_9g2ZVX53Wz3QZ-IgE4ECwLVxWLIJlZ8"
                data["thing7"] = {"value": "您报名的活动即将举行,请按时到场并签到"}
                data["thing4"] = {"value": TheActivityItem.Name}
                data["date5"] = {
                    "value":
                    time.strftime("%Y{y}%m{m}%d{d} %H:%M",
                                  time.localtime(
                                      TheActivityItem.StartTime)).format(y='年',
                                                                         m='月',
                                                                         d='日')
                }
                data["thing6"] = {"value": TheActivityItem.Place}
            else:
                Success = False
                Reason = "没有对应的消息类型"
        except:
            Success = False
            Reason = "产生消息阶段异常"

    if Success:
        try:
            obj["touser"] = TheUserItem.OpenID
            obj["page"] = pagePrefix + str(TheActivityItem.ID)
            obj["data"] = data
        except:
            Success = False
            Reason = "产生消息阶段2异常"

    if Success:
        try:
            url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + GlobalFunctions.GetAccessToken(
            )
            TheRequest = requests.post(url,
                                       data=json.dumps(obj),
                                       timeout=(5, 10),
                                       allow_redirects=True)
        except:
            Success = False
            ErrorId = Constants.ERROR_CODE_NETWORK_ERROR
            Reason = "网络繁忙,访问超时!"

    if Success:
        try:
            if TheRequest.status_code < 200 or TheRequest.status_code >= 400:
                Success = False
                ErrorId = Constants.ERROR_CODE_NETWORK_ERROR
                Reason = "网络繁忙,访问微信失败!!"
            TheJson = TheRequest.json()
            #print(TheJson)
            if "errcode" in TheJson:
                if TheJson["errcode"] == -1:
                    Success = False
                    ErrorId = Constants.ERROR_CODE_NETWORK_ERROR
                    Reason = "网络繁忙,访问微信失败!"
                elif TheJson["errcode"] != 0:
                    Success = False
                    ErrorId = Constants.ERROR_CODE_NETWORK_ERROR
                    Reason = TheJson["errmsg"]
        except:
            Success = False
            ErrorId = Constants.ERROR_CODE_NETWORK_ERROR
            Reason = "网络繁忙,访问微信失败!"

    return Success