Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(interview): Value signals [6] #1190

Draft
wants to merge 1 commit into
base: interview-import-dataset
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions rdmo/projects/assets/js/interview/actions/interviewActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,16 +279,16 @@ export function storeValue(value) {
const valueFile = value.file
const valueSuccess = value.success

const page = getState().interview.page
const sets = getState().interview.sets
const question = page.questions.find((question) => question.attribute === value.attribute)
const refresh = question && question.optionsets.some((optionset) => optionset.has_refresh)

dispatch(addToPending(pendingId))
dispatch(storeValueInit(valueIndex))

return ValueApi.storeValue(projectId, value)
return ValueApi.storeValue(projectId, value, { page: page.id })
.then((value) => {
const page = getState().interview.page
const sets = getState().interview.sets
const question = page.questions.find((question) => question.attribute === value.attribute)
const refresh = question && question.optionsets.some((optionset) => optionset.has_refresh)

dispatch(fetchNavigation(page))
dispatch(updateProgress())

Expand Down Expand Up @@ -401,15 +401,16 @@ export function deleteValue(value) {
dispatch(addToPending(pendingId))
dispatch(deleteValueInit(value))

const page = getState().interview.page
const sets = getState().interview.sets
const question = page.questions.find((question) => question.attribute === value.attribute)
const refresh = question.optionsets.some((optionset) => optionset.has_refresh)

if (isNil(value.id)) {
return dispatch(deleteValueSuccess(value))
} else {
return ValueApi.deleteValue(projectId, value)
return ValueApi.deleteValue(projectId, value, { page: page.id })
.then(() => {
const page = getState().interview.page
const sets = getState().interview.sets
const question = page.questions.find((question) => question.attribute === value.attribute)
const refresh = question.optionsets.some((optionset) => optionset.has_refresh)

dispatch(fetchNavigation(page))
dispatch(updateProgress())
Expand Down
10 changes: 5 additions & 5 deletions rdmo/projects/assets/js/interview/api/ValueApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ class ValueApi extends BaseApi {
return this.get(`/api/v1/projects/values/search/?${encodeParams(params)}`)
}

static storeValue(projectId, value) {
static storeValue(projectId, value, params) {
if (isUndefined(value.id)) {
return this.post(`/api/v1/projects/projects/${projectId}/values/`, value)
return this.post(`/api/v1/projects/projects/${projectId}/values/?${encodeParams(params)}`, value)
} else {
return this.put(`/api/v1/projects/projects/${projectId}/values/${value.id}/`, value)
return this.put(`/api/v1/projects/projects/${projectId}/values/${value.id}/?${encodeParams(params)}`, value)
}
}

Expand All @@ -27,9 +27,9 @@ class ValueApi extends BaseApi {
return this.postFormData(`/api/v1/projects/projects/${projectId}/values/${value.id}/file/`, formData)
}

static deleteValue(projectId, value) {
static deleteValue(projectId, value, params) {
if (!isUndefined(value.id)) {
return this.delete(`/api/v1/projects/projects/${projectId}/values/${value.id}/`)
return this.delete(`/api/v1/projects/projects/${projectId}/values/${value.id}/?${encodeParams(params)}`)
}
}

Expand Down
5 changes: 5 additions & 0 deletions rdmo/projects/signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.dispatch import Signal

value_created = Signal()
value_updated = Signal()
value_deleted = Signal()
13 changes: 13 additions & 0 deletions rdmo/projects/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
)
from .serializers.v1.overview import CatalogSerializer, ProjectOverviewSerializer
from .serializers.v1.page import PageSerializer
from .signals import value_created, value_deleted, value_updated
from .utils import check_conditions, get_upload_accept, send_invite_email


Expand Down Expand Up @@ -437,6 +438,18 @@ def get_queryset(self):
# this is needed for the swagger ui
return Value.objects.none()

def perform_create(self, serializer):
super().perform_create(serializer)
value_created.send(sender=Value, request=self.request, instance=serializer.instance)

def perform_update(self, serializer):
super().perform_update(serializer)
value_updated.send(sender=Value, request=self.request, instance=serializer.instance)

def perform_destroy(self, instance):
super().perform_destroy(instance)
value_deleted.send(sender=Value, request=self.request, instance=instance)

@action(detail=False, methods=['POST'], url_path='set',
permission_classes=(HasModelPermission | HasProjectPermission, ))
def copy_set(self, request, parent_lookup_project, pk=None):
Expand Down
6 changes: 6 additions & 0 deletions rdmo/questions/models/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,12 @@ def descendants(self):
descendants += [element, *element.descendants]
return descendants

@cached_property
def attributes(self):
attributes = [self.attribute] if self.attribute else []
attributes += [descendant.attribute for descendant in self.descendants if descendant.attribute]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it not more readable just like this? standard notation for list comprehension..

attributes += [i.attribute for i in self.descendants if i.attribute]

return attributes

def prefetch_elements(self):
models.prefetch_related_objects([self], *self.prefetch_lookups)

Expand Down
6 changes: 6 additions & 0 deletions rdmo/questions/models/questionset.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,12 @@ def descendants(self):
descendants += [element, *element.descendants]
return descendants

@cached_property
def attributes(self):
attributes = [self.attribute] if self.attribute else []
attributes += [descendant.attribute for descendant in self.descendants if descendant.attribute]
return attributes

def prefetch_elements(self):
models.prefetch_related_objects([self], *self.prefetch_lookups)

Expand Down