Beispiel #1
0
class PlanHandler(tornado.web.RequestHandler):
    def initialize(self):
        self.dataController = DataController(self.application.con)

    def get(self):
        userId = getUserIdFromCookie(self)
        if not userId:
            self.redirect("/login", permanent=True)
            return
        planId = self.get_argument("planId", None)
        user, supervisors, superviseds, planNum, completedPlanNum, unReadMessageNum = self.dataController.getUserInfoByUserId(
            userId
        )
        if not planId:
            plan = dict()
            plan["id"] = 0
            plan["name"] = ""
            plan["startTime"] = ""
            plan["endTime"] = ""
            plan["completingRate"] = 100
            planSupervisors = list()
            comments = list()
            stages = list()
            usersForComments = list()
            """
			supervisors = list()
			superviseds = list()
			completedNum = 0
			plans = list()
			"""
            self.render(
                "plan.html",
                userOfPlan=user,
                usersForComments=usersForComments,
                plan=plan,
                planSupervisors=planSupervisors,
                stages=stages,
                comments=comments,
                user=user,
                planNum=planNum,
                mySupervisors=supervisors,
                mySuperviseds=superviseds,
                completedPlanNum=completedPlanNum,
                unReadMessageNum=unReadMessageNum,
            )
        else:
            plan = self.dataController.getPlanById(planId)
            userOfPlan = self.dataController.getUserById(plan["userId"])
            canAccess = False
            if plan:
                if plan["userId"] == userId:
                    canAccess = True
                else:
                    srs = self.dataController.getSrsByPlanId(planId)
                    for sr in srs:
                        if sr["userId"] == userId:
                            canAccess = True
                            break
            if canAccess:
                stages = self.dataController.getStagesByPlanId(planId)
                comments = self.dataController.getCommentsByPlanId(planId)

                usersForComments = list()
                for comment in comments:
                    comment["commentTime"] = time.strftime("%Y-%m-%d", time.localtime(comment["commentTime"]))
                    usersForComments.append(self.dataController.getUserById(comment["userId"]))
                if not plan["name"]:
                    plan["name"] = ""
                if not plan["startTime"]:
                    plan["startTime"] = ""
                else:
                    plan["startTime"] = time.strftime("%Y-%m-%d", time.localtime(plan["startTime"]))
                if not plan["endTime"]:
                    plan["endTime"] = ""
                else:
                    plan["endTime"] = time.strftime("%Y-%m-%d", time.localtime(plan["endTime"]))
                plan["completingRate"] = 100
                if plan["unCompletedStageNum"] != 0:
                    plan["completingRate"] = int(
                        round(
                            float(plan["completedStageNum"])
                            / (plan["completedStageNum"] + plan["unCompletedStageNum"])
                            * 100
                        )
                    )

                srs = self.dataController.getSrsByPlanId(planId)
                planSupervisors = list()
                for sr in srs:
                    planSupervisors.append(self.dataController.getUserById(sr["userId"]))

                self.render(
                    "plan.html",
                    userOfPlan=userOfPlan,
                    usersForComments=usersForComments,
                    plan=plan,
                    planSupervisors=planSupervisors,
                    stages=stages,
                    comments=comments,
                    user=user,
                    planNum=planNum,
                    mySupervisors=supervisors,
                    mySuperviseds=superviseds,
                    completedPlanNum=completedPlanNum,
                    unReadMessageNum=unReadMessageNum,
                )

            else:
                self.write("你没有访问该计划的权利")

    def post(self):
        userId = getUserIdFromCookie(self)
        if not userId:
            self.redirect("/login", permanent=True)
            return
        planId = int(self.get_argument("planId", ""))
        isSave = int(self.get_argument("save", 0))
        if isSave:
            plan = dict()
            plan["name"] = self.get_argument("name", "")
            plan["startTime"] = time.mktime(time.strptime(self.get_argument("startTime", ""), "%Y-%m-%d"))
            plan["endTime"] = time.mktime(time.strptime(self.get_argument("endTime", ""), "%Y-%m-%d"))
            plan["userId"] = userId
            if planId:
                plan["id"] = planId
            else:
                plan["id"] = self.dataController.createPlan(plan)
                planId = plan["id"]

            completedStageNum = 0
            unCompletedStageNum = 0

            stagesId = map(int, self.get_arguments("stageId", ""))
            stagesContent = self.get_arguments("stageContent", "")
            stagesState = map(int, self.get_arguments("stageState", ""))
            stagesDelete = map(int, self.get_arguments("stageDelete", ""))
            stages = list()
            if (
                len(stagesId) == len(stagesContent)
                and len(stagesContent) == len(stagesState)
                and len(stagesState) == len(stagesDelete)
                and len(stagesState) > 0
            ):
                stage = dict()
                stage["planId"] = planId
                for index in range(len(stagesId)):
                    if stagesDelete[index] == 1:
                        self.dataController.deleteStageById(stagesId[index])
                    else:
                        stage["content"] = stagesContent[index]
                        stage["state"] = stagesState[index]
                        if stagesId[index] == 0:
                            stage["id"] = self.dataController.createStage(stage)
                        else:
                            stage["id"] = stagesId[index]
                            self.dataController.updateStage(stage)
                        if stage["state"] == 1:
                            completedStageNum += 1
                        else:
                            unCompletedStageNum += 1

            plan["completedStageNum"] = completedStageNum
            plan["unCompletedStageNum"] = unCompletedStageNum
            self.dataController.updatePlan(plan)

            supervisorIds = map(int, self.get_arguments("supervisorId", ""))
            sr = dict()
            message = dict()
            sr["planId"] = planId
            message["planId"] = planId
            message["sid"] = userId
            message["state"] = 0
            for supervisor in supervisorIds:
                if supervisor:
                    sr["userId"] = supervisor
                    self.dataController.createSr(sr)
                    message["rid"] = supervisor
                    self.dataController.createMessage(message)
            self.redirect("/plan?planId=" + str(planId), permanent=True)

        else:
            self.dataController.deleteStagesByPlanId(planId)
            self.dataController.deleteCommentsByPlanId(planId)
            self.dataController.deleteSrsByPlanId(planId)
            self.dataController.deleteMessagesByPlanId(planId)
            self.dataController.deletePlanById(planId)
            self.redirect("/index", permanent=True)
Beispiel #2
0
class PlanHandler(tornado.web.RequestHandler):
	def initialize(self):
		self.dataController = DataController(self.application.con)

	def get(self):
		userId = getUserIdFromCookie(self)
		if not userId:
			self.redirect('/login', permanent=True)
			return
		planId = self.get_argument('planId', None)
		user, supervisors, superviseds, planNum, completedPlanNum, \
			unReadMessageNum = self.dataController.getUserInfoByUserId(userId)
		if not planId:
			plan = dict()
			plan['id'] = 0
			plan['name'] = ''
			plan['startTime'] = ''
			plan['endTime'] = ''
			plan['completingRate'] = 100
			planSupervisors = list()
			comments = list()
			stages = list()
			usersForComments = list()
			'''
			supervisors = list()
			superviseds = list()
			completedNum = 0
			plans = list()
			'''
			self.render('plan.html', userOfPlan=user, 
				usersForComments=usersForComments, 
				plan=plan, planSupervisors=planSupervisors, 
				stages=stages, comments=comments, 
				user=user, planNum=planNum, 
				mySupervisors=supervisors, mySuperviseds=superviseds, 
				completedPlanNum=completedPlanNum, unReadMessageNum=unReadMessageNum)
		else:
			plan = self.dataController.getPlanById(planId)
			userOfPlan = self.dataController.getUserById(plan['userId'])
			canAccess = False
			if plan:
				if plan['userId'] == userId:
					canAccess = True
				else:
					srs = self.dataController.getSrsByPlanId(planId)
					for sr in srs:
						if sr['userId'] == userId:
							canAccess = True
							break
			if canAccess:
				stages = self.dataController.getStagesByPlanId(planId)
				comments = self.dataController.getCommentsByPlanId(planId)

				usersForComments = list()
				for comment in comments:
					comment['commentTime'] = time.strftime("%Y-%m-%d", time.localtime(comment['commentTime']))
					usersForComments.append(self.dataController.getUserById(comment['userId']))
				if not plan['name']:
					plan['name'] = ''
				if not plan['startTime']:
					plan['startTime'] = ''
				else:
					plan['startTime'] = time.strftime("%Y-%m-%d", time.localtime(plan['startTime']))
				if not plan['endTime']:
					plan['endTime'] = ''
				else:
					plan['endTime'] = time.strftime("%Y-%m-%d", time.localtime(plan['endTime']))
				plan['completingRate'] = 100
				if plan['unCompletedStageNum'] != 0:
					plan['completingRate'] = int(round(float(plan['completedStageNum']) \
						/ (plan['completedStageNum'] + plan['unCompletedStageNum']) * 100))

				srs = self.dataController.getSrsByPlanId(planId)
				planSupervisors = list()
				for sr in srs:
					planSupervisors.append(self.dataController.getUserById(sr['userId']))

				
				self.render('plan.html', userOfPlan=userOfPlan, 
					usersForComments=usersForComments, 
					plan=plan, planSupervisors=planSupervisors, 
					stages=stages, comments=comments, 
					user=user, planNum=planNum, 
					mySupervisors=supervisors, mySuperviseds=superviseds, 
					completedPlanNum=completedPlanNum, unReadMessageNum=unReadMessageNum)

			else:
				self.write('你没有访问该计划的权利')

	def post(self):
		userId = getUserIdFromCookie(self)
		if not userId:
			self.redirect('/login', permanent=True)
			return
		planId = int(self.get_argument('planId', ''))
		isSave = int(self.get_argument('save', 0))
		if isSave:
			plan = dict()
			plan['name'] = self.get_argument('name', '')
			plan['startTime'] = time.mktime(time.strptime(self.get_argument('startTime', ''),'%Y-%m-%d'))
			plan['endTime'] = time.mktime(time.strptime(self.get_argument('endTime', ''),'%Y-%m-%d'))
			plan['userId'] = userId
			if planId:
				plan['id'] = planId
			else:
				plan['id'] = self.dataController.createPlan(plan)
				planId = plan['id']

			completedStageNum = 0
			unCompletedStageNum = 0

			stagesId = map(int, self.get_arguments('stageId', ''))
			stagesContent = self.get_arguments('stageContent', '')
			stagesState = map(int, self.get_arguments('stageState', ''))
			stagesDelete = map(int, self.get_arguments('stageDelete', ''))
			stages = list()
			if len(stagesId) == len(stagesContent) \
			and len(stagesContent) == len(stagesState) \
			and len(stagesState) == len(stagesDelete) \
			and len(stagesState) > 0:
				stage = dict()
				stage['planId'] = planId
				for index in range(len(stagesId)):
					if stagesDelete[index] == 1:
						self.dataController.deleteStageById(stagesId[index])
					else:
						stage['content'] = stagesContent[index]
						stage['state'] = stagesState[index]
						if stagesId[index] == 0:
							stage['id'] = self.dataController.createStage(stage)
						else:
							stage['id'] = stagesId[index]
							self.dataController.updateStage(stage)
						if stage['state'] == 1:
							completedStageNum += 1
						else:
							unCompletedStageNum += 1

			plan['completedStageNum'] = completedStageNum
			plan['unCompletedStageNum'] = unCompletedStageNum
			self.dataController.updatePlan(plan)

			supervisorIds = map(int, self.get_arguments('supervisorId', ''))
			sr = dict()
			message = dict()
			sr['planId'] = planId
			message['planId'] = planId
			message['sid'] = userId
			message['state'] = 0
			for supervisor in supervisorIds:
				if supervisor:
					sr['userId'] = supervisor
					self.dataController.createSr(sr)
					message['rid'] = supervisor
					self.dataController.createMessage(message)
			self.redirect('/plan?planId='+str(planId), permanent=True)

		else:
			self.dataController.deleteStagesByPlanId(planId)
			self.dataController.deleteCommentsByPlanId(planId)
			self.dataController.deleteSrsByPlanId(planId)
			self.dataController.deleteMessagesByPlanId(planId)
			self.dataController.deletePlanById(planId)
			self.redirect('/index', permanent=True)