|
12 | 12 | from rdmo.core.utils import markdown2html |
13 | 13 |
|
14 | 14 | from .constants import ROLE_CHOICES |
15 | | -from .models import Integration, IntegrationOption, Invite, Membership, Project, Snapshot |
| 15 | +from .models import Integration, IntegrationOption, Invite, Membership, Project, Snapshot, Visibility |
16 | 16 | from .validators import ProjectParentValidator |
17 | 17 |
|
18 | 18 |
|
@@ -98,6 +98,48 @@ class Meta: |
98 | 98 | fields = ('title', 'description') |
99 | 99 |
|
100 | 100 |
|
| 101 | +class ProjectUpdateVisibilityForm(forms.ModelForm): |
| 102 | + |
| 103 | + use_required_attribute = False |
| 104 | + |
| 105 | + def __init__(self, *args, **kwargs): |
| 106 | + self.project = kwargs.pop('instance') |
| 107 | + try: |
| 108 | + instance = self.project.visibility |
| 109 | + except Visibility.DoesNotExist: |
| 110 | + instance = None |
| 111 | + |
| 112 | + super().__init__(*args, instance=instance, **kwargs) |
| 113 | + |
| 114 | + # remove the sites or group sets if they are not needed, doing this in Meta would break tests |
| 115 | + if not settings.MULTISITE: |
| 116 | + self.fields.pop('sites') |
| 117 | + if not settings.GROUPS: |
| 118 | + self.fields.pop('groups') |
| 119 | + |
| 120 | + class Meta: |
| 121 | + model = Visibility |
| 122 | + fields = ('sites', 'groups') |
| 123 | + |
| 124 | + def save(self, *args, **kwargs): |
| 125 | + if 'cancel' in self.data: |
| 126 | + pass |
| 127 | + elif 'delete' in self.data: |
| 128 | + self.instance.delete() |
| 129 | + else: |
| 130 | + visibility, created = Visibility.objects.update_or_create(project=self.project) |
| 131 | + |
| 132 | + sites = self.cleaned_data.get('sites') |
| 133 | + if sites is not None: |
| 134 | + visibility.sites.set(sites) |
| 135 | + |
| 136 | + groups = self.cleaned_data.get('groups') |
| 137 | + if groups is not None: |
| 138 | + visibility.groups.set(groups) |
| 139 | + |
| 140 | + return self.project |
| 141 | + |
| 142 | + |
101 | 143 | class ProjectUpdateCatalogForm(forms.ModelForm): |
102 | 144 |
|
103 | 145 | use_required_attribute = False |
|
0 commit comments