def createproject_view(request):
    session = request.session
    username = session.get("user")
    userid = session.get("userid")
    if username is None:
        return HTTPFound('/login')
    if request.method == 'POST':
        data = request.POST
        if "projectTitle" in data and "projectDescription" in data and "projectStart" in data and "projectEnd" in data:
            ptitle = data.get('projectTitle')
            pdescription = data.get('projectDescription')
            pstart = data.get('projectStart')
            pend = data.get('projectEnd')
            parsedstart = utils.tryParseDate(pstart)
            parsedend = utils.tryParseDate(pend)
            if parsedstart is None or parsedend is None:
                return HTTPFound('/projectcreate')
            newproject = projektModel(name = ptitle, description = pdescription, start = parsedstart, end = parsedend)
            created = database.writeNewProject(userid, newproject)
            if created is not 0:
                print("Error: " + replyCodes[created])
                return HTTPFound('/projectcreate')
	    return HTTPFound('/dashboard')
	    
    return{}
def projectmodify_view(request):
    session = request.session
    username = session.get("user")
    userid = session.get("userid")
    projektid = request.matchdict['projektid']
    if username is None:
        return HTTPFound('/login')
      
    projekt = database.readProjectByProjectid(userid, projektid)
    if type(projekt) is int:
        print("Error: " + replyCodes[projekt])
        return HTTPFound('/projectdetails/' + projektid)
      
    if request.method == 'POST':
        data = request.POST
        projectM = projektModel()
        projectM.id = projekt.id

        if "projectTitle" in data:
            projectM.name = data.get("projectTitle")
        else:
            projectM.name = projekt.name
            
        if "projectDescription" in data:
            projectM.description = data.get("projectDescription")
        else:
            projectM.description = projekt.description
        
        if "projectStart" in data:
            projectM.start = utils.tryParseDate(data.get("projectStart"))
        else:
            projectM.start = projekt.start
            
        if "projectEnd" in data:
            projectM.end = utils.tryParseDate(data.get("projectEnd"))
        else:
            projectM.end = projekt.end

        if not utils.tryParseDate(projectM.start) or not utils.tryParseDate(projectM.end):
            return HTTPFound('/projectdetails/' + projektid)

        changed = database.changeProjectInfo(userid, projectM)
        if changed > 0:
            print("Error: " + replyCodes[changed])
        
    return{'projekt' : projekt}
def sprintcreate_view(request):
    session = request.session
    username = session.get("user")
    userid = session.get("userid")
    activeprojectid = session.get("activeprojectid")
    if username is None:
        return HTTPFound('/login')
    if request.method == 'POST':
        data = request.POST
        if "sprintStart" in data and "sprintEnd" in data :
            parsedstart = utils.tryParseDate(data.get('sprintStart'))
            parsedend = utils.tryParseDate(data.get('sprintEnd'))
            if parsedstart is None or parsedend is None:
                return HTTPFound('/sprintcreate')
        sprint = database.writeNewSprint(userid, activeprojectid, parsedstart, parsedend)
        if sprint is not 0:
            print("Error: " + replyCodes[sprint])
            return HTTPFound('/sprintcreate')
        return HTTPFound('/scrumboard/' + activeprojectid)

    return{'done' : 'done'}
def sprintmodify_view(request):
    session = request.session
    username = session.get("user")
    userid = session.get("userid")
    projektid = request.matchdict['projektid']
    sprintnumber = request.matchdict['sprintnumber']
    if username is None:
        return HTTPFound('/login')
    sprint = database.readSprintOfProject(userid, projektid, sprintnumber)
    if type(sprint) is int:
        print("Error " + replyCodes[sprint])
        return HTTPFound("/projectdetail/" + projektid)
    
    if request.method == 'POST':
        data = request.POST
        sprintM = sprintModel()
        sprintM.id = sprint.id
        sprintM.projektid = sprint.projektid
        sprintM.number = sprint.number

        if "sprintStart" in data:
            sprintM.start = utils.tryParseDate(data.get("sprintStart"))
        else: 
            sprintM.start = sprint.start

        if "sprintEnd" in data:
            sprintM.end = utils.tryParseDate(data.get("sprintEnd"))
        else: 
            sprintM.end = sprint.end

        if not utils.tryParseDate(sprintM.start) or not utils.tryParseDate(sprintM.end):
            return HTTPFound('/projectdetails/' + projektid)

        database.changeSprintInfo(userid, sprintM)
        
    return{'sprint' : sprint, 'projektid' : projektid}
def __getIsInProjectPeriod(session, checkedDate, projectId):
    if type(checkedDate) is unicode or type(checkedDate) is str:
        checkedDate = utils.tryParseDate(checkedDate)
    if checkedDate == None:
        return 72

    project = __getProjectById(session, projectId)
    if type(project) is int:
        return project

    if checkedDate < project.start:
        return 70
    if checkedDate > project.end:
        return 70
    return True