def ChangeActivity(TheUserID, Information): ''' 描述:修改活动信息 参数:用户openid,待修改信息 返回:{result:success}/{result:fail,reason:xxx, code:xxx} ''' Success = True Return = {} Reason = "" Code = Constants.UNDEFINED_NUMBER ChangeDictionary = {} #判断是否能找到这个活动 if Success: try: TheUser = User.objects.get(OpenID=TheUserID) TheActivity = Activity.objects.get(ID=Information["id"]) if JudgeValid.JudgeWhetherManager(TheUserID, Information["id"]) != True: Success = False Reason = "没有修改权限,需要是管理员或者创建者!" Code = Constants.ERROR_CODE_LACK_ACCESS except: Success = False Reason = "没有修改权限" Code = Constants.ERROR_CODE_LACK_ACCESS if Success: if JudgeValid.JudgeActivityNormal(Information["id"]) != True: Success = False Reason = "活动状态为异常或结束,不能操作!" Code = Constants.ERROR_CODE_INVALID_CHANGE #读取修改数据 WhetherJudgeTime = False if Success: try: if "name" in Information: ChangeDictionary["name"] = Information["name"] else: ChangeDictionary["name"] = TheActivity.Name if "place" in Information: ChangeDictionary["place"] = Information["place"] else: ChangeDictionary["place"] = TheActivity.Place if "start" in Information: WhetherJudgeTime = True ChangeDictionary["start"] = int( GlobalFunctions.TimeStringToTimeStamp( Information["start"])) else: ChangeDictionary["start"] = TheActivity.StartTime if "end" in Information: WhetherJudgeTime = True ChangeDictionary["end"] = int( GlobalFunctions.TimeStringToTimeStamp(Information["end"])) else: ChangeDictionary["end"] = TheActivity.EndTime if "signupBeginAt" in Information: WhetherJudgeTime = True ChangeDictionary["signupBeginAt"] = int( GlobalFunctions.TimeStringToTimeStamp( Information["signupBeginAt"])) else: ChangeDictionary["signupBeginAt"] = TheActivity.SignUpStartTime if "signupStopAt" in Information: WhetherJudgeTime = True ChangeDictionary["signupStopAt"] = int( GlobalFunctions.TimeStringToTimeStamp( Information["signupStopAt"])) else: ChangeDictionary["signupStopAt"] = TheActivity.SignUpEndTime if "minUser" in Information: ChangeDictionary["minUser"] = int(Information["minUser"]) else: ChangeDictionary["minUser"] = TheActivity.MinUser if "maxUser" in Information: ChangeDictionary["maxUser"] = int(Information["maxUser"]) else: ChangeDictionary["maxUser"] = TheActivity.MaxUser if "type" in Information: ChangeDictionary["type"] = Information["type"] else: ChangeDictionary["type"] = TheActivity.Type if "statusGlobal" in Information: ChangeDictionary["statusGlobal"] = Information["statusGlobal"] else: ChangeDictionary["statusGlobal"] = TheActivity.StatusGlobal if "statusJoin" in Information: ChangeDictionary["statusJoin"] = Information["statusJoin"] else: ChangeDictionary["statusJoin"] = TheActivity.StatusJoin if "statusCheck" in Information: ChangeDictionary["statusCheck"] = Information["statusCheck"] else: ChangeDictionary["statusCheck"] = TheActivity.StatusCheck if "canBeSearched" in Information: ChangeDictionary["canBeSearched"] = bool( Information["canBeSearched"]) else: ChangeDictionary["canBeSearched"] = TheActivity.CanBeSearched if "imageUrl" in Information: ChangeDictionary["imageUrl"] = Information["imageUrl"] else: ChangeDictionary["imageUrl"] = TheActivity.ImageURL if "position" in Information: ChangeDictionary["position"] = Information["position"] else: ChangeDictionary["position"] = TheActivity.GPSPlace if "tags" in Information: if JudgeValid.JudgeTagListValid(Information["tags"]) != True: Success = False Reason = "参数不合法,标签不能带有英文逗号!" Code = Constants.ERROR_CODE_INVALID_PARAMETER ChangeDictionary["tags"] = GlobalFunctions.MergeTags( Information["tags"]) else: ChangeDictionary["tags"] = TheActivity.Tags #如果rules不存在就changedictionary为空,要判断 if "rules" in Information: if "ruleType" in Information["rules"]: ChangeDictionary["ruleType"] = Information["rules"][ "ruleType"] else: ChangeDictionary["ruleType"] = TheActivity.GlobalRule ChangeDictionary["rules"] = Information["rules"] else: ChangeDictionary["rules"] = [] ChangeDictionary["ruleType"] = TheActivity.GlobalRule ChangeDictionary["curTime"] = GlobalFunctions.GetCurrentTime() ChangeDictionary["curUser"] = TheActivity.CurrentUser except: Success = False Reason = "待修改数据格式不合法" Code = Constants.ERROR_CODE_INVALID_PARAMETER #print(Success) #判断活动type修改后是否有效 if Success: try: JudgeResult = JudgeValid.JudgeTypeChangeValid( TheActivity.Type, ChangeDictionary["type"], TheActivity.StatusGlobal) if JudgeResult["result"] != "success": Success = False Reason = JudgeResult["reason"] Code = JudgeResult["code"] except: Success = False Reason = "待修改数据格式不合法" Code = Constants.ERROR_CODE_INVALID_PARAMETER #判断时间,人数等修改后是否有效 if Success and WhetherJudgeTime: try: JudgeResult = JudgeValid.JudgeParameterValid(ChangeDictionary["curTime"], ChangeDictionary["start"], ChangeDictionary["end"], \ ChangeDictionary["signupBeginAt"], ChangeDictionary["signupStopAt"], ChangeDictionary["curUser"], ChangeDictionary["minUser"], \ ChangeDictionary["maxUser"]) if JudgeResult["result"] != "success": Success = False Reason = JudgeResult["reason"] Code = Constants.ERROR_CODE_INVALID_CHANGE except: Success = False Reason = "待修改数据格式不合法" Code = Constants.ERROR_CODE_INVALID_PARAMETER if Success and ChangeDictionary["maxUser"] != Constants.UNDEFINED_NUMBER: try: if ChangeDictionary["minUser"] > ChangeDictionary["maxUser"]: Success = False Reason = "最小人数不能大于最大人数!" Code = Constants.ERROR_CODE_INVALID_CHANGE if ChangeDictionary["curUser"] > ChangeDictionary["maxUser"]: Success = False Reason = "当前人数不能大于最大人数!" Code = Constants.ERROR_CODE_INVALID_CHANGE except: Success = False Reason = "待修改数据格式不合法" Code = Constants.ERROR_CODE_INVALID_PARAMETER #判断状态等改后是否有效 if Success: try: JudgeResult = JudgeValid.JudgeActivityStatusChangeValid( TheActivity.StatusGlobal, ChangeDictionary["statusGlobal"]) if JudgeResult["result"] != "success": Success = False Reason = JudgeResult["reason"] Code = Constants.ERROR_CODE_INVALID_CHANGE if JudgeValid.JudgeActivityStatusJoinValid( ChangeDictionary["statusJoin"]) != True: Success = False Reason = "待修改的活动加入状态不合法" Code = Constants.ERROR_CODE_INVALID_PARAMETER if JudgeValid.JudgeActivityStatusCheckValid( ChangeDictionary["statusCheck"]) != True: Success = False Reason = "待修改的活动签到状态不合法" Code = Constants.ERROR_CODE_INVALID_PARAMETER if JudgeValid.JudgeRuleTypeValid( ChangeDictionary["ruleType"]) != True: Success = False Reason = "待修改的活动规则类型不合法!" Code = Constants.ERROR_CODE_INVALID_PARAMETER except: Success = False Reason = "待修改数据格式不合法" Code = Constants.ERROR_CODE_INVALID_PARAMETER #判断高级规则等改后是否有效 if Success: try: if ChangeDictionary["rules"] != []: JudgeResult = JudgeValid.JudgeAdvancedRuleValid( ChangeDictionary["rules"]) if JudgeResult["result"] != "success": Success = False Reason = JudgeResult["reason"] Code = Constants.ERROR_CODE_INVALID_CHANGE except: Success = False Reason = "待修改数据格式不合法" Code = Constants.ERROR_CODE_INVALID_PARAMETER #修改 #print(Success) if Success: try: TheActivity.Name = ChangeDictionary["name"] TheActivity.Place = ChangeDictionary["place"] TheActivity.StartTime = ChangeDictionary["start"] TheActivity.EndTime = ChangeDictionary["end"] TheActivity.SignUpStartTime = ChangeDictionary["signupBeginAt"] TheActivity.SignUpEndTime = ChangeDictionary["signupStopAt"] TheActivity.MinUser = ChangeDictionary["minUser"] TheActivity.MaxUser = ChangeDictionary["maxUser"] TheActivity.Type = ChangeDictionary["type"] TheActivity.StatusGlobal = ChangeDictionary["statusGlobal"] TheActivity.StatusJoin = ChangeDictionary["statusJoin"] TheActivity.StatusCheck = ChangeDictionary["statusCheck"] TheActivity.CanBeSearched = ChangeDictionary["canBeSearched"] TheActivity.GlobalRule = ChangeDictionary["ruleType"] TheActivity.Tags = ChangeDictionary["tags"] TheActivity.ImageURL = ChangeDictionary["imageUrl"] TheActivity.GPSPlace = ChangeDictionary["position"] TheActivity.save() TheSearcher = SearchAndRecommend.WhooshSearcher.Create() TheSearcher.UpdateOneInfo( Information["id"], TheActivity.Name + ',' + TheActivity.Tags) except: Success = False Reason = "修改数据失败" Code = Constants.ERROR_CODE_UNKNOWN if Success: try: #print(ChangeDictionary["rules"], Information["id"]) if ChangeDictionary["rules"] != []: #修改高级报名规则 if ChangeAdvancedRules(Information["id"], ChangeDictionary["rules"]) != True: Success = False Reason = "修改数据失败" Code = Constants.ERROR_CODE_UNKNOWN except: Success = False Reason = "修改数据失败" Code = Constants.ERROR_CODE_UNKNOWN if Success == False: Return["result"] = "fail" Return["reason"] = Reason Return["code"] = Code else: Return["result"] = "success" return Return
def AddActivity(ID, Information): ''' 描述:添加一个活动到数据库 参数:创建者ID,数据信息 返回:{result:success,activityId:xxx}/失败{result:fail,reason:xxx, code:xxx} ''' Return = {} Success = True Code = Constants.UNDEFINED_NUMBER Reason = "" CurTime = 0 StartTime = 0 EndTime = 0 StartSignTime = 0 StopSignTime = 0 TheMinUser = 0 TheMaxUser = 0 TheStatus = 0 TheSearched = False TheGlobalRule = -1 TheAcceptRule = [] TheAuditRule = [] TheRejectRule = [] TheType = "" #把时间转化为时间戳,并且判断时间和人数是否合法 if Success: try: CurTime = GlobalFunctions.GetCurrentTime() StartTime = GlobalFunctions.TimeStringToTimeStamp( Information["start"]) EndTime = GlobalFunctions.TimeStringToTimeStamp(Information["end"]) if 'signupBeginAt' in Information: StartSignTime = GlobalFunctions.TimeStringToTimeStamp( Information["signupBeginAt"]) else: StartSignTime = CurTime if 'signupStopAt' in Information: StopSignTime = GlobalFunctions.TimeStringToTimeStamp( Information["signupStopAt"]) else: StopSignTime = StartTime if 'minUser' in Information: TheMinUser = int(Information["minUser"]) else: TheMinUser = 0 if 'maxUser' in Information: TheMaxUser = int(Information["maxUser"]) else: TheMaxUser = Constants.UNDEFINED_NUMBER JudgeResult = JudgeValid.JudgeParameterValid( CurTime, StartTime, EndTime, StartSignTime, StopSignTime, 1, TheMinUser, TheMaxUser) if "imageUrl" in Information: TheImageURL = Information["imageUrl"] else: TheImageURL = "UNDEFINED" if "position" in Information: ThePosition = Information["position"] else: ThePosition = "" if JudgeResult["result"] != "success": Success = False Reason = JudgeResult["reason"] Code = Constants.ERROR_CODE_INVALID_CHANGE except: Success = False Reason = "输入的开始/结束/开始报名/报名截止时间不合理!" Code = Constants.ERROR_CODE_INVALID_PARAMETER #判断活动状态,类型等是否合法 if Success: try: TheType = Information["type"] TheSearched = bool(Information["canBeSearched"]) if JudgeValid.JudgeActivityTypeValid(TheType) == False: Success = False Reason = "活动类型不合法,添加活动失败!" Code = Constants.ERROR_CODE_INVALID_PARAMETER TheStatusGlobal = GlobalFunctions.GetStartActivityGlobalStatus( TheType) TheStatusJoin = GlobalFunctions.GetStartActivityJoinStatus( StartSignTime, StopSignTime) TheStatusCheck = GlobalFunctions.GetStartActivityCheckStatus( StartTime, EndTime) except: Success = False Reason = "参数不合法,添加活动失败!" Code = Constants.ERROR_CODE_INVALID_PARAMETER #获得标签,正文,头像 if Success: try: TheTagsList = Information["tags"] TheDescription = Information["description"] if JudgeValid.JudgeTagListValid(TheTagsList) != True: Success = False Reason = "参数不合法,标签不能带有英文逗号!" Code = Constants.ERROR_CODE_INVALID_PARAMETER TheTags = GlobalFunctions.MergeTags(TheTagsList) except: Success = False Reason = "参数不合法,添加活动失败!" Code = Constants.ERROR_CODE_INVALID_PARAMETER #获得高级报名信息 if Success: try: #判断是否合法 TheGlobalRule = Information["rules"]["ruleType"] if JudgeValid.JudgeRuleTypeValid(TheGlobalRule) != True: Success = False Reason = "参数不合法,规则类型不合法!" Code = Constants.ERROR_CODE_INVALID_PARAMETER TheJudgeAdvanceRuleResult = JudgeValid.JudgeAdvancedRuleValid( Information["rules"]) #print(TheJudgeAdvanceRuleResult) if TheJudgeAdvanceRuleResult["result"] != "success": Success = False Reason = TheJudgeAdvanceRuleResult["reason"] Code = TheJudgeAdvanceRuleResult["code"] try: TheAcceptRule = Information["rules"]["accept"] except: TheAcceptRule = [] try: TheAuditRule = Information["rules"]["needAudit"] except: TheAuditRule = [] try: TheRejectRule = Information["rules"]["reject"] except: TheRejectRule = [] except: Success = False Reason = "参数不合法,添加活动失败!" Code = Constants.ERROR_CODE_INVALID_PARAMETER #print(CurTime, StartTime, EndTime, StartSignTime, StopSignTime) #print(Success) #插入数据库 if Success: try: TheName = Information["name"] ThePlace = Information["place"] #TheCreator = User.objects.get(OpenID = ID) #print(TheCreator.ID) TheCreateTime = GlobalFunctions.GetCurrentTime() NewActivity = Activity.objects.create(Name = TheName, Place = ThePlace, CreateTime = TheCreateTime, StartTime = StartTime, EndTime = EndTime, SignUpStartTime = StartSignTime,\ SignUpEndTime = StopSignTime, MinUser = TheMinUser, MaxUser = TheMaxUser, CurrentUser = 0, Type = TheType, \ StatusGlobal = TheStatusGlobal, StatusJoin = TheStatusJoin, StatusCheck = TheStatusCheck, CanBeSearched = TheSearched, \ GlobalRule = TheGlobalRule, Tags = TheTags, Description = TheDescription, ImageURL = TheImageURL, GPSPlace = ThePosition) NewActivity.save() ActivityID = NewActivity.ID TheSearcher = SearchAndRecommend.WhooshSearcher.Create() TheSearcher.AddOneInfo(ActivityID, TheName + ',' + TheTags) except: Success = False Reason = "参数不合法,添加活动失败!" Code = Constants.ERROR_CODE_INVALID_PARAMETER if Success: if "rules" in Information: try: #添加rules for item in TheAcceptRule: AddAdvancedRules(ActivityID, item, Constants.ADVANCED_RULE_ACCEPT) for item in TheAuditRule: AddAdvancedRules(ActivityID, item, Constants.ADVANCED_RULE_AUDIT) for item in TheRejectRule: AddAdvancedRules(ActivityID, item, Constants.ADVANCED_RULE_REJECT) except: Success = False Reason = "参数不合法,添加活动失败!" Code = Constants.ERROR_CODE_INVALID_PARAMETER #print(Success) #让creator加入活动 if Success: try: #print(int(Return["id"])) JoinActivityResult = AddCreatorIntoActivity(ID, ActivityID) if JoinActivityResult["result"] != "success": Success = False Reason = JoinActivityResult["reason"] Code = JoinActivityResult["code"] else: Return["activityId"] = ActivityID except: Success = False Reason = "添加活动失败" Code = Constants.ERROR_CODE_UNKNOWN if Success: if TheStatusGlobal == Constants.ACTIVITY_STATUS_GLOBAL_NORMAL: Return["result"] = "success" else: Return["result"] = "needAudit" else: Return["result"] = "fail" Return["reason"] = Reason Return["code"] = Code return Return