diff --git a/combo/apps/fargo/migrations/0004_recentdocumentscell_fargo_site.py b/combo/apps/fargo/migrations/0004_recentdocumentscell_fargo_site.py new file mode 100644 index 00000000..baeb08f6 --- /dev/null +++ b/combo/apps/fargo/migrations/0004_recentdocumentscell_fargo_site.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('fargo', '0003_recentdocumentscell_last_update_timestamp'), + ] + + operations = [ + migrations.AddField( + model_name='recentdocumentscell', + name='fargo_site', + field=models.CharField(max_length=50, verbose_name='Site', blank=True), + ), + ] diff --git a/combo/apps/fargo/models.py b/combo/apps/fargo/models.py index 2299f298..8749e2db 100644 --- a/combo/apps/fargo/models.py +++ b/combo/apps/fargo/models.py @@ -15,6 +15,9 @@ # along with this program. If not, see . from django.conf import settings +from django.db import models +from django.forms import models as model_forms +from django.forms import Select from django.utils.translation import ugettext_lazy as _ from combo.data.models import CellBase @@ -22,14 +25,33 @@ from combo.data.library import register_cell_class from combo.utils import requests - @register_cell_class class RecentDocumentsCell(CellBase): template_name = 'combo/fargo/recent-documents-cell.html' + fargo_site = models.CharField(_('Site'), max_length=50, blank=True) class Meta: verbose_name = _('Recent Documents') + def get_form_fields(self): + if len(get_fargo_services()) == 1: + return [] + return ['fargo_site'] + + def get_form_widgets(self): + if len(get_fargo_services()) == 1: + return {} + combo_fargo_sites = [('', _('Default'))] + fargo_sites = [(x, y.get('title')) for x, y in get_fargo_services().items()] + fargo_sites.sort(key=lambda x: x[1]) + combo_fargo_sites.extend(fargo_sites) + return {'fargo_site': Select(choices=combo_fargo_sites)} + + def get_default_form_class(self): + return model_forms.modelform_factory(self.__class__, + fields=self.get_form_fields(), + widgets=self.get_form_widgets()) + def is_visible(self, user=None): if not user or user.is_anonymous(): return False @@ -40,8 +62,9 @@ class RecentDocumentsCell(CellBase): return hasattr(settings, 'KNOWN_SERVICES') and settings.KNOWN_SERVICES.get('fargo') def get_json(self, path, context): + remote_service = get_fargo_site(self.fargo_site) response = requests.get(path, - remote_service=settings.KNOWN_SERVICES['fargo'].values()[0], + remote_service=get_fargo_site(self.fargo_site), user=self.get_concerned_user(context), raise_if_not_cached=not(context.get('synchronous')), headers={'accept': 'application/json'}) @@ -52,3 +75,16 @@ class RecentDocumentsCell(CellBase): def render(self, context): context.update(self.get_json('api/documents/recently-added', context)) return super(RecentDocumentsCell, self).render(context) + + +def get_fargo_services(): + return settings.KNOWN_SERVICES.get('fargo') or [] + + +def get_fargo_site(fargo_site): + if fargo_site: + return settings.KNOWN_SERVICES['fargo'].get(fargo_site) + for site in settings.KNOWN_SERVICES['fargo'].values(): + if not site.get('secondary'): + return site + return settings.KNOWN_SERVICES['fargo'].values()[0]