Skip to content

Commit

Permalink
Move SITE_ID to html meta, use DjangoFilterBackend for index actions,…
Browse files Browse the repository at this point in the history
… and simplify questions angular app
  • Loading branch information
jochenklar committed Oct 20, 2022
1 parent ed5facc commit b43ca6b
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 74 deletions.
1 change: 1 addition & 0 deletions rdmo/core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@
}

SETTINGS_EXPORT = [
'SITE_ID',
'LOGIN_URL',
'LOGOUT_URL',
'ACCOUNT',
Expand Down
1 change: 1 addition & 0 deletions rdmo/core/templates/core/base_head.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

<meta name='baseurl' content="{% url 'home' %}" />
<meta name='staticurl' content="{% static '' %}" />
<meta name='site_id' content="{{ settings.SITE_ID }}" />

<link rel="shortcut icon" href="{% static 'core/img/favicon.png' %}" type="image/png" />

Expand Down
79 changes: 32 additions & 47 deletions rdmo/questions/static/questions/js/catalogs.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ angular.module('catalogs', ['core'])

}])

.factory('CatalogsService', ['$resource', '$timeout', '$window', '$q', '$location', 'utils', function($resource, $timeout, $window, $q, $location, utils) {
.factory('CatalogsService', ['$resource', '$timeout', '$window', '$q', '$location', '$filter', 'utils', function($resource, $timeout, $window, $q, $location, $filter, utils) {

/* get the base url */
/* get the base url and the site_id */

var baseurl = angular.element('meta[name="baseurl"]').attr('content');
var site_id = angular.element('meta[name="site_id"]').attr('content');

/* configure resources */

Expand Down Expand Up @@ -85,85 +86,69 @@ angular.module('catalogs', ['core'])
service.widgettypes = resources.widgettypes.query();
service.valuetypes = resources.valuetypes.query();
service.settings = resources.settings.get();
service.sites = resources.sites.query( function(sites) {
service.sites = sites;
service.currentSiteDomainName = angular.element('meta[property="og:url"]').attr('content');
service.currentSite = sites.find(site => site.domain == service.currentSiteDomainName);
service.currentSiteId = service.currentSite.id;
});
service.sites = resources.sites.query();
service.groups = resources.groups.query();
service.uri_prefixes = []
service.uri_prefix = ''
service.options = []
service.filter = sessionStorage.getItem('questions_filter') || '';
service.showQuestionSets = !(sessionStorage.getItem('options_showQuestionSets') === 'false');
service.showQuestions = !(sessionStorage.getItem('options_showOptions') === 'false');

// start of chain of promise.then() calls
service.sites.$promise.then(
function(sites) {

if ((sessionStorage.getItem('options_filterCatalogsCurrentSite') === null &&
angular.isUndefined(service.filterCatalogsCurrentSite)) &&
sites.length > 1 ) {

if (angular.isUndefined(service.filterCatalogsCurrentSite)) {
if (sessionStorage.getItem('options_filterCatalogsCurrentSite') === null) {
// when user arrives on page for the first time, set filterCatalogsCurrentSite to true
service.filterCatalogsCurrentSite = true;
} else if ((sessionStorage.getItem('options_filterCatalogsCurrentSite') === 'false' &&
angular.isUndefined(service.filterCatalogsCurrentSite))) {
} else if (sessionStorage.getItem('options_filterCatalogsCurrentSite') === 'false') {
// when user switches language and options_filterCatalogsCurrentSite was false before
service.filterCatalogsCurrentSite = false;
} else if ((sessionStorage.getItem('options_filterCatalogsCurrentSite') === 'true' &&
angular.isUndefined(service.filterCatalogsCurrentSite))) {
} else if (sessionStorage.getItem('options_filterCatalogsCurrentSite') === 'true') {
// when user switches language and options_filterCatalogsCurrentSite was true before
service.filterCatalogsCurrentSite = true;
}
}

// default api query for catalogs
service.catalogs_query = {list_action: 'index'};
if (service.filterCatalogsCurrentSite && sites.length > 1) {
// add the filter for sites with current site id to the query
const sites_filter = {'sites' : service.currentSiteId}
service.catalogs_query = Object.assign(service.catalogs_query, sites_filter);
}
var catalogs_params = {
list_action: 'index'
}
if (service.filterCatalogsCurrentSite) {
// query only catalogs for this site
catalogs_params['sites'] = site_id
}

return resources.catalogs.query(service.catalogs_query).$promise
})
.then(function(response) {
resources.catalogs.query(catalogs_params, function(response) {
service.catalogs = response;

// try to get the catalog from the address bar
var catalog_id = $location.path().replace(/\//g,'');
// check if catalog_id is in the array of catalogs
if (catalog_id) {
// try to get the catalog from the address bar
var catalog_id = $location.path().replace(/\//g,'');

// check if catalog_id is in the array of catalogs
if (catalog_id) {
// check if catalog_id is in catalogs
// required for when user switches filterCatalogsCurrentSite
var catalog_id_isin_catalogs = service.catalogs.find(x => x.id == catalog_id);
if (catalog_id_isin_catalogs) {
service.current_catalog_id = catalog_id;
} else if (service.catalogs.length) {
service.current_catalog_id = service.catalogs[0].id;
var catalog_id_isin_catalogs = $filter('filter')(service.catalogs, {'id': 1});
if (catalog_id_isin_catalogs.length > 0) {
service.current_catalog_id = catalog_id;
} else {
service.current_catalog_id = null;
// the catalog is (propably) not available for this site
service.current_catalog_id = service.catalogs[0].id;
}
} else if (service.catalogs.length) {
} else if (service.catalogs.length > 0) {
service.current_catalog_id = service.catalogs[0].id;
} else {
// no catalogs are available
service.current_catalog_id = null;
};

return response
})
.then(function(response) {
service.initView();
service.initView().then(function() {
var current_scroll_pos = sessionStorage.getItem('questions_scroll_pos');
if (current_scroll_pos) {
$timeout(function() {
$window.scrollTo(0, current_scroll_pos);
});
}
});

});

$window.addEventListener('beforeunload', function() {
sessionStorage.setItem('questions_scroll_pos', $window.scrollY);
Expand Down
11 changes: 5 additions & 6 deletions rdmo/questions/templates/questions/catalogs_sidebar.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

<div ng-cloak>

<h2>{% trans 'Catalog' %}</h2>
<h2>{% trans 'Catalog' %}</h2>

<select class="form-control" ng-model="service.current_catalog_id" ng-change="service.initView()" by-number ng-cloak>
<option ng-repeat="catalog in service.catalogs | orderBy : 'title'" value="{$ catalog.id $}">{$ catalog.title $}</option>
</select>
<option ng-repeat="catalog in service.catalogs | orderBy : 'title'" value="{$ catalog.id $}">{$ catalog.title $}</option>
</select>

{% if settings.MULTISITE %}
<div class="form-group" ng-show="service.sites.length > 1">
<div class="form-group">
<div class="checkbox">
<label>
<input type="checkbox"
<input type="checkbox"
ng-model="service.filterCatalogsCurrentSite"
ng-change="service.init()">
{% trans 'Filter catalogs for' %} {{ request.site.domain }}
Expand All @@ -21,7 +21,6 @@ <h2>{% trans 'Catalog' %}</h2>
</div>
{% endif %}


<h2>{% trans 'Filter questions' %}</h2>

<div class="form-group">
Expand Down
26 changes: 5 additions & 21 deletions rdmo/questions/viewsets.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from django.contrib.sites.shortcuts import get_current_site
from django.db import models
from django.db.models import Prefetch
from django_filters.rest_framework import DjangoFilterBackend
Expand Down Expand Up @@ -39,8 +38,7 @@ class CatalogViewSet(CopyModelMixin, ModelViewSet):
'uri',
'key',
'comment',
'sites',

'sites'
)

def get_queryset(self):
Expand Down Expand Up @@ -75,12 +73,7 @@ def nested(self, request, pk):

@action(detail=False)
def index(self, request):
sites_query = request.query_params.get('sites', None)
if sites_query:
queryset = Catalog.objects.filter(sites=sites_query)
else:
queryset = Catalog.objects.all()
# breakpoint()
queryset = self.filter_queryset(self.get_queryset())
serializer = CatalogIndexSerializer(queryset, many=True)
return Response(serializer.data)

Expand All @@ -96,15 +89,6 @@ def detail_export(self, request, pk=None):
xml = CatalogRenderer().render([serializer.data])
return XMLResponse(xml, name=self.get_object().key)

@action(detail=False)
def current_site(self, request):
''' optional list action to get all catalogs for the current site '''
current_site = get_current_site(request)
queryset = Catalog.objects.filter(sites=current_site)
serializer = CatalogIndexSerializer(queryset, many=True)
return Response(serializer.data)



class SectionViewSet(CopyModelMixin, ModelViewSet):
permission_classes = (HasModelPermission, )
Expand Down Expand Up @@ -149,7 +133,7 @@ def nested(self, request, pk):

@action(detail=False)
def index(self, request):
queryset = Section.objects.all()
queryset = self.filter_queryset(self.get_queryset())
serializer = SectionIndexSerializer(queryset, many=True)
return Response(serializer.data)

Expand Down Expand Up @@ -209,7 +193,7 @@ def nested(self, request, pk):

@action(detail=False)
def index(self, request):
queryset = QuestionSet.objects.all()
queryset = self.filter_queryset(self.get_queryset())
serializer = QuestionSetIndexSerializer(queryset, many=True)
return Response(serializer.data)

Expand Down Expand Up @@ -264,7 +248,7 @@ def nested(self, request, pk):

@action(detail=False)
def index(self, request):
queryset = Question.objects.all()
queryset = self.filter_queryset(self.get_queryset())
serializer = QuestionIndexSerializer(queryset, many=True)
return Response(serializer.data)

Expand Down

0 comments on commit b43ca6b

Please sign in to comment.