Example #1
0
    def dispatch(self):
        user_id = self.request.GET.get('user_id')

        if user_id and self.request.has_perm('admin'):
            user = User.query.get(user_id)
        elif user_id:
            raise Forbidden()
        else:
            user = self.request.user
        form = UserEditForm(self.request.POST, obj=user)
        if self.request.method == 'POST' and form.validate():
            user.availability_link = form.availability_link.data or None
            user.tasks_link = form.tasks_link.data or None
            user.skype = form.skype.data or None
            user.phone = form.phone.data or None
            user.phone_on_desk = form.phone_on_desk.data or None
            user.irc = form.irc.data or None
            user.location = form.location.data or None
            user.start_work = form.start_work.data or None
            user.description = form.description.data or None
            user.date_of_birth = form.date_of_birth.data or None
            user.roles = form.roles.data
            if self.request.has_perm('admin'):
                user.is_active = form.is_active.data
                groups = form.groups.data
                if "freelancer" in groups:
                    groups.remove('freelancer')
                    user.freelancer = True
                else:
                    user.freelancer = False
                user.groups = groups
                user.start_full_time_work = form.start_full_time_work.data or None
                user.stop_work = form.stop_work.data or None
            if self.request.has_perm('admin'):
                user.employment_contract = form.employment_contract.data

            if form.avatar.data:
                preview = Preview(self.request)
                if not preview.swap_avatar(type='users', id=user.id):
                    self.flash(self._(u"No preview to swap"))

            self.flash(self._(u"User data saved"))
            LOG(u"User data saved")
            if user_id and self.request.has_perm('admin'):
                return HTTPFound(location=self.request.url_for(
                    '/user/edit', user_id=user_id))
            else:
                return HTTPFound(location=self.request.url_for('/user/edit'))

        if user.freelancer:
            form.groups.data = user.groups + ['freelancer']
        return dict(id=user.id, user=user, form=form)
Example #2
0
    def dispatch(self):
        user_id = self.request.GET.get('user_id')

        if user_id and self.request.has_perm('admin'):
            user = User.query.get(user_id)
        elif user_id:
            raise Forbidden()
        else:
            user = self.request.user
        form = UserEditForm(self.request.POST, obj=user)
        if self.request.method == 'POST' and form.validate():
            user.availability_link = form.availability_link.data or None
            user.tasks_link = form.tasks_link.data or None
            user.skype = form.skype.data or None
            user.phone = form.phone.data or None
            user.phone_on_desk = form.phone_on_desk.data or None
            user.irc = form.irc.data or None
            user.location = form.location.data or None
            user.start_work = form.start_work.data or None
            user.description = form.description.data or None
            user.date_of_birth = form.date_of_birth.data or None
            user.roles = form.roles.data
            if self.request.has_perm('admin'):
                user.is_active = form.is_active.data
                groups = form.groups.data
                if "freelancer" in groups:
                    groups.remove('freelancer')
                    user.freelancer = True
                else:
                    user.freelancer = False
                user.groups = groups
                user.start_full_time_work = form.start_full_time_work.data or None
                user.stop_work = form.stop_work.data or None
            if self.request.has_perm('admin'):
                user.employment_contract = form.employment_contract.data


            if form.avatar.data:
                preview = Preview(self.request)
                if not preview.swap_avatar(type='users', id=user.id):
                    self.flash(self._(u"No preview to swap"))

            self.flash(self._(u"User data saved"))
            LOG(u"User data saved")
            if user_id and self.request.has_perm('admin'):
                return HTTPFound(location=self.request.url_for('/user/edit', user_id=user_id))
            else:
                return HTTPFound(location=self.request.url_for('/user/edit'))

        if user.freelancer:
            form.groups.data = user.groups + ['freelancer']
        return dict(id=user.id, user=user, form=form)
Example #3
0
    def dispatch(self):
        user_id = self.request.GET.get('user_id')

        if user_id and self.request.has_perm('can_edit_users'):
            user = User.query.get(user_id)
        elif user_id:
            raise Forbidden()
        else:
            user = self.request.user

        if not user:
            raise HTTPNotFound()

        form = UserEditForm(self.request.POST, obj=user)
        if self.request.method == 'POST' and form.validate():
            user.availability_link = form.availability_link.data or None
            user.tasks_link = form.tasks_link.data or None
            user.skype = form.skype.data or None
            user.phone = form.phone.data or None
            user.phone_on_desk = form.phone_on_desk.data or None
            user.irc = form.irc.data or None
            user.location = form.location.data or None
            user.start_work = form.start_work.data or None
            user.description = form.description.data or None
            user.date_of_birth = form.date_of_birth.data or None
            user.roles = form.roles.data
            if self.request.has_perm('can_edit_users'):
                user.is_active = form.is_active.data
                user.start_work_experience = form.start_work_experience.data or None
                user.start_full_time_work = form.start_full_time_work.data or None
                user.stop_work = form.stop_work.data or None
                user.employment_contract = form.employment_contract.data

            if self.request.has_perm('can_add_user_to_group'):
                groups = form.groups.data

                if 'coordinator' in user.groups:
                    groups.append('coordinator')
                user.groups = groups

            if form.avatar.data:
                preview = Preview(self.request)
                if not preview.swap_avatar(type='users', id=user.id):
                    self.flash(self._(u"No preview to swap"))

            self.flash(self._(u"User data saved"))
            LOG(u"User data saved")
            if user_id and self.request.has_perm('can_edit_users'):
                return HTTPFound(location=self.request.url_for('/user/edit', user_id=user_id))
            else:
                return HTTPFound(location=self.request.url_for('/user/edit'))
        return dict(id=user.id, user=user, form=form)
Example #4
0
    def dispatch(self):
        user_id = self.request.GET.get("user_id")

        if user_id and self.request.has_perm("admin"):
            user = User.query.get(user_id)
        elif user_id:
            raise Forbidden()
        else:
            user = self.request.user
        form = UserEditForm(self.request.POST, obj=user)
        if self.request.method == "POST" and form.validate():
            user.availability_link = form.availability_link.data or None
            user.tasks_link = form.tasks_link.data or None
            user.skype = form.skype.data or None
            user.phone = form.phone.data or None
            user.phone_on_desk = form.phone_on_desk.data or None
            user.irc = form.irc.data or None
            user.location = form.location.data or None
            user.start_work = form.start_work.data or None
            user.description = form.description.data or None
            if form.level.data:
                user.levels = reduce(lambda x, y: x | y, [int(x) for x in form.level.data])
            if self.request.has_perm("admin"):
                user.is_active = form.is_active.data
                groups = form.groups.data
                if "freelancer" in groups:
                    groups.remove("freelancer")
                    user.freelancer = True
                else:
                    user.freelancer = False
                user.groups = groups
                user.start_full_time_work = form.start_full_time_work.data or None
            user.is_programmer = form.is_programmer.data
            user.is_frontend_developer = form.is_frontend_developer.data
            user.is_graphic_designer = form.is_graphic_designer.data

            if form.avatar.data:
                preview = Preview(self.request)
                if not preview.swap_avatar(type="users", id=user.id):
                    self.flash(self._(u"No preview to swap"))

            self.flash(self._(u"User data saved"))
            LOG(u"User data saved")
            if user_id and self.request.has_perm("admin"):
                return HTTPFound(location=self.request.url_for("/user/edit", user_id=user_id))
            else:
                return HTTPFound(location=self.request.url_for("/user/edit"))

        if user.freelancer:
            form.groups.data = user.groups + ["freelancer"]
        return dict(id=user.id, user=user, form=form)
Example #5
0
class Team(ApiView):
    def get(self):
        team_id = self.request.matchdict.get('team_id')
        team = Team_m.query.get(team_id)
        if team:
            return team.to_dict()
        else:
            raise HTTPNotFound()

    @has_perm('admin')
    def put(self):
        team_id = self.request.matchdict.get('team_id')
        team = Team_m.query.get(team_id)
        if not team:
            raise HTTPNotFound()

        try:
            json_team = self.request.json_body
        except ValueError:
            raise HTTPBadRequest('Expect json')

        team_schema = TeamUpdateSchema()
        try:
            team_des = team_schema.deserialize(json_team)
        except colander.Invalid, e:
            errors = e.asdict()
            raise HTTPBadRequest(errors)

        team.name = team_des.get('name') or team.name

        if 'users' in team_des:
            new_users = team_des['users']
            old_users = self.session.query(TeamMember.user_id).filter(
                TeamMember.team_id == team.id).all()
            users_delete = list(set(old_users) - set(new_users))
            users_add = list(set(new_users) - set(old_users))

            if users_delete:
                TeamMember.query.filter(TeamMember.team_id==team.id)\
                                .filter(TeamMember.user_id.in_(users_delete))\
                                .delete(synchronize_session=False)

            if users_add:
                self.session.add_all([
                    TeamMember(user_id=u_id, team_id=team.id)
                    for u_id in users_add
                ])

        if team_des.get('swap_with_preview'):
            preview = Preview(self.request)
            if not preview.swap_avatar(type='teams', id=team.id):
                raise HTTPBadRequest('No preview to swap')

        return HTTPOk("OK")
Example #6
0
class Teams(ApiView):

    def get(self):
        def get_worked_hours(startDate, endDate, projects_ids):
            worked_hours = DBSession.query(
                TimeEntry.project_id,
                func.sum(TimeEntry.time)
            )
            return worked_hours\
                .filter(TimeEntry.project_id.in_(projects_ids))\
                .filter(TimeEntry.date >= startDate)\
                .filter(TimeEntry.date <= endDate)\
                .group_by(TimeEntry.project_id)

        def get_project_worked_hours(project_id, worked_hours):
            worked_hours = filter(lambda x: x[0] == project_id, worked_hours)
            return worked_hours[0][1] if worked_hours else 0.0

        tickets_end_date = datetime.now()
        tickets_start_date = tickets_end_date.replace(day=1)
        tickets_end_date = tickets_end_date.replace(day=1)
        tickets_end_date += relativedelta(months=1)
        tickets_end_date -= relativedelta(days=1)

        tickets_last_month_end_date = tickets_start_date - timedelta(days=1)
        tickets_last_month_start_date = tickets_last_month_end_date.replace(
                                                                    day=1)

        teams = DBSession.query(Team_m, TeamMember.user_id)\
                            .outerjoin(TeamMember)

        team_to_project = DBSession.query(Team_m.id, Project, Client)\
                                      .filter(Sprint.team_id==Team_m.id)\
                                      .filter(Sprint.project_id==Project.id)\
                                      .filter(Project.client_id==Client.id)\
                                      .order_by(Sprint.end.desc())

        teams = h.groupby(
            teams,
            lambda x: x[0],
            lambda x: x[1]
        )
        team_to_project = h.groupby(
            team_to_project,
            lambda x: x[0],
            lambda x: x[1:]
        )

        projects_ids = []
        for value in team_to_project.values():
            for projectAndClient in value:
                projects_ids.append(projectAndClient[0].id)

        this_month_worked_hours = get_worked_hours(
            tickets_start_date, tickets_end_date, projects_ids)

        last_month_worked_hours = get_worked_hours(
            tickets_last_month_start_date, tickets_start_date,
            projects_ids)

        result = []
        for team, members in teams.iteritems():
            team = team.to_dict()
            team['users'] = members
            projects = team_to_project.get(team['id'], [])
            team['projects'] = [
                dict(
                    id=project.id,
                    name=project.name,
                    client=dict(
                        id=client.id,
                        name=client.name,
                    ),
                    this_month_worked_hours=
                        get_project_worked_hours(project.id,
                            this_month_worked_hours),
                    last_month_worked_hours=
                        get_project_worked_hours(project.id,
                            last_month_worked_hours)
                ) for project, client in projects
            ]
            result.append(team)

        return dict(
            teams=result
        )

    @has_perm('can_edit_teams')
    def post(self):
        try:
            json_team = self.request.json_body
        except ValueError:
            raise HTTPBadRequest('Expect json')

        team_schema = TeamAddSchema()
        try:
            team_des = team_schema.deserialize(json_team)
        except colander.Invalid, e:
            errors = e.asdict()
            raise HTTPBadRequest(errors)

        team = Team_m(name=team_des['name'])
        DBSession.add(team)
        try:
            DBSession.flush()
        except IntegrityError:
            raise HTTPBadRequest('Team exists')

        if team_des.get('swap_with_preview'):
            preview = Preview(self.request)
            if not preview.swap_avatar(type='teams', id=team.id):
                raise HTTPBadRequest('No preview to swap')

        return team.to_dict()