@allowed_methods to check for correct HTTP method
[mygpo.git] / mygpo / web / views / device.py
blob774e59d513ff912ad45929e26a24197effad3c17
2 # This file is part of my.gpodder.org.
4 # my.gpodder.org is free software: you can redistribute it and/or modify it
5 # under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or (at your
7 # option) any later version.
9 # my.gpodder.org is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
12 # License for more details.
14 # You should have received a copy of the GNU Affero General Public License
15 # along with my.gpodder.org. If not, see <http://www.gnu.org/licenses/>.
18 from django.shortcuts import render_to_response
19 from django.http import HttpResponseRedirect, HttpResponse, HttpResponseBadRequest, Http404, HttpResponseForbidden
20 from django.contrib.auth.models import User
21 from django.template import RequestContext
22 from mygpo.api.models import Podcast, Episode, Device, EpisodeAction, SubscriptionAction, ToplistEntry, EpisodeToplistEntry, Subscription, SuggestionEntry, SyncGroup, SUBSCRIBE_ACTION, UNSUBSCRIBE_ACTION, SubscriptionMeta
23 from mygpo.data.models import BackendSubscription, Listener
24 from mygpo.web.forms import DeviceForm, SyncForm
25 from django.forms import ValidationError
26 from django.utils.translation import ugettext as _
27 from django.contrib.auth.decorators import login_required
28 from django.shortcuts import get_object_or_404
29 from django.db import IntegrityError
30 from datetime import datetime, date, timedelta
31 from django.contrib.sites.models import Site
32 from mygpo.api.sanitizing import sanitize_url
33 from mygpo.log import log
34 from mygpo.utils import daterange
35 from mygpo.api import simple
36 from mygpo.decorators import manual_gc, allowed_methods
37 import re
38 import random
39 import string
42 @manual_gc
43 @login_required
44 def overview(request):
45 devices = Device.objects.filter(user=request.user,deleted=False).order_by('sync_group')
46 deleted_devices = Device.objects.filter(user=request.user,deleted=True)
48 return render_to_response('devicelist.html', {
49 'devices': devices,
50 'deleted_devices': deleted_devices,
51 }, context_instance=RequestContext(request))
54 @manual_gc
55 @login_required
56 def show(request, device_id, error_message=None):
57 device = Device.objects.get(pk=device_id, user=request.user)
59 if device.user != request.user:
60 return HttpResponseForbidden(_('You are not allowed to access this device'))
62 subscriptions = device.get_subscriptions()
63 synced_with = list(device.sync_group.devices()) if device.sync_group else []
64 if device in synced_with: synced_with.remove(device)
65 sync_form = SyncForm()
66 sync_form.set_targets(device.sync_targets(), _('Synchronize with the following devices'))
68 return render_to_response('device.html', {
69 'device': device,
70 'sync_form': sync_form,
71 'error_message': error_message,
72 'subscriptions': subscriptions,
73 'synced_with': synced_with,
74 'has_sync_targets': len(device.sync_targets()) > 0
75 }, context_instance=RequestContext(request))
78 @login_required
79 @allowed_methods(['GET', 'POST'])
80 def edit(request, device_id):
82 device = get_object_or_404(Device, id=device_id, user=request.user)
83 success = False
84 error_message = ''
86 if request.method == 'POST':
87 device_form = DeviceForm(request.POST)
89 if device_form.is_valid():
90 device.name = device_form.cleaned_data['name']
91 device.type = device_form.cleaned_data['type']
92 device.uid = device_form.cleaned_data['uid']
93 try:
94 device.save()
95 success = True
96 except IntegrityError, ie:
97 device = Device.objects.get(pk=device_id)
98 error_message = _('You can\'t use the same Device ID for two devices.')
100 else:
101 device_form = DeviceForm({
102 'name': device.name,
103 'type': device.type,
104 'uid' : device.uid
107 return render_to_response('device-edit.html', {
108 'device': device,
109 'device_form': device_form,
110 'success': success,
111 'error_message': error_message,
112 }, context_instance=RequestContext(request))
117 @manual_gc
118 @login_required
119 def opml(request, device_id):
120 device = get_object_or_404(Device, id=device_id, user=request.user)
122 response = simple.format_podcast_list(simple.get_subscriptions(request.user, device.uid), 'opml', request.user.username)
123 response['Content-Disposition'] = 'attachment; filename=%s.opml' % device.uid
124 return response
127 @manual_gc
128 @login_required
129 @allowed_methods(['POST'])
130 def delete(request, device_id):
131 device = Device.objects.get(pk=device_id)
132 device.deleted = True
133 device.save()
135 return HttpResponseRedirect('/devices/')
138 def delete_permanently(request, device_id):
140 device = get_object_or_404(Device, pk=device_id, user=request.user)
142 SubscriptionAction.objects.filter(device=device).delete()
143 EpisodeAction.objects.filter(device=device).delete()
144 BackendSubscription.objects.filter(device=device).delete()
145 Listener.objects.filter(device=device).delete()
146 device.delete()
148 return HttpResponseRedirect('/devices/')
150 @manual_gc
151 @login_required
152 def undelete(request, device_id):
153 device = get_object_or_404(Device, pk=device_id, user=request.user)
155 device.deleted = False
156 device.save()
158 return HttpResponseRedirect('/device/%s' % device.id)
161 @manual_gc
162 @login_required
163 @allowed_methods(['POST'])
164 def sync(request, device_id):
165 form = SyncForm(request.POST)
166 if not form.is_valid():
167 return HttpResponseBadRequest('invalid')
169 try:
170 target = form.get_target()
172 device = Device.objects.get(pk=device_id)
173 device.sync_with(target)
175 except ValueError, e:
176 log('error while syncing device %s: %s' % (device_id, e))
178 return HttpResponseRedirect('/device/%s' % device_id)
181 @manual_gc
182 @login_required
183 @allowed_methods(['GET'])
184 def unsync(request, device_id):
185 dev = Device.objects.get(pk=device_id)
187 try:
188 dev.unsync()
189 except ValueError, e:
190 return show(request, device_id, e)
192 return HttpResponseRedirect('/device/%s' % device_id)
195 from mygpo.web import views
196 history = views.history