Skip to content

Commit

Permalink
Merge pull request #401 from brunoamaral/400-remove-deprecated-catego…
Browse files Browse the repository at this point in the history
…ries-from-modelspy-and-associated-endpoints

Add new API endpoints per team
  • Loading branch information
brunoamaral authored May 30, 2024
2 parents 958e10c + 99d39b2 commit c970d8e
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 178 deletions.
116 changes: 58 additions & 58 deletions django/admin/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,25 @@
"""
from django.contrib import admin
from django.conf import settings
import organizations
from organizations.backends import invitation_backend

from django.conf.urls.static import static
from django.urls import include, path, re_path
from rest_framework import routers
from rest_framework.authtoken import views

from api.views import (
ArticleViewSet, ArticlesByAuthorList, ArticlesByCategory, ArticlesBySourceList,
ArticleViewSet, ArticlesByAuthorList, ArticlesByCategory,
ArticlesByJournal, ArticlesBySubject, AuthorsViewSet, OpenAccessArticles,
RelevantList, UnsentList, TrialsBySourceList, SourceViewSet, TrialViewSet,
RelevantList, UnsentList, TrialsBySource, SourceViewSet, TrialViewSet,
post_article, newsletterByWeek, lastXdays, CategoryViewSet, TrialsByCategory, MonthlyCountsView, LoginView, ProtectedEndpointView,
ArticlesByTeam, ArticlesBySubject, TeamsViewSet, SubjectsViewSet, TrialsByTeam, SubjectsByTeam, SourcesByTeam,CategoriesByTeam,
ArticlesByTeam, ArticlesBySubject, TeamsViewSet, SubjectsViewSet, TrialsByTeam, SubjectsByTeam, SourcesByTeam, CategoriesByTeam,
ArticlesByCategoryAndTeam,ArticlesBySource,TrialsBySubject
)
from rss.views import (
ArticlesByAuthorFeed, ArticlesByCategoryFeed, ArticlesBySubjectFeed, OpenAccessFeed,
LatestArticlesFeed, LatestTrialsFeed, MachineLearningFeed,
LatestArticlesFeed, LatestTrialsFeed, MachineLearningFeed
)
from subscriptions.views import subscribe_view
from organizations.backends import invitation_backend

# Initialize the router and register some endpoints
router = routers.DefaultRouter()
Expand All @@ -49,77 +49,77 @@
urlpatterns = [
# Admin routes
path('admin/', admin.site.urls),
# Organization routes
re_path(r'^accounts/', include('organizations.urls')),
re_path(r'^invitations/', include(invitation_backend().get_urls())),

# API auth route
path('api-auth/', include('rest_framework.urls')),
path('api/token/', LoginView.as_view(), name='token_obtain_pair'),
path('api/token/get/', views.obtain_auth_token),

# Article routes
path('articles/relevant/', RelevantList.as_view()),
# API routes
path('articles/post/', post_article),
# Articles by team
path('teams/<int:team_id>/articles/', ArticlesByTeam.as_view({'get': 'list'}), name='articles-by-team'),
# Clinical Trials by team
path('teams/<int:team_id>/trials/', TrialsByTeam.as_view({'get': 'list'}), name='trials-by-team'),
# Subjects by team
path('teams/<int:team_id>/subjects/', SubjectsByTeam.as_view({'get': 'list'}), name='subjects-by-team'),
# Sources by team
path('teams/<int:team_id>/sources/', SourcesByTeam.as_view({'get': 'list'}), name='sources-by-team'),
# Category by team
path('teams/<int:team_id>/categories/', CategoriesByTeam.as_view({'get': 'list'}), name='categories-by-team'),
# Articles by team and subject
path('articles/team/<int:team_id>/', ArticlesByTeam.as_view({'get': 'list'}), name='articles-by-team'),
path('articles/subject/<int:subject_id>/', ArticlesBySubject.as_view({'get': 'list'}), name='articles-by-subject'),

# Feed routes
path('feed/articles/author/<int:author_id>/', ArticlesByAuthorFeed(), name='articles_by_author_feed'),
path('feed/articles/category/<str:category>/', ArticlesByCategoryFeed()),
path('feed/articles/subject/<str:subject>/', ArticlesBySubjectFeed()),
path('feed/articles/open-access/',OpenAccessFeed()), # Renamed for clarity
path('feed/articles/open-access/', OpenAccessFeed()), # Renamed for clarity
path('feed/latest/articles/', LatestArticlesFeed()),
path('feed/latest/trials/', LatestTrialsFeed()),
path('feed/machine-learning/', MachineLearningFeed()),
path('feed/teams/<int:team_id>/categories/<str:category_slug>/', ArticlesByCategoryFeed(), name='articles-by-category-feed'),

# Organization routes
re_path(r'^accounts/', include('organizations.urls')),
re_path(r'^invitations/', include(invitation_backend().get_urls())),

# Protected endpoint route
path('protected_endpoint/', ProtectedEndpointView.as_view(), name='protected_endpoint'),

# Subscriptions route
path('subscriptions/new/', subscribe_view),

# More articles routes
# Team API
## List Teams
path('teams/', TeamsViewSet.as_view({'get':'list'})),
## List articles
path('teams/<int:team_id>/articles/', ArticlesByTeam.as_view({'get': 'list'}), name='articles-by-team'),
## List article per ID
## List articles per subject
path('teams/<int:team_id>/articles/subject/<int:subject_id>/', ArticlesBySubject.as_view({'get': 'list'}), name='articles-by-subject'),
## List articles per category: OK
path('teams/<int:team_id>/articles/category/<str:category_slug>/', ArticlesByCategoryAndTeam.as_view({'get': 'list'}), name='articles-by-category-and-team'),
## List articles per source
path('teams/<int:team_id>/articles/source/<int:source_id>/', ArticlesBySource.as_view({'get': 'list'}), name='articles-by-category-and-team'),
## List articles per journal
## List clinical trials
path('teams/<int:team_id>/trials/', TrialsByTeam.as_view({'get': 'list'}), name='trials-by-team'),
## List clinical trials per category
path('teams/<int:team_id>/trials/category/<str:category_slug>/', TrialsByCategory.as_view({'get':'list'})),
## List clinical trials per subject
path('teams/<int:team_id>/trials/subject/<int:subject_id>/', TrialsBySubject.as_view({'get':'list'})),
## List clinical trials per source
path('teams/<int:team_id>/trials/source/<int:source_id>/', TrialsBySource.as_view(), name='trials-by-source'),
## List categories
path('teams/<int:team_id>/categories/', CategoriesByTeam.as_view({'get': 'list'}), name='categories-by-team'),
## List monthly counts per category
path('teams/<int:team_id>/categories/<str:category_slug>/monthly-counts/', MonthlyCountsView.as_view()),
## List subjects
path('teams/<int:team_id>/subjects/', SubjectsByTeam.as_view({'get': 'list'}), name='subjects-by-team'),
## List sources
path('teams/<int:team_id>/sources/', SourcesByTeam.as_view({'get': 'list'}), name='sources-by-team'),


# Old API routes

## Articles routes
path('articles/author/<int:author_id>/', ArticlesByAuthorList.as_view()),
path('articles/relevant/', RelevantList.as_view()),
path('articles/relevant/last/<int:days>/', lastXdays.as_view({'get': 'list'})),
path('articles/relevant/week/<int:year>/<int:week>/', newsletterByWeek.as_view({'get': 'list'})),
re_path('^articles/category/(?P<category_slug>[-\w]+)/$', ArticlesByCategory.as_view({'get':'list'})),
path('articles/source/<int:source_id>', ArticlesBySourceList.as_view()),
re_path('^articles/journal/(?P<journal_slug>.+)/$', ArticlesByJournal.as_view({'get':'list'})),
re_path('^articles/open-access/$', OpenAccessArticles.as_view()),
re_path('^articles/unsent/$', UnsentList.as_view()),

# Relevant articles routes
path('articles/relevant/week/<int:year>/<int:week>/', newsletterByWeek.as_view({'get':'list'})),
path('articles/relevant/last/<int:days>/', lastXdays.as_view({'get':'list'})),

# Articles by team and subject
path('articles/team/<int:team_id>/', ArticlesByTeam.as_view({'get': 'list'}), name='articles-by-team'),
path('articles/subject/<int:subject_id>/', ArticlesBySubject.as_view({'get': 'list'}), name='articles-by-subject'),


# Category routes
path('categories/', CategoryViewSet.as_view({'get':'list'})),
path('categories/<str:category_slug>/monthly-counts/', MonthlyCountsView.as_view()),

# Trial routes
re_path('^trials/category/(?P<category_slug>[-\w]+)/$', TrialsByCategory.as_view({'get':'list'})),
re_path('^trials/source/(?P<source>.+)/$', TrialsBySourceList.as_view()),

# Token routes
path('api/token/', LoginView.as_view(), name='token_obtain_pair'),
path('api/token/get/', views.obtain_auth_token),

# Protected endpoint route
path('protected_endpoint/', ProtectedEndpointView.as_view(), name='protected_endpoint'),

# Team Routes
path('teams/', TeamsViewSet.as_view({'get':'list'})),

# Include router routes
path('', include(router.urls)),

] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
33 changes: 17 additions & 16 deletions django/api/serializers.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
from rest_framework import serializers
from gregory.models import Articles, Trials, Sources, Authors, Categories, Subject, Team, MLPredictions, ArticleSubjectRelevance,TeamCategory,Subject
from gregory.models import Articles, Trials, Sources, Authors, Subject, Team, MLPredictions, ArticleSubjectRelevance,TeamCategory,Subject
from organizations.models import Organization
from sitesettings.models import CustomSetting
from django.contrib.sites.models import Site
from django.conf import settings

customsettings = CustomSetting.objects.get(site=settings.SITE_ID)
site = Site.objects.get(pk=settings.SITE_ID)

class TeamSerializer(serializers.ModelSerializer):
class Meta:
model = Team
fields = '__all__'
class SubjectsSerializer(serializers.ModelSerializer):
team_id = serializers.IntegerField(source='team.id', read_only=True)
class Meta:
Expand All @@ -30,15 +35,10 @@ class Meta:
model = MLPredictions
fields = ['gnb', 'lr', 'lsvc', 'mnb', 'created_date', 'subject']

class TeamSerializer(serializers.ModelSerializer):
class Meta:
model = Team
fields = ['id', 'name']

class CategorySerializer(serializers.ModelSerializer):
class Meta:
model = Categories
fields = ['category_id', 'category_description', 'category_name', 'category_slug', 'category_terms', 'article_count']
model = TeamCategory
fields = ['id', 'category_description', 'category_name', 'category_slug', 'category_terms', 'article_count']

class ArticleAuthorSerializer(serializers.ModelSerializer):
country = serializers.SerializerMethodField()
Expand All @@ -53,7 +53,6 @@ def get_country(self, obj):

class ArticleSerializer(serializers.HyperlinkedModelSerializer):
sources = serializers.SlugRelatedField(many=True, read_only=True, slug_field='name')
categories = CategorySerializer(many=True, read_only=True)
team_categories = TeamCategorySerializer(many=True, read_only=True)
authors = ArticleAuthorSerializer(many=True, read_only=True)
teams = TeamSerializer(many=True, read_only=True)
Expand All @@ -68,20 +67,19 @@ class Meta:
'article_id', 'title', 'summary', 'link', 'published_date', 'sources', 'teams',
'subjects', 'publisher', 'container_title', 'authors', 'relevant',
'discovery_date', 'article_subject_relevances',
'noun_phrases', 'doi', 'access', 'takeaways', 'categories', 'team_categories', 'ml_predictions',
'noun_phrases', 'doi', 'access', 'takeaways', 'team_categories', 'ml_predictions',
]
read_only_fields = ('discovery_date', 'ml_predictions', 'noun_phrases', 'takeaways')

class TrialSerializer(serializers.HyperlinkedModelSerializer):
source = serializers.SlugRelatedField(read_only=True, slug_field='name')
categories = CategorySerializer(many=True, read_only=True)
team_categories = TeamCategorySerializer(many=True, read_only=True)

class Meta:
model = Trials
fields = [
'trial_id', 'title', 'summary', 'published_date', 'discovery_date', 'link', 'source', 'relevant',
'identifiers', 'categories', 'team_categories', 'export_date', 'internal_number', 'last_refreshed_on',
'identifiers', 'team_categories', 'export_date', 'internal_number', 'last_refreshed_on',
'scientific_title', 'primary_sponsor', 'retrospective_flag', 'date_registration',
'source_register', 'recruitment_status', 'other_records', 'inclusion_agemin',
'inclusion_agemax', 'inclusion_gender', 'date_enrollement', 'target_size',
Expand Down Expand Up @@ -127,8 +125,11 @@ def get_articles_count(self, obj):
return Articles.objects.count()


class TeamSerializer(serializers.ModelSerializer):
class Meta:
model = Team
fields = '__all__'
class ArticlesByCategoryAndTeamSerializer(serializers.ModelSerializer):
articles = ArticleSerializer(many=True, read_only=True)
team = TeamSerializer(read_only=True)
category = TeamCategorySerializer(read_only=True, source='self')

class Meta:
model = TeamCategory
fields = ['id', 'team', 'category', 'articles']
Loading

0 comments on commit c970d8e

Please sign in to comment.