Skip to content

Commit

Permalink
default community: add possibility to set to None
Browse files Browse the repository at this point in the history
  • Loading branch information
anikachurilova authored and kpsherva committed Oct 4, 2023
1 parent 3ebe119 commit 3be00a6
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export function CommunityCompactItem({
itemClassName,
showPermissionLabel,
detailUrl,
isCommunityDefault,
}) {
return (
<>
Expand All @@ -21,6 +22,7 @@ export function CommunityCompactItem({
itemClassName={itemClassName}
showPermissionLabel={showPermissionLabel}
detailUrl={detailUrl}
isCommunityDefault={isCommunityDefault}
/>
<CommunityCompactItemMobile
result={result}
Expand All @@ -29,6 +31,7 @@ export function CommunityCompactItem({
itemClassName={itemClassName}
showPermissionLabel={showPermissionLabel}
detailUrl={detailUrl}
isCommunityDefault={isCommunityDefault}
/>
</>
);
Expand All @@ -41,6 +44,7 @@ CommunityCompactItem.propTypes = {
itemClassName: PropTypes.string,
showPermissionLabel: PropTypes.bool,
detailUrl: PropTypes.string,
isCommunityDefault: PropTypes.bool.isRequired,
};

CommunityCompactItem.defaultProps = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import PropTypes from "prop-types";
import _truncate from "lodash/truncate";

import { Image, InvenioPopup } from "react-invenio-forms";
import { Icon } from "semantic-ui-react";
import { Icon, Label } from "semantic-ui-react";
import { CommunityTypeLabel, RestrictedLabel } from "../labels";

export const CommunityCompactItemComputer = ({
Expand All @@ -20,6 +20,7 @@ export const CommunityCompactItemComputer = ({
itemClassName,
showPermissionLabel,
detailUrl,
isCommunityDefault,
}) => {
const { metadata, ui, links, access, id } = result;
const communityType = ui?.type?.title_l10n;
Expand Down Expand Up @@ -56,15 +57,23 @@ export const CommunityCompactItemComputer = ({
</p>
)}

{(result.access.visibility === "restricted" ||
communityType ||
extraLabels) && (
<div className="rel-mt-1">
<RestrictedLabel access={access.visibility} />
<CommunityTypeLabel type={communityType} />
{extraLabels}
</div>
)}
<div className="rel-mt-1">
{(result.access.visibility === "restricted" ||
communityType ||
extraLabels) && (
<>
<RestrictedLabel access={access.visibility} />
<CommunityTypeLabel type={communityType} />
{extraLabels}
</>
)}
{isCommunityDefault && (
<Label color="purple" size="tiny">
<Icon name="paint brush" />
{i18next.t("Branding")}
</Label>
)}
</div>
</div>
</div>

Expand Down Expand Up @@ -97,6 +106,7 @@ CommunityCompactItemComputer.propTypes = {
itemClassName: PropTypes.string,
showPermissionLabel: PropTypes.bool,
detailUrl: PropTypes.string,
isCommunityDefault: PropTypes.bool.isRequired,
};

CommunityCompactItemComputer.defaultProps = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { RestrictedLabel } from "../labels";
import _truncate from "lodash/truncate";
import React from "react";
import { Image, InvenioPopup } from "react-invenio-forms";
import { Icon } from "semantic-ui-react";
import { Icon, Label } from "semantic-ui-react";
import PropTypes from "prop-types";

export const CommunityCompactItemMobile = ({
Expand All @@ -20,6 +20,7 @@ export const CommunityCompactItemMobile = ({
itemClassName,
showPermissionLabel,
detailUrl,
isCommunityDefault,
}) => {
const communityType = result.ui?.type?.title_l10n;
const { metadata, ui, links, access, id } = result;
Expand Down Expand Up @@ -76,15 +77,23 @@ export const CommunityCompactItemMobile = ({
</p>
)}

{(result.access.visibility === "restricted" ||
communityType ||
extraLabels) && (
<div className="rel-mt-1">
<RestrictedLabel access={access.visibility} />
<CommunityTypeLabel type={communityType} />
{extraLabels}
</div>
)}
<div className="rel-mt-1">
{(result.access.visibility === "restricted" ||
communityType ||
extraLabels) && (
<>
<RestrictedLabel access={access.visibility} />
<CommunityTypeLabel type={communityType} />
{extraLabels}
</>
)}
{isCommunityDefault && (
<Label color="purple" size="tiny">
<Icon name="paint brush" />
{i18next.t("Branding")}
</Label>
)}
</div>
</div>
</div>
);
Expand All @@ -97,6 +106,7 @@ CommunityCompactItemMobile.propTypes = {
showPermissionLabel: PropTypes.bool,
actions: PropTypes.node,
detailUrl: PropTypes.string,
isCommunityDefault: PropTypes.bool.isRequired,
};

CommunityCompactItemMobile.defaultProps = {
Expand Down
7 changes: 7 additions & 0 deletions invenio_communities/communities/resources/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
CommunityFeaturedEntryDoesNotExistError,
LogoSizeLimitError,
OpenRequestsForCommunityDeletionError,
SetDefaultCommunityError,
)

community_error_handlers = RecordResourceConfig.error_handlers.copy()
Expand Down Expand Up @@ -68,6 +69,12 @@
)
)
),
SetDefaultCommunityError: create_error_handler(
lambda e: HTTPJSONException(
code=400,
description=str(e),
)
),
}
)

Expand Down
13 changes: 13 additions & 0 deletions invenio_communities/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,16 @@ def __init__(self, community, expected_status):
"""Constructor."""
self.community = community
self.expected_status = expected_status


class SetDefaultCommunityError(CommunityError):
"""Record is not part of a community that is being set as default."""

def __init__(self):
"""Initialise error."""
super().__init__(
_(
"Cannot set community as the default. "
"The record has not been added to the community."
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from invenio_records.api import Record

from invenio_communities.communities.records.api import Community
from invenio_communities.errors import SetDefaultCommunityError


class CommunitiesRelationManager:
Expand Down Expand Up @@ -166,11 +167,8 @@ def default(self, community_or_id):
not, then use ``.add(community, default=True)`` instead.
"""
id_ = self._to_id(community_or_id)
if id_ not in self._communities_ids:
raise AttributeError(
"Cannot set community as the default. "
"The record has not been added to the community."
)
if id_ and id_ not in self._communities_ids:
raise SetDefaultCommunityError
self._default_id = id_

@default.deleter
Expand Down
3 changes: 2 additions & 1 deletion tests/records/test_mockrecords_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from sqlalchemy.exc import IntegrityError

from invenio_communities.communities.records.api import Community
from invenio_communities.errors import SetDefaultCommunityError


@pytest.fixture()
Expand Down Expand Up @@ -129,7 +130,7 @@ def test_change_default(db, record, c, c2):
del record.communities.default
assert record.communities.default is None

with pytest.raises(AttributeError):
with pytest.raises(SetDefaultCommunityError):
# record not part of c2, so will fail
record.communities.default = c2

Expand Down

0 comments on commit 3be00a6

Please sign in to comment.