def sprintdelete_view(request):
    projektid = request.matchdict['projektid']
    sprintnumber = request.matchdict['sprintnumber']
    session = request.session
    username = session.get("user")
    userid = session.get("userid")
    if username is None:
        return HTTPFound('/login')
      
    sprint = database.readSprintOfProject(userid, int(projektid), int(sprintnumber))
    if type(sprint) is int:
        print("Error:" + replyCodes[sprint])
        return HTTPFound('/scrumboard/' + projektid + '/' + sprintnumber)
    projekt = database.readProjectByProjectid(userid, projektid)
    if type(projekt) is int:
        print("Error: " + replyCodes[projekt])
        return HTTPFound('/scrumboard/' + projektid + '/' + sprintnumber)
      
    if request.method == 'POST':
        data = request.POST
        deleted = database.deleteSprint(userid, sprint.id)
        if deleted > 0:
	    print("Error: " + replyCodes[deleted])
	return HTTPFound('/projectdetails/' + projektid)
        
    return{'sprint' : sprint, 'projekt' : projekt}
def chart_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')
      
    projekt = database.readProjectByProjectid(userid, projektid)
    if type(projekt) is int:
        print("Error! " + replyCodes[projekt])
        return HTTPFound("/scrumboard/" + projektid + "/" + sprintnumber)
      
    sprint = database.readSprintOfProject(userid, projektid, sprintnumber)
    if type(sprint) is int:
        print("Error! " + replyCodes[sprint])
        return HTTPFound("/scrumboard/" + projektid + "/" + sprintnumber)  
      
    tickets = database.readTicketsOfSprint(userid, projektid, sprintnumber)
    if type(tickets) is int:
        print("Error! " + replyCodes[tickets])
        return HTTPFound("/scrumboard/" + projektid + "/" + sprintnumber)
      
    totaleffort = 0
    for ticket in tickets:
        totaleffort = totaleffort + ticket.effort
        
    dateEffortValues = []
    curDate = sprint.start
    curEffortHeight = totaleffort
    while curDate <= sprint.end:
        for t in tickets:
	    if curDate == t.finished:
	        curEffortHeight = curEffortHeight - t.effort
        dateEffortValues.append({
	    'remainingEffort':curEffortHeight,
	    'datum':str(curDate.day) + '.' + str(curDate.month)
        })
        curDate = curDate + timedelta(days=1)
        
    ticketdata = {}
    ticketdata['totaleffort'] = totaleffort
    ticketdata['dateEffortValues'] = dateEffortValues
    ticketdata['daysNum'] = len(ticketdata['dateEffortValues'])
        
    return{'projekt' : projekt, 'sprint' : sprint, 'tickets' : tickets, 'ticketdata' : ticketdata}
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}