OpenProject is the leading open source project management software.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
openproject/lib/api/v3/projects/project_representer.rb

232 lines
7.8 KiB

#-- encoding: UTF-8
#-- copyright
# OpenProject is a project management system.
# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
# Copyright (C) 2006-2017 Jean-Philippe Lang
# Copyright (C) 2010-2013 the ChiliProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# See docs/COPYRIGHT.rdoc for more details.
#++
require 'roar/decorator'
require 'roar/json/hal'
module API
module V3
module Projects
class ProjectRepresenter < ::API::Decorators::Single
include API::Decorators::LinkedResource
include API::Decorators::DateProperty
include ::API::Caching::CachedRepresenter
include API::Decorators::FormattableProperty
extend ::API::V3::Utilities::CustomFieldInjector::RepresenterClass
cached_representer key_parts: %i(status),
disabled: false
self_link
def from_hash(body)
# Representable is broken when passing nil as parameters
# it will set the property :status and :statusExplanation
# regardless of what the setter actually does
# Bug opened at https://github.com/trailblazer/representable/issues/234
super(body).tap do |struct|
next unless struct.respond_to?(:status_attributes)
# Set the status attribute properly
struct.status = struct.status_attributes
# Remove temporary attributes workaround
struct.delete_field(:status_attributes)
# Remove nil status_explanation when passed as nil
if struct.respond_to?(:status_explanation)
struct.delete_field(:status_explanation)
end
end
end
link :createWorkPackage,
cache_if: -> { current_user_allowed_to(:add_work_packages, context: represented) } do
{
href: api_v3_paths.create_project_work_package_form(represented.id),
method: :post
}
end
link :createWorkPackageImmediately,
cache_if: -> { current_user_allowed_to(:add_work_packages, context: represented) } do
{
href: api_v3_paths.work_packages_by_project(represented.id),
method: :post
}
end
link :workPackages,
cache_if: -> {
current_user_allowed_to(:view_work_packages, context: represented)
} do
{ href: api_v3_paths.work_packages_by_project(represented.id) }
end
link :categories do
{ href: api_v3_paths.categories_by_project(represented.id) }
end
link :versions,
cache_if: -> {
current_user_allowed_to(:view_work_packages, context: represented) ||
current_user_allowed_to(:manage_versions, context: represented)
} do
{ href: api_v3_paths.versions_by_project(represented.id) }
end
link :memberships,
cache_if: -> {
current_user_allowed_to(:view_members, context: represented)
} do
{
href: api_v3_paths.path_for(:memberships, filters: [{ project: { operator: "=", values: [represented.id.to_s] }}]),
}
end
link :types,
cache_if: -> {
current_user_allowed_to(:view_work_packages, context: represented) ||
current_user_allowed_to(:manage_types, context: represented)
} do
{ href: api_v3_paths.types_by_project(represented.id) }
end
link :update,
cache_if: -> {
current_user_allowed_to(:edit_project, context: represented)
} do
{
href: api_v3_paths.project_form(represented.id),
method: :post
}
end
link :updateImmediately,
cache_if: -> {
current_user_allowed_to(:edit_project, context: represented)
} do
{
href: api_v3_paths.project(represented.id),
method: :patch
}
end
link :delete,
cache_if: -> { current_user.admin? } do
{
href: api_v3_paths.project(represented.id),
method: :delete
}
end
[31023] Preparation for project edit fields: Generalize edit and display fields currently work package specific (#7726) * Check edit fields for Work package dependencies * Make EditForm, NotificationService, EditContext indepenedent of Work Packages * Make EventsService independent of WorkPackages * Start renaming WpNotificationService (WiP) * Fix more references * Fix typescript errors * Add basic halEditingService * Rename to global halResourceEditingService (WiP) * Move typing from class to methods (WiP) * Fix typescript errors * Remove space in type * Add test project widget && generalize the editFieldGroup * Rename editing portal service [ci skip] * Rename WpEditFieldComponent * Rename WpDisplayFields * Rename display-XX-field to XX-display-field [ci skip] * Add WP specific ID field to distinguish between resources * Re-add state in work package resource * Generalize display field renderer * Rename spent-time to wp-spent-time and fix highlight specifics [ci skip] * Actually load the project schema and make field editable * Make edit-field-group.component an edit-form.component and subclass EditForm * Remove edit context in favor of specialized EditForm * Add special cases for work package editing * Fix edit actions bar * Fix codeclimate issues * Use WorkPackageNotificationService if necessary * Override NotificationService for WPs to allow WP specififc notifications (WiP) * Correctly provide wpNotification service Because the ui-router doesn't seem to correctly use the parent element's injector, we need to provide the wpNotification service not in the wp-base, but rather the wp-list component as well as in the isolated query space. * Allow to filter halEvents for specific resourceTypes (e.g. WorkPackage) * Remove superfluous cell class constant * Start renaming selectors for wp-edit-field into generics * Remove wp-table--cell-span in favor of display field selector * Consolidate other display and edit field styles * Provide specialized service for transitions in active edit forms * Remove superfluous overflowSelector * Accept that date field contains some work package specifics * Ignore unreadable files * Provide the changeset for work packages as a hook Since hal resource editing service is provided per query space, we cannot register them once (would only work globally) * Fix dangerfile * Remove another todo in halResourceNotification service * Fix npm TestBed for changed dependencies * Show inplace edit field in project details widget * Fix highlighting in single view * Provide HalResourceEditingService outside of project context * Used typedState for single-view * Also provide wpNotification service in split view * Correct check for resource type in eventsService * Fix getSchemaName in display field renderer * Fix passing ids into `halEditing.stopEditing` * Do not globally inject the halResourceEditingService There's a bug(?) in ui-router that gives you the global service before the parent injected service for a ui-view * Fix wpCreate service on copying and parallel creation * Remove test project widget * Revert changes for project details widget
5 years ago
link :schema do
{
href: api_v3_paths.projects_schema
}
end
associated_resource :parent,
v3_path: :project,
representer: ::API::V3::Projects::ProjectRepresenter,
uncacheable_link: true,
link: ->(*) {
if represented.parent&.visible?
{
href: api_v3_paths.project(represented.parent.id),
title: represented.parent.name
}
else
{
href: nil,
title: nil
}
end
}
property :id
property :identifier
property :name
property :active
property :public
formattable_property :description
date_time_property :created_at
date_time_property :updated_at
property :status,
name_source: ->(*) { I18n.t('activerecord.attributes.project/status.code') },
render_nil: true,
getter: ->(*) {
next unless status&.code
5 years ago
status.code.to_s.tr('_', ' ')
},
reader: ->(doc:, represented:, **) {
next unless doc.key?('status')
represented.status_attributes ||= {}
represented.status_attributes[:code] =
if doc['status'].nil?
nil
else
doc['status'].strip.tr(' ', '_').underscore.to_sym
end
}
property :status_explanation,
writeable: -> { represented.writable?(:status) },
getter: ->(*) {
::API::Decorators::Formattable.new(status&.explanation,
object: self,
plain: false)
},
setter: ->(fragment:, represented:, **) {
represented.status_attributes ||= {}
represented.status_attributes[:explanation] = fragment["raw"]
}
def _type
'Project'
end
self.to_eager_load = [:status,
:parent,
:enabled_modules,
custom_values: :custom_field]
self.checked_permissions = [:add_work_packages]
end
end
end
end