project1 = Project(project_id='9G90401', summary='煤气化装置项目', isassociated=False, isreserved=False, doclink='//fileserver2/platform/9G90401', pm=person1, projecttype=type1) project1.save() project2 = Project(project_id='8H10001', summary='浙大八通道', isassociated=False, isreserved=False, doclink='//fileserver2/platform/8H10001', pm=person3, projecttype=type1) project2.save() project3 = Project(project_id='有薪假', summary='Paid leave', isassociated=False, isreserved=False, doclink='//fileserver/Devision/HR', pm=person_admin, projecttype=type5) project3.save() t1 = TaskTime(employee=person1, t_hours=1.0, t_percentage=0.1, workday=date.today(), project=project1) t1.save() t2 = TaskTime(employee=person1, t_hours=1.6, t_percentage=0.2, workday=date.today(), project=project2) t2.save() t3 = TaskTime(employee=person1, t_hours=1.6, t_percentage=0.2, workday=date.today(), project=project3) t3.save() print 'Database creation successful'
def timeline(request, year, month, week=0): """Renders the timeline page.""" assert isinstance(request, HttpRequest) if request.method == 'POST': result = validate_task(request) if result[0]: return HttpResponseRedirect('/error/') else: # update the database for key, value in result[1].iteritems(): day_project = key.split(DELIMITER) # day string format: 2016 Fri, Oct 21 # retrieve year from 'year' parameter datetime0 = datetime.strptime('%s %s' % (year, day_project[0]), FORM_DATE_FORMAT) day = datetime(datetime0.year, datetime0.month, datetime0.day) project = Project.objects.get(project_id=day_project[1]) percentage = value # Retrieving a single object with get() user = Person.objects.get(user=request.user) t_percentage = percentage / 100.0 t_hours = float('%.1f' % (t_percentage * DAY_WORKING_HOURS)) # INSERT or UPDATE a database row try: tm = TaskTime.objects.get(workday=day, project=project) tm.t_hours = t_hours tm.t_percentage = t_percentage except TaskTime.DoesNotExist: # add a new row in DB tm = TaskTime(employee=user, t_hours=t_hours, t_percentage=t_percentage, workday=day, project=project) except TaskTime.MultipleObjectsReturned: return HttpResponseRedirect('/error/') tm.save() return HttpResponseRedirect('/thanks/') else: weeks = calendar.monthcalendar(int(year), int(month)) weeknumber = int(week) if 0 == weeknumber or weeknumber > len(weeks): weeknumber = -1 else: weeknumber -= 1 # Find the first day in one week of this month like [0, 0, 0, 0, 1, 2, 3] first = 0 for day in range(0, WEEK_DAYS_NUM, 1): if 0 != weeks[weeknumber][day]: first = day break # Make a valid week range which may cover the last month or the next month. monday = datetime(int(year), int(month), weeks[weeknumber][first]) - timedelta(days=first) weekdays = [monday] for day in range(1, WEEK_DAYS_NUM, 1): weekdays.append(monday + timedelta(days=day)) tasks = TaskTime.objects.filter(employee__user__username=request.user.username, workday__gte=weekdays[0], workday__lte=weekdays[-1]) # If there is no task, then we create empty tasks in the browser. task_num = len(tasks) empty_task_num = PLACEHOLD_TASKTIME_NUM if 0 == task_num: task_num = PLACEHOLD_TASKTIME_NUM else: empty_task_num = 0 return render( request, 'app/timeline.html', { 'title': 'About', 'message': 'Your timeline page', 'year': datetime.now().year, 'tasks': tasks, 'projects': Project.objects.all(), 'weekdays': weekdays, 'projectID': create_names(task_num, PROJECT_ELEMENT_NAME, ELEMENT_ID_SURNAME), 'dayID': create_names(task_num, DAY_ELEMENT_NAME, ELEMENT_ID_SURNAME), 'timeID': create_names(task_num, TASKTIME_ELEMENT_NAME, ELEMENT_ID_SURNAME), 'projectName': create_names(task_num, PROJECT_ELEMENT_NAME, ELEMENT_SURNAME), 'dayName': create_names(task_num, DAY_ELEMENT_NAME, ELEMENT_SURNAME), 'timeName': create_names(task_num, TASKTIME_ELEMENT_NAME, ELEMENT_SURNAME), 'emptyTasks': range(empty_task_num), } )