forked from openedx/credentials
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added credentials site creation API EDLY-2904 (#8)
- Loading branch information
Muhammad Haseeb
authored
May 5, 2021
1 parent
edfb3e2
commit 51cf14c
Showing
8 changed files
with
369 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
credentials/apps/edx_credentials_extensions/edly_credentials_app/api/v1/constants.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
""" | ||
Constants for Edly Credentials API. | ||
""" | ||
from django.utils.translation import ugettext as _ | ||
|
||
ERROR_MESSAGES = { | ||
'CLIENT_SITES_SETUP_SUCCESS': _('Client sites setup successful.'), | ||
'CLIENT_SITES_SETUP_FAILURE': _('Client sites setup failed.'), | ||
} | ||
|
||
CLIENT_SITE_SETUP_FIELDS = [ | ||
'lms_site', | ||
'credentials_site', | ||
'wordpress_site', | ||
'edly_slug', | ||
'platform_name', | ||
'discovery_site', | ||
'theme_dir_name', | ||
'oauth_clients' | ||
] | ||
|
||
EDLY_PANEL_WORKER_USER = 'edly_panel_worker' |
8 changes: 7 additions & 1 deletion
8
credentials/apps/edx_credentials_extensions/edly_credentials_app/api/v1/urls.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,15 @@ | ||
from django.conf.urls import url | ||
from rest_framework import routers | ||
|
||
from credentials.apps.edx_credentials_extensions.edly_credentials_app.api.v1.views.edly_sites import EdlySiteViewSet | ||
from credentials.apps.edx_credentials_extensions.edly_credentials_app.api.v1.views.program_certificate_configuration import ProgramCertificateConfigurationViewSet | ||
|
||
|
||
router = routers.SimpleRouter() | ||
router.register(r'program-certificate-configuration', ProgramCertificateConfigurationViewSet, basename='program-certificate-configuration') | ||
|
||
urlpatterns = router.urls | ||
urlpatterns = [ | ||
url(r'^edly_sites/', EdlySiteViewSet.as_view(), name='edly_sites'), | ||
] | ||
|
||
urlpatterns += router.urls |
80 changes: 80 additions & 0 deletions
80
credentials/apps/edx_credentials_extensions/edly_credentials_app/api/v1/views/edly_sites.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
""" | ||
Views for Edly Site Creation API. | ||
""" | ||
from django.contrib.sites.models import Site | ||
from rest_framework import status | ||
from rest_framework.permissions import IsAuthenticated | ||
from rest_framework.response import Response | ||
from rest_framework.views import APIView | ||
|
||
from credentials.apps.core.models import SiteConfiguration | ||
from credentials.apps.edx_credentials_extensions.edly_credentials_app.api.permissions import CanAccessSiteCreation | ||
from credentials.apps.edx_credentials_extensions.edly_credentials_app.api.v1.constants import ERROR_MESSAGES | ||
from credentials.apps.edx_credentials_extensions.edly_credentials_app.helpers import ( | ||
get_credentials_site_configuration, | ||
validate_site_configurations, | ||
) | ||
|
||
|
||
class EdlySiteViewSet(APIView): | ||
""" | ||
Creates credentials site and it's site configuration. | ||
""" | ||
permission_classes = [IsAuthenticated, CanAccessSiteCreation] | ||
|
||
def post(self, request): | ||
""" | ||
POST /api/edly_api/v1/edly_sites. | ||
""" | ||
validations_messages = validate_site_configurations(request.data) | ||
if len(validations_messages) > 0: | ||
return Response(validations_messages, status=status.HTTP_400_BAD_REQUEST) | ||
|
||
try: | ||
self.process_client_sites_setup() | ||
return Response( | ||
{'success': ERROR_MESSAGES.get('CLIENT_SITES_SETUP_SUCCESS')}, | ||
status=status.HTTP_200_OK | ||
) | ||
except TypeError: | ||
return Response( | ||
{'error': ERROR_MESSAGES.get('CLIENT_SITES_SETUP_FAILURE')}, | ||
status=status.HTTP_400_BAD_REQUEST | ||
) | ||
|
||
def process_client_sites_setup(self): | ||
""" | ||
Process client sites setup and update configurations. | ||
""" | ||
edly_slug = self.request.data.get('edly_slug', '') | ||
credentials_base = self.request.data.get('credentials_site', '') | ||
theme_dir_name = self.request.data.get('theme_dir_name', 'openedx') | ||
lms_url_root = '{protocol}://{lms_url_root}'.format( | ||
protocol=self.request.data.get('protocol', 'https'), | ||
lms_url_root=self.request.data.get('lms_site', '') | ||
) | ||
catalog_api_url = '{protocol}://{discovery_site}/api/v1/'.format( | ||
protocol=self.request.data.get('protocol', 'https'), | ||
discovery_site=self.request.data.get('discovery_site', '') | ||
) | ||
wordpress_site = '{protocol}://{wordpress_site}'.format( | ||
protocol=self.request.data.get('protocol', 'https'), | ||
wordpress_site=self.request.data.get('wordpress_site', '') | ||
) | ||
credentials_site, __ = Site.objects.update_or_create(domain=credentials_base, defaults=dict(name=credentials_base)) | ||
credentials_site_config, __ = SiteConfiguration.objects.update_or_create( | ||
site=credentials_site, | ||
defaults=dict( | ||
edx_org_short_name=edly_slug, | ||
platform_name=self.request.data.get('platform_name', ''), | ||
company_name=self.request.data.get('platform_name', ''), | ||
theme_name=theme_dir_name, | ||
lms_url_root=lms_url_root, | ||
catalog_api_url=catalog_api_url, | ||
tos_url='{lms_url_root}/tos'.format(lms_url_root=lms_url_root), | ||
privacy_policy_url='{lms_url_root}/privacy'.format(lms_url_root=lms_url_root), | ||
homepage_url=wordpress_site, | ||
certificate_help_url='{wordpress_site}/contact-us'.format(wordpress_site=wordpress_site), | ||
edly_client_branding_and_django_settings=get_credentials_site_configuration(self.request.data), | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
credentials/apps/edx_credentials_extensions/edly_credentials_app/helpers.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from credentials.apps.edx_credentials_extensions.edly_credentials_app.api.v1.constants import CLIENT_SITE_SETUP_FIELDS | ||
|
||
|
||
def validate_site_configurations(request_data): | ||
""" | ||
Identify missing required fields for client's site setup. | ||
Arguments: | ||
request_data (dict): Request data passed for site setup | ||
Returns: | ||
validation_messages (dict): Missing fields information | ||
""" | ||
|
||
validation_messages = {} | ||
|
||
for field in CLIENT_SITE_SETUP_FIELDS: | ||
if not request_data.get(field, None): | ||
validation_messages[field] = '{0} is Missing'.format(field.replace('_', ' ').title()) | ||
|
||
return validation_messages | ||
|
||
|
||
def get_credentials_site_configuration(request_data): | ||
""" | ||
Prepare Credentials Site Configurations for Client based on Request Data. | ||
Arguments: | ||
request_data (dict): Request data passed for site setup | ||
Returns: | ||
(dict): Credentials Site Configuration | ||
""" | ||
protocol = request_data.get('protocol', 'https') | ||
lms_site = request_data.get('lms_site', '') | ||
lms_site_with_protocol = '{protocol}://{lms_root_domain}'.format( | ||
protocol=protocol, | ||
lms_root_domain=lms_site, | ||
) | ||
oauth2_clients = request_data.get('oauth_clients', {}) | ||
credentials_sso_values = oauth2_clients.get('credentials-sso', {}) | ||
credentials_backend_values = oauth2_clients.get('credentials-backend', {}) | ||
|
||
return { | ||
'DJANGO_SETTINGS_OVERRIDE': { | ||
'SOCIAL_AUTH_EDX_OAUTH2_KEY': credentials_sso_values.get('id', ''), | ||
'SOCIAL_AUTH_EDX_OAUTH2_SECRET': credentials_sso_values.get('secret', ''), | ||
'SOCIAL_AUTH_EDX_OAUTH2_ISSUER': lms_site_with_protocol, | ||
'SOCIAL_AUTH_EDX_OAUTH2_URL_ROOT': lms_site_with_protocol, | ||
'SOCIAL_AUTH_EDX_OAUTH2_PUBLIC_URL_ROOT': lms_site_with_protocol, | ||
'SOCIAL_AUTH_EDX_OAUTH2_LOGOUT_URL': '{lms_site_with_protocol}/logout'.format( | ||
lms_site_with_protocol=lms_site_with_protocol | ||
), | ||
'BACKEND_SERVICE_EDX_OAUTH2_KEY': credentials_backend_values.get('id', ''), | ||
'BACKEND_SERVICE_EDX_OAUTH2_SECRET': credentials_backend_values.get('secret', ''), | ||
'BACKEND_SERVICE_EDX_OAUTH2_PROVIDER_URL': '{lms_site_with_protocol}/oauth2'.format( | ||
lms_site_with_protocol=lms_site_with_protocol | ||
), | ||
} | ||
} |
Oops, something went wrong.