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

Add missing fields in orgs app #50

Draft
wants to merge 4 commits into
base: dev
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
3 changes: 2 additions & 1 deletion CapX/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
UsersBySkillViewSet, UsersByTagViewSet, TerritoryViewSet,
)
from bugs.views import BugViewSet, AttachmentViewSet
from orgs.views import OrganizationViewSet, OrganizationTypeViewSet
from orgs.views import OrganizationViewSet, OrganizationTypeViewSet, TagDiffViewSet
from events.views import EventViewSet, EventParticipantViewSet, EventOrganizationsViewSet
from message.views import MessageViewSet
from projects.views import ProjectViewSet, ProjectMemberViewSet, ProjectMemberAcceptanceViewSet
Expand All @@ -41,6 +41,7 @@
router.register('territory', TerritoryViewSet, basename='territory')
router.register('organizations', OrganizationViewSet, basename='organizations')
router.register('organization_type', OrganizationTypeViewSet, basename='organization_type')
router.register('tag_diff', TagDiffViewSet, basename='tag_diff')
router.register('bugs', BugViewSet, basename='bugs')
router.register('attachment', AttachmentViewSet, basename='attachment')
router.register('users_by_skill', UsersBySkillViewSet, basename='users_by_skill')
Expand Down
23 changes: 23 additions & 0 deletions orgs/migrations/0009_organization_email_organization_website.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 4.2.11 on 2025-01-11 17:38

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('orgs', '0008_organization_tag_diff'),
]

operations = [
migrations.AddField(
model_name='organization',
name='email',
field=models.EmailField(blank=True, help_text='The email address of the organization.', max_length=254, null=True),
),
migrations.AddField(
model_name='organization',
name='website',
field=models.URLField(blank=True, help_text='The URL of the organization website.', null=True),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Generated by Django 4.2.11 on 2025-01-11 18:26

from django.db import migrations, models
import django.utils.timezone


class Migration(migrations.Migration):

dependencies = [
('orgs', '0009_organization_email_organization_website'),
]

operations = [
migrations.CreateModel(
name='TagDiff',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('tag', models.CharField(max_length=255, unique=True)),
('creation_date', models.DateTimeField(default=django.utils.timezone.now)),
],
),
migrations.RemoveField(
model_name='organization',
name='tag_diff',
),
migrations.AddField(
model_name='organization',
name='tag_diff',
field=models.ManyToManyField(blank=True, help_text='The tag used by the organization on Diff posts (if any).', related_name='tag_diff', to='orgs.tagdiff'),
),
]
19 changes: 17 additions & 2 deletions orgs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,20 @@ class Organization(models.Model):
message='Invalid URL format. The format should be https://meta.wikimedia.org/wiki/PageName'
)]
)
email = models.EmailField(
blank=True, null=True,
help_text='The email address of the organization.',
)
website = models.URLField(
blank=True, null=True,
help_text='The URL of the organization website.',
)
mastodon = models.URLField(
blank=True, null=True,
help_text='The URL of the organization Mastodon account.',
)
tag_diff = models.CharField(
max_length=255, blank=True, default='',
tag_diff = models.ManyToManyField(
'orgs.TagDiff', related_name='tag_diff', blank=True,
help_text='The tag used by the organization on Diff posts (if any).',
)
home_project = models.URLField(
Expand All @@ -71,3 +79,10 @@ def __str__(self):
return self.display_name + " (" + self.acronym + ")"
else:
return self.display_name

class TagDiff(models.Model):
tag = models.CharField(max_length=255, unique=True)
creation_date = models.DateTimeField(default=timezone.now)

def __str__(self):
return self.tag
21 changes: 19 additions & 2 deletions orgs/serializers.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
from rest_framework import serializers
from orgs.models import Organization, OrganizationType
from orgs.models import Organization, OrganizationType, TagDiff
from users.models import CustomUser


class OrganizationSerializer(serializers.ModelSerializer):
projects = serializers.SerializerMethodField()
events = serializers.SerializerMethodField()

class Meta:
model = Organization
fields = '__all__'
read_only_fields = ['creation_date']

def get_projects(self, obj):
from projects.models import ProjectMember
return ProjectMember.objects.filter(organization=obj, projectmemberacceptance__accepted=True).values_list('project', flat=True)

def get_events(self, obj):
from events.models import EventOrganizations
return EventOrganizations.objects.filter(organization=obj).values_list('event', flat=True)



class OrganizationTypeSerializer(serializers.ModelSerializer):
class Meta:
model = OrganizationType
fields = '__all__'
fields = '__all__'

class TagDiffSerializer(serializers.ModelSerializer):
class Meta:
model = TagDiff
fields = ['tag']
48 changes: 33 additions & 15 deletions orgs/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from rest_framework import viewsets, status, permissions
from rest_framework.response import Response
from .models import Organization, OrganizationType
from .serializers import OrganizationSerializer, OrganizationTypeSerializer
from .models import Organization, OrganizationType, TagDiff
from .serializers import OrganizationSerializer, OrganizationTypeSerializer, TagDiffSerializer
from users.models import CustomUser as User, Territory
from drf_spectacular.utils import extend_schema, extend_schema_view, OpenApiParameter

Expand Down Expand Up @@ -29,18 +29,6 @@ def get_queryset(self):
return Organization.objects.all()
else:
return Organization.objects.filter(managers__isnull=False)

@extend_schema(
summary='Retrieve an organization by ID.',
description='This endpoint retrieves an organization by its ID.',
)
def retrieve(self, request, *args, **kwargs):
instance = self.get_object()
serializer = self.get_serializer(instance)
data = serializer.data
data['territory'] = [Territory.objects.get(id=id).territory_name for id in data['territory']]
data['managers'] = [User.objects.get(id=id).username for id in data['managers']]
return Response(data)

@extend_schema(
summary='Create a new organization.',
Expand Down Expand Up @@ -88,4 +76,34 @@ def destroy(self, request, *args, **kwargs):
)
class OrganizationTypeViewSet(viewsets.ReadOnlyModelViewSet):
queryset = OrganizationType.objects.all()
serializer_class = OrganizationTypeSerializer
serializer_class = OrganizationTypeSerializer

@extend_schema_view(
list=extend_schema(
summary='List all tag diffs.',
description='This endpoint lists all tag diffs.',
),
retrieve=extend_schema(
summary='Retrieve a tag diff by ID.',
description='This endpoint retrieves a tag diff by its ID.',
),
create=extend_schema(
summary='Create a new tag diff.',
description='This endpoint creates a new tag diff.',
),
destroy=extend_schema(
summary='Delete a tag diff.',
description='This endpoint deletes a tag diff by its ID.',
),
)
class TagDiffViewSet(viewsets.ModelViewSet):
queryset = TagDiff.objects.all()
serializer_class = TagDiffSerializer

@extend_schema(exclude=True)
def update(self, request, *args, **kwargs):
return Response("PUT method not allowed", status=status.HTTP_405_METHOD_NOT_ALLOWED)

@extend_schema(exclude=True)
def partial_update(self, request, *args, **kwargs):
return Response("PATCH method not allowed", status=status.HTTP_405_METHOD_NOT_ALLOWED)
3 changes: 3 additions & 0 deletions projects/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ class Project(models.Model):
help_text='The date when the project was added on the platform.'
)

def __str__(self):
return self.display_name

class ProjectMember(models.Model):
project = models.ForeignKey(
'projects.Project', on_delete=models.CASCADE, related_name='organizations',
Expand Down
8 changes: 7 additions & 1 deletion users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from orgs.models import Organization
from .serializers import ProfileSerializer, TerritorySerializer, LanguageSerializer, WikimediaProjectSerializer, UsersBySkillSerializer, UsersByTagSerializer
from skills.models import Skill
from events.models import Events
from projects.models import Project
from rest_framework import status, viewsets, filters
from rest_framework.response import Response
from django.shortcuts import get_object_or_404
Expand Down Expand Up @@ -199,6 +201,10 @@ def get_queryset(self):
return Organization.objects.all()
elif list_type == 'territory':
return Territory.objects.all()
elif list_type == 'event':
return Events.objects.all()
elif list_type == 'project':
return Project.objects.all()
elif list_type == 'skills':
return Skill.objects.all()
else:
Expand All @@ -219,7 +225,7 @@ def get_serializer_class(self): # pragma: no cover
OpenApiParameter.PATH,
required=True,
description='The type of list to retrieve.',
enum=['language', 'wikimedia_project', 'affiliation', 'territory', 'skills'],
enum=['language', 'wikimedia_project', 'affiliation', 'territory', 'skills', 'event', 'project'],
),
],
responses={(200, 'application/json'): {
Expand Down
Loading