Skip to content

Commit

Permalink
Merge pull request #5247 from onepercentclub/hotfix/BB-20397-add-down…
Browse files Browse the repository at this point in the history
…load-link-to-documents

Add download link to private document fields in the admin.
  • Loading branch information
eodolphi committed Sep 26, 2022
2 parents e895a3a + 6a9b9ba commit 0682140
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 20 deletions.
24 changes: 22 additions & 2 deletions bluebottle/files/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,32 @@ def formfield(self, **kwargs):
return super(DocumentField, self).formfield(**defaults)


class PrivateDocumentModelChoiceField(ModelChoiceField):
def __init__(self, related_field=None, view_name=None, *args, **kwargs):
self.related_field = related_field
self.view_name = view_name
super().__init__(*args, **kwargs)

def widget_attrs(self, widget):
attrs = super().widget_attrs(widget)

attrs['related_field'] = self.related_field
attrs['view_name'] = self.view_name

return attrs


class PrivateDocumentField(ForeignKey):

def __init__(self, to=None, on_delete=models.CASCADE, related_name=None, related_query_name=None,
limit_choices_to=None, parent_link=False, to_field=None,
db_constraint=True, **kwargs):
db_constraint=True, view_name=None, **kwargs):
if not to:
from bluebottle.files.models import PrivateDocument
to = PrivateDocument

self.view_name = view_name

super(PrivateDocumentField, self).__init__(
to, on_delete, related_name, related_query_name,
limit_choices_to, parent_link, to_field,
Expand All @@ -78,7 +96,9 @@ def formfield(self, **kwargs):
queryset = self.remote_field.model._default_manager.using(db)
defaults = {
'widget': PrivateDocumentWidget,
'form_class': ModelChoiceField,
'related_field': self.related_query_name(),
'view_name': self.view_name,
'form_class': PrivateDocumentModelChoiceField,
'queryset': queryset,
'to_field_name': self.remote_field.field_name,
}
Expand Down
17 changes: 4 additions & 13 deletions bluebottle/files/templates/widgets/document.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
<!-- We need this to show the select icons properly -->
<select name="{{ widget.name }}"{% include "django/forms/widgets/attrs.html" %}>
{% for group_name, group_choices, group_index in widget.optgroups %}
{% for option in group_choices %}
{% if option.selected %}
{% include option.template_name with widget=option %}
{{ option }}
{{ option.value }}
{% endif %}
{% endfor %}
{% endfor %}
</select>
<br>
{% load i18n %}
{% if download_link %}
<a href="{{download_link}}" class="private-document-link"/>{% trans "Download" %}</a>
{% endif %}
10 changes: 8 additions & 2 deletions bluebottle/files/widgets.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.forms import Select
from bluebottle.utils.utils import reverse_signed


class ImageWidget(Select):
Expand Down Expand Up @@ -32,9 +33,14 @@ class PrivateDocumentWidget(Select):

def get_context(self, name, value, attrs):
context = super(PrivateDocumentWidget, self).get_context(name, value, attrs)

if value:
from bluebottle.files.models import PrivateDocument
context['file'] = PrivateDocument.objects.get(pk=value).file
document = PrivateDocument.objects.get(pk=value)
context['download_link'] = reverse_signed(
self.attrs['view_name'],
args=(getattr(document, f"{self.attrs['related_field']}_set").first().pk, )
)
else:
context['file'] = None
context['download_link'] = None
return context
4 changes: 3 additions & 1 deletion bluebottle/funding/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,9 @@ def __str__(self):


class PlainPayoutAccount(PayoutAccount):
document = PrivateDocumentField(blank=True, null=True, on_delete=models.deletion.SET_NULL)
document = PrivateDocumentField(
blank=True, null=True, on_delete=models.deletion.SET_NULL, view_name='kyc-document'
)

ip_address = models.GenericIPAddressField(_('IP address'), blank=True, null=True, default=None)

Expand Down
1 change: 1 addition & 0 deletions bluebottle/time_based/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,7 @@ class PeriodParticipantAdmin(ContributorChildAdmin):
inlines = ContributorChildAdmin.inlines + [TimeContributionInlineAdmin]
readonly_fields = ContributorChildAdmin.readonly_fields + ['total']
fields = ContributorChildAdmin.fields + ['total', 'motivation', 'current_period', 'document']
raw_id_fields = ContributorChildAdmin.raw_id_fields + ('document', )
list_display = ['__str__', 'activity_link', 'status']

def total(self, obj):
Expand Down
4 changes: 2 additions & 2 deletions bluebottle/time_based/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ class Meta:

class DateParticipant(Participant):
motivation = models.TextField(blank=True, null=True)
document = PrivateDocumentField(blank=True, null=True)
document = PrivateDocumentField(blank=True, null=True, view_name='date-participant-document')

class Meta():
verbose_name = _("Participant on a date")
Expand All @@ -606,7 +606,7 @@ class JSONAPIMeta:

class PeriodParticipant(Participant, Contributor):
motivation = models.TextField(blank=True, null=True)
document = PrivateDocumentField(blank=True, null=True)
document = PrivateDocumentField(blank=True, null=True, view_name='period-participant-document')

current_period = models.DateField(null=True, blank=True)

Expand Down
16 changes: 16 additions & 0 deletions bluebottle/time_based/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.utils.timezone import now
from pytz import UTC

from bluebottle.files.tests.factories import PrivateDocumentFactory
from bluebottle.initiatives.models import InitiativePlatformSettings
from bluebottle.initiatives.tests.factories import InitiativeFactory
from bluebottle.offices.tests.factories import LocationFactory
Expand Down Expand Up @@ -219,6 +220,21 @@ def test_adjusting_contribution(self):
self.assertEqual(page.status, '200 OK')
self.assertTrue('This field is required.' in page.text)

def test_document(self):
self.participant.document = PrivateDocumentFactory.create()
self.participant.save()

self.url = reverse('admin:time_based_dateparticipant_change', args=(self.participant.id,))
page = self.app.get(self.url)
self.assertEqual(page.status, '200 OK')

link = page.html.find("a", {'class': 'private-document-link'})
self.assertTrue(
link.attrs['href'].startswith(
reverse('date-participant-document', args=(self.participant.pk, ))
)
)


class TestSkillAdmin(BluebottleAdminTestCase):

Expand Down
1 change: 1 addition & 0 deletions bluebottle/time_based/urls/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
url(r'^/participants/date/transitions$',
DateParticipantTransitionList.as_view(),
name='date-participant-transition-list'),

url(r'^/participants/date/(?P<pk>\d+)/document$',
DateParticipantDocumentDetail.as_view(),
name='date-participant-document'),
Expand Down

0 comments on commit 0682140

Please sign in to comment.