Revision 8759c981
Added by Serghei Mihai almost 10 years ago
| corbo/api_views.py | ||
|---|---|---|
|
# You should have received a copy of the GNU Affero General Public License
|
||
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||
|
|
||
|
import json
|
||
|
from collections import defaultdict
|
||
|
|
||
|
from django.core.exceptions import PermissionDenied
|
||
|
from django.db.models import Q
|
||
|
from django.db import transaction
|
||
|
|
||
|
from rest_framework.views import APIView
|
||
|
from rest_framework.response import Response
|
||
|
|
||
| ... | ... | |
|
'transports': transports}
|
||
|
newsletters.append(newsletter)
|
||
|
return Response({'data': newsletters})
|
||
|
|
||
|
|
||
|
class SubscriptionsView(APIView):
|
||
|
|
||
|
def get_subscriptions(self, email, uuid=None):
|
||
|
subscriptions = defaultdict(dict)
|
||
|
identifier = 'mailto:'+email
|
||
|
for s in Subscription.objects.filter(Q(identifier=identifier)|Q(uuid=uuid)):
|
||
|
cat_id = s.category.pk
|
||
|
subscriptions[cat_id]['id'] = str(cat_id)
|
||
|
subscriptions[cat_id]['text'] = s.category.name
|
||
|
transport_id, transport_name = identifier.split(':')
|
||
|
transport = {'id': transport_id,
|
||
|
'text': transport_id}
|
||
|
if 'transports' in subscriptions[cat_id]:
|
||
|
subscriptions[cat_id]['transports'].append(transport)
|
||
|
else:
|
||
|
subscriptions[cat_id]['transports'] = [transport]
|
||
|
|
||
|
return subscriptions.values()
|
||
|
|
||
|
@transaction.atomic
|
||
|
def update_subscriptions(self, category_id, transports, email, uuid=None):
|
||
|
uuid = uuid or u''
|
||
|
identifier = u'mailto:'
|
||
|
if email:
|
||
|
identifier += email
|
||
|
try:
|
||
|
cat = Category.objects.get(pk=category_id)
|
||
|
except Category.DoesNotExist:
|
||
|
return
|
||
|
|
||
|
subcriptions = cat.subscription_set
|
||
|
|
||
|
if email:
|
||
|
if 'mailto' in transports:
|
||
|
subcriptions.get_or_create(identifier=identifier, uuid=uuid)
|
||
|
if uuid:
|
||
|
subcriptions.exclude(uuid=uuid).filter(identifier=identifier).delete()
|
||
|
else:
|
||
|
subcriptions.filter(identifier=identifier).delete()
|
||
|
|
||
|
if uuid:
|
||
|
if 'homepage' in transports:
|
||
|
subcriptions.get_or_create(uuid=uuid)
|
||
|
else:
|
||
|
subcriptions.filter(identifier='', uuid=uuid).delete()
|
||
|
|
||
|
def get(self, request):
|
||
|
email = request.GET.get('email')
|
||
|
uuid = request.GET.get('uuid')
|
||
|
if not email:
|
||
|
raise PermissionDenied('Email parameter required')
|
||
|
return Response({'data': self.get_subscriptions(email, uuid)})
|
||
|
|
||
|
def post(self, request):
|
||
|
email = request.GET.get('email')
|
||
|
uuid = request.GET.get('uuid')
|
||
|
if not email:
|
||
|
raise PermissionDenied('Email parameter required')
|
||
|
data = json.loads(request.body)
|
||
|
for subscription in data:
|
||
|
self.update_subscriptions(subscription['id'], subscription['transports'],
|
||
|
email, uuid)
|
||
|
return Response({'data': True})
|
||
|
|
||
|
def delete(self, request):
|
||
|
email = request.GET.get('email')
|
||
|
uuid = request.GET.get('uuid')
|
||
|
if not email:
|
||
|
raise PermissionDenied('Email parameter required')
|
||
|
|
||
|
for subscription in self.get_subscriptions(email):
|
||
|
self.update_subscriptions(subscription['id'], [], email, uuid)
|
||
|
return Response({'data': True})
|
||
Also available in: Unified diff
api: subscriptions management endpoint (#10794)