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

248 lines
8.6 KiB

#-- encoding: UTF-8
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 2012-2021 the OpenProject GmbH
#
# 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-2013 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
[34444] Projects copy APIv3 (#9149) * Create copy project endpoint * Add representers * Add _meta representer for copy module information * Add Meta payload representer * Extract parsing of copy attributes into service * Extract enqueue job for projects copy * Keep request object available in the bodied endpoint this allows us to access grape (e.g., for redirecting) * Add DelayedModify endpoint that redirects to job status * Use DelayedModify endpoint for copying projects * Add api paths for form/copy * Disable cache on ProjectCopyPayload * Add spec * Add resource spec * Extend schema for meta * Extend docs * Don't pass service result, but state as meta to forms * Use copy dependencies for naming copy options in API * Add description property for counting * Remove unused action attribute * Skip writable checks on meta property for payloads * Use prepend to allow create(...) usage with meta * Extend copy spec with custom fields usage * Add spec for copy flags * Add todos * Pass errors correctly to copy settings * Remove invalid parameters to copy that are now caught through project service * Remove duplicated validation * Linting * Make count a human readable, formattable description string * Make source count string readable * Provide the source project for generating the counts * Extract copying of attachments into sepearate dependent services This will allow the API to dynamically generate a copy association schema from each dependent service * Add spec for copy schema representer * Add payload representer spec * Also validate the model to pass the validations when copying * Make description optional and add note to schemas.apib * Add send_notifications to representer * Allow to configure sendNotifications * Allow bodied to receive a state process callback That allows us to manage the meta state on the API level, not on the service level * Make other services compatible with BaseCallable * Fix params passing from BaseCallable Wrapping a single hash into kwargs obviously loses their key indifference and we can't expect all services to use a single params object. Older services use kwargs * Copy wiki page with parent_id This will save some memory instead of memoizing the entire wiki page * Fix typo * Default to true for all copy associations That means if only a true value is passed, all other options will still be true * Do not try to copy attachments if the base dependency wasnt copied * Ensure null identifier gets rendered * Fix paths to the form and commit * Change redirect to an URL * Extend documentation on meta properties as table * Ensure we pass a default params if incoming params are nil Now that SetAttributes needs to splat params, they do no longer correctly get the no-param-default behavior of Ruby, so we need to explictly assign a default params hash * Fix expect for trailing path
4 years ago
property :identifier,
render_nil: true
property :name,
render_nil: true
property :active
property :public
formattable_property :description
date_time_property :created_at
date_time_property :updated_at
resource :status,
getter: ->(*) {
next unless represented.status&.code
5 years ago
::API::V3::Projects::Statuses::StatusRepresenter
.create(represented.status.code, current_user: current_user, embed_links: embed_links)
},
link: ->(*) {
if represented.status&.code
{
href: api_v3_paths.project_status(represented.status.code),
title: I18n.t(:"activerecord.attributes.projects/status.codes.#{represented.status.code}",
default: nil)
}.compact
else
{
href: nil
}
end
},
setter: ->(fragment:, represented:, **) {
represented.status_attributes ||= OpenStruct.new
link = ::API::Decorators::LinkObject.new(represented.status_attributes,
path: :project_status,
property_name: :status,
getter: :code,
setter: :"code=")
link.from_hash(fragment)
}
property :status_explanation,
writeable: -> { represented.writable?(:status) },
getter: ->(*) {
::API::Decorators::Formattable.new(status&.explanation,
object: self,
plain: false)
},
setter: ->(fragment:, represented:, **) {
represented.status_attributes ||= OpenStruct.new
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