Ejemplo n.º 1
0
    def post(self):
        absence = self.request.json.get('absence')
        form = AbsentApplicationForm(MultiDict(**absence), request=self.request)
        if form.validate():
            response = {
                u'request': False,
                u'hours': False,
                u'calendar_entry': False,
            }

            memcache.clear()

            date_start = form.popup_date_start.data
            date_end = form.popup_date_end.data
            days = 0
            if date_start and date_end:
                days = h.get_working_days(date_start, date_end)
            type = form.popup_type.data
            remarks = form.popup_remarks.data
            absence = Absence(
                user_id=self.request.user.id,
                date_start=date_start,
                date_end=date_end,
                days=days,
                type=type,
                remarks=remarks,
            )

            DBSession.add(absence)
            memcache.delete(MEMCACHED_NOTIFY_KEY % date_start)

            if absence.type != 'inne':
                holidays = Holiday.all()
                date = date_start
                oneday = datetime.timedelta(days=1)
                description = self._(
                    u'Auto Leave: ${type} - ${remarks}',
                    type=dict(ABSENCE_TYPES)[type],
                    remarks=remarks
                )
                project_id = L4_PROJECT_ID if type == u'l4' else LEAVE_PROJECT_ID

                while date <= date_end:
                    if not Holiday.is_holiday(date, holidays=holidays):
                        timeentry = TimeEntry(
                            user_id=self.request.user.id,
                            date=date,
                            time=8,
                            description=description,
                            project_id=project_id,
                        )
                        DBSession.add(timeentry)
                    date += oneday

                self._send_email(
                    absence.type,
                    date_start.strftime('%Y-%m-%d'),
                    date_end.strftime('%Y-%m-%d'),
                    days,
                    remarks,
                )

                response[u'request'] = True
                response[u'hours'] = True

            calendar = cal.Calendar(self.request.user)
            event = cal.Event(
                date_start,
                date_end + cal.oneday,
                u'NIEOB-[%s]' % self.request.user.name,
                remarks
            )
            event_id = calendar.addEvent(event)

            response[u'calendar_entry'] = bool(event_id)

            return response

        self.request.response.status = 400
        return form.errors
Ejemplo n.º 2
0
    def post(self):
        absence = self.request.json.get('absence')
        form = AbsentApplicationForm(MultiDict(**absence),
                                     request=self.request)
        if form.validate():
            response = {
                u'request': False,
                u'hours': False,
                u'calendar_entry': False,
            }

            memcache.clear()

            date_start = form.popup_date_start.data
            date_end = form.popup_date_end.data
            days = 0
            if date_start and date_end:
                days = h.get_working_days(date_start, date_end)
            type = form.popup_type.data
            remarks = form.popup_remarks.data
            absence = Absence(
                user_id=self.request.user.id,
                date_start=date_start,
                date_end=date_end,
                days=days,
                type=type,
                remarks=remarks,
            )

            DBSession.add(absence)
            memcache.delete(MEMCACHED_NOTIFY_KEY % date_start)

            if absence.type != 'inne':
                holidays = Holiday.all()
                date = date_start
                oneday = datetime.timedelta(days=1)
                description = self._(u'Auto Leave: ${type} - ${remarks}',
                                     type=dict(ABSENCE_TYPES)[type],
                                     remarks=remarks)
                project_id = L4_PROJECT_ID if type == u'l4' else LEAVE_PROJECT_ID

                while date <= date_end:
                    if not Holiday.is_holiday(date, holidays=holidays):
                        timeentry = TimeEntry(
                            user_id=self.request.user.id,
                            date=date,
                            time=8,
                            description=description,
                            project_id=project_id,
                        )
                        DBSession.add(timeentry)
                    date += oneday

                self._send_email(
                    absence.type,
                    date_start.strftime('%Y-%m-%d'),
                    date_end.strftime('%Y-%m-%d'),
                    days,
                    remarks,
                )

                response[u'request'] = True
                response[u'hours'] = True

            calendar = cal.Calendar(self.request.user)
            event = cal.Event(date_start, date_end + cal.oneday,
                              u'NIEOB-[%s]' % self.request.user.name, remarks)
            event_id = calendar.addEvent(event)

            response[u'calendar_entry'] = bool(event_id)

            return response

        self.request.response.status = 400
        return form.errors
Ejemplo n.º 3
0
    def dispatch(self):
        form = AbsentApplicationForm(self.request.POST, request=self.request)
        days, mandated, used, left = 0, 0, 0, 0
        if form.popup_date_start.data:
            mandated, used, left = user_leave(self.request, form.popup_date_start.data.year)
            if form.popup_date_end.data:
                days = h.get_working_days(form.popup_date_start.data, form.popup_date_end.data)
                left -= days
        if self.request.method == 'POST' and form.validate():
            memcache.clear()
            response = u''
            date_start = form.popup_date_start.data
            date_end = form.popup_date_end.data
            type = form.popup_type.data
            remarks = form.popup_remarks.data
            absence = Absence(
                user_id=self.request.user.id,
                date_start=date_start,
                date_end=date_end,
                days=days,
                type=type,
                remarks=remarks,
            )
            memcache.delete(MEMCACHED_NOTIFY_KEY % date_start)
            self.session.add(absence)

            if absence.type != 'inne':
                # let's add timeentries for this leave
                holidays = Holiday.all()
                date = date_start
                oneday = datetime.timedelta(days=1)
                description = self._(u'Auto Leave: ${type} - ${remarks}',
                    type=dict(ABSENCE_TYPES)[type],
                    remarks=remarks
                )
                project_id = L4_PROJECT_ID if type == u'l4' else LEAVE_PROJECT_ID

                while date <= date_end:
                    if not Holiday.is_holiday(date, holidays=holidays):
                        timeentry = TimeEntry(
                            user_id=self.request.user.id,
                            date=date,
                            time=8,
                            description=description,
                            project_id=project_id,
                        )
                        self.session.add(timeentry)
                    date += oneday

                ## let's send email
                deferred = self._send_mail(
                    absence.type,
                    date_start.strftime('%Y-%m-%d'),
                    date_end.strftime('%Y-%m-%d'),
                    days,
                    remarks,
                )
                response += self._(u'Request added<br>Hours added<br>')

            ## and add event to calendar:
            calendar = cal.Calendar(self.request.user)
            event = cal.Event(
                date_start,
                date_end+cal.oneday,
                u'NIEOB-[%s]' % self.request.user.name,
                remarks,
            )
            event_id = calendar.addEvent(event)

            if event_id:
                response += self._(u'Calendar entry has been added')
            else:
                response += u'Calendar entry has <b class="red">NOT</b> beed added'

            return Response(response)

        return dict(
            form=form,
            days=days,
            mandated=mandated,
            used=used,
            left=left
        )