Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
amschaal committed Jan 17, 2020
1 parent 984b64a commit 80cf1b9
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 9 deletions.
16 changes: 16 additions & 0 deletions ezreg/api/filters.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from rest_framework import filters
import operator
from functools import reduce
from django.db.models.query_utils import Q

class MultiFilter(filters.BaseFilterBackend):
"""
Expand All @@ -16,4 +18,18 @@ def filter_queryset(self, request, queryset, view):
filters[mf]=val
if len(filters):
queryset = queryset.filter(**filters)
return queryset

class OrFilter(filters.BaseFilterBackend):
#in view, add property like:
#or_filters = {'registrant':['registration__first_name__icontains', 'registration__last_name__icontains', 'registration__email__icontains']}
def filter_queryset(self, request, queryset, view):
or_filters = getattr(view, 'or_filters',[])
filters = {}
for k, f in or_filters.items():
val = view.request.query_params.get(k,None)
if val:
list_of_Q = [Q(**{key: val}) for key in f]
queryset = queryset.filter(reduce(operator.or_, list_of_Q))
# filters[k]=val
return queryset
7 changes: 6 additions & 1 deletion ezreg/api/serializers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from rest_framework import serializers
from ezreg.models import Price, PaymentProcessor, EventProcessor, EventPage, Registration,\
Event, Organizer
Event, Organizer, Refund
from mailqueue.models import MailerMessage

class JSONSerializerField(serializers.Field):
Expand Down Expand Up @@ -89,6 +89,11 @@ class Meta:
model = MailerMessage
fields = ('id','subject','to_address','bcc_address','content','html_content','sent','last_attempt','registration','event')

class RefundSerializer(serializers.ModelSerializer):
class Meta:
model = Refund
exclude = []

# class EventProcessorSerializer(serializers.ModelSerializer):
# class Meta:
# model = EventProcessor
Expand Down
21 changes: 18 additions & 3 deletions ezreg/api/views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from rest_framework import viewsets, status
from ezreg.api.serializers import PriceSerializer, PaymentProcessorSerializer,\
EventPageSerializer, RegistrationSerializer, MailerMessageSerializer,\
EventSerializer, DetailedEventSerializer
EventSerializer, DetailedEventSerializer, RefundSerializer
from ezreg.models import Price, PaymentProcessor, Event, EventProcessor,\
EventPage, Registration, OrganizerUserPermission
EventPage, Registration, OrganizerUserPermission, Refund
from rest_framework.decorators import api_view, action
from rest_framework.response import Response
from mailqueue.models import MailerMessage
Expand All @@ -15,7 +15,7 @@
from ezreg.api.permissions import EventPermission
from django.utils import timezone
from django.http.response import HttpResponse
from ezreg.api.filters import MultiFilter
from ezreg.api.filters import MultiFilter, OrFilter
from django_logger.models import Log
from django.db.models.aggregates import Count

Expand Down Expand Up @@ -129,6 +129,21 @@ def get_queryset(self):
POST {"processors":{3:{"enabled":true},5:{"enabled":true}}} where JSON object keys are PaymentProcessor ids
"""

# Not currently used....
class RefundViewset(viewsets.ReadOnlyModelViewSet):
# queryset = MailerMessage.objects.all().prefetch_related('registrations')
filter_backends = viewsets.ReadOnlyModelViewSet.filter_backends + [OrFilter]
or_filters = {'registrant':['registration__first_name__icontains', 'registration__last_name__icontains', 'registration__email__icontains']}
serializer_class = RefundSerializer
filter_fields = {'registration__id':['exact'],'registration__event':['exact'],'status':['exact','icontains'],'registration__payment__external_id':['icontains'],'registration__payment__external_id':['icontains'],'registration__first_name':['icontains'],'registration__last_name':['icontains']}
def get_queryset(self):
if self.request.user.is_staff:
qs = Refund.objects.all()
else:
qs = Refund.objects.filter(registration__event__organizer__user_permissions__permission=OrganizerUserPermission.PERMISSION_ADMIN,registration__event__organizer__user_permissions__user=self.request.user)
return qs.select_related('registration', 'registration__payment', 'requester', 'admin')


@api_view(['POST','GET'])
@event_access_decorator([OrganizerUserPermission.PERMISSION_ADMIN])
def event_payment_processors(request, event):
Expand Down
22 changes: 20 additions & 2 deletions ezreg/templates/ezreg/admin/pending_refunds.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{% extends 'base.html' %}
{% block content %}
<h3>Pending refunds</h3>
{% if refunds|length > 0%}
{% if pending_refunds|length > 0%}
<table class="table table-bordered table-striped table-condensed">
<tr><th>Requested</th><th>Event</th><th>Registration</th><th>External ID<th>Requester</th><th>Amount remaining</th><th>Refund</th><th>Status</th><th>Action</th></tr>
{% for r in refunds %}
{% for r in pending_refunds %}
<tr>
<td>{{r.requested}}</td>
<td><a href="{% url 'manage_event' event=r.registration.event.id %}">{{r.registration.event}}</a></td>
Expand All @@ -21,4 +21,22 @@ <h3>Pending refunds</h3>
<p>There are no pending refunds at the moment</p>
{% endif %}

{% if past_refunds|length > 0%}
<h3>Recent refunds</h3>
<table class="table table-bordered table-striped table-condensed">
<tr><th>Requested</th><th>Event</th><th>Registration</th><th>External ID<th>Requester</th><th>Amount remaining</th><th>Refund</th><th>Status</th></tr>
{% for r in past_refunds %}
<tr>
<td>{{r.requested}}</td>
<td><a href="{% url 'manage_event' event=r.registration.event.id %}">{{r.registration.event}}</a></td>
<td><a href="{% url 'registration' id=r.registration.id %}">{{r.registration.display}}</a></td>
<td>{{r.registration.payment.external_id}}</td>
<td>{{r.requester.display}}</td>
<td>${{r.registration.payment.amount_remaining}}</td>
<td>${{r.amount}}</td>
<td>{{r.status}} {% if r.updated %}{{r.updated}}{% endif %}</td>
{% endfor %}
</table>
{% endif %}

{% endblock %}
4 changes: 3 additions & 1 deletion ezreg/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
from cas import views as cas_views
from ezreg.jsutils import jsurls
from ezreg.api.views import PriceViewset, PaymentProcessorViewset, \
EventPageViewset, RegistrationViewset, MailerMessageViewset, EventViewset
EventPageViewset, RegistrationViewset, MailerMessageViewset, EventViewset,\
RefundViewset
from ezreg.registration import RegistrationWizard
from django_logger.api.views import LogViewset
from ezreg.feeds import EventsFeed, UpcomingEventsFeed, PastEventsFeed
Expand All @@ -38,6 +39,7 @@
router.register(r'registrations', RegistrationViewset, 'Registration')
router.register(r'emails', MailerMessageViewset, 'Email')
router.register(r'logs', LogViewset, 'Log')
router.register(r'refunds', RefundViewset, 'Refund')

urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
Expand Down
5 changes: 3 additions & 2 deletions ezreg/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,9 @@ def request_refund(request, id):

@user_passes_test(lambda u: u.is_staff)
def pending_refunds(request):
refunds = Refund.objects.filter(status=Refund.STATUS_PENDING)
return render(request, 'ezreg/admin/pending_refunds.html', {'refunds':refunds})
pending_refunds = Refund.objects.filter(status=Refund.STATUS_PENDING)
past_refunds = Refund.objects.exclude(status=Refund.STATUS_PENDING)
return render(request, 'ezreg/admin/pending_refunds.html', {'pending_refunds':pending_refunds, 'past_refunds': past_refunds})

@user_passes_test(lambda u: u.is_staff)
def complete_refund(request, id):
Expand Down

0 comments on commit 80cf1b9

Please sign in to comment.