remove unnecessary imports
[mygpo.git] / mygpo / web / views / device.py
blob3f9cb7661cb21179b263b316060f4df464b9b610
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, HttpResponseBadRequest, HttpResponseForbidden
20 from django.template import RequestContext
21 from mygpo.api.models import Device, EpisodeAction, SubscriptionAction
22 from mygpo.data.models import BackendSubscription, Listener
23 from mygpo.web.forms import DeviceForm, SyncForm
24 from django.utils.translation import ugettext as _
25 from django.contrib.auth.decorators import login_required
26 from django.shortcuts import get_object_or_404
27 from django.db import IntegrityError
28 from mygpo.log import log
29 from mygpo.api import simple
30 from mygpo.decorators import manual_gc, allowed_methods
33 @manual_gc
34 @login_required
35 def overview(request):
36 devices = Device.objects.filter(user=request.user,deleted=False).order_by('sync_group')
37 deleted_devices = Device.objects.filter(user=request.user,deleted=True)
39 return render_to_response('devicelist.html', {
40 'devices': devices,
41 'deleted_devices': deleted_devices,
42 }, context_instance=RequestContext(request))
45 @manual_gc
46 @login_required
47 def show(request, device_id, error_message=None):
48 device = Device.objects.get(pk=device_id, user=request.user)
50 if device.user != request.user:
51 return HttpResponseForbidden(_('You are not allowed to access this device'))
53 subscriptions = device.get_subscriptions()
54 synced_with = list(device.sync_group.devices()) if device.sync_group else []
55 if device in synced_with: synced_with.remove(device)
56 sync_form = SyncForm()
57 sync_form.set_targets(device.sync_targets(), _('Synchronize with the following devices'))
59 return render_to_response('device.html', {
60 'device': device,
61 'sync_form': sync_form,
62 'error_message': error_message,
63 'subscriptions': subscriptions,
64 'synced_with': synced_with,
65 'has_sync_targets': len(device.sync_targets()) > 0
66 }, context_instance=RequestContext(request))
69 @login_required
70 @allowed_methods(['GET', 'POST'])
71 def edit(request, device_id):
73 device = get_object_or_404(Device, id=device_id, user=request.user)
74 success = False
75 error_message = ''
77 if request.method == 'POST':
78 device_form = DeviceForm(request.POST)
80 if device_form.is_valid():
81 device.name = device_form.cleaned_data['name']
82 device.type = device_form.cleaned_data['type']
83 device.uid = device_form.cleaned_data['uid']
84 try:
85 device.save()
86 success = True
87 except IntegrityError, ie:
88 device = Device.objects.get(pk=device_id)
89 error_message = _('You can\'t use the same Device ID for two devices.')
91 else:
92 device_form = DeviceForm({
93 'name': device.name,
94 'type': device.type,
95 'uid' : device.uid
98 return render_to_response('device-edit.html', {
99 'device': device,
100 'device_form': device_form,
101 'success': success,
102 'error_message': error_message,
103 }, context_instance=RequestContext(request))
108 @manual_gc
109 @login_required
110 def opml(request, device_id):
111 device = get_object_or_404(Device, id=device_id, user=request.user)
113 response = simple.format_podcast_list(simple.get_subscriptions(request.user, device.uid), 'opml', request.user.username)
114 response['Content-Disposition'] = 'attachment; filename=%s.opml' % device.uid
115 return response
118 @manual_gc
119 @login_required
120 @allowed_methods(['POST'])
121 def delete(request, device_id):
122 device = Device.objects.get(pk=device_id)
123 device.deleted = True
124 device.save()
126 return HttpResponseRedirect('/devices/')
129 def delete_permanently(request, device_id):
131 device = get_object_or_404(Device, pk=device_id, user=request.user)
133 SubscriptionAction.objects.filter(device=device).delete()
134 EpisodeAction.objects.filter(device=device).delete()
135 BackendSubscription.objects.filter(device=device).delete()
136 Listener.objects.filter(device=device).delete()
137 device.delete()
139 return HttpResponseRedirect('/devices/')
141 @manual_gc
142 @login_required
143 def undelete(request, device_id):
144 device = get_object_or_404(Device, pk=device_id, user=request.user)
146 device.deleted = False
147 device.save()
149 return HttpResponseRedirect('/device/%s' % device.id)
152 @manual_gc
153 @login_required
154 @allowed_methods(['POST'])
155 def sync(request, device_id):
156 form = SyncForm(request.POST)
157 if not form.is_valid():
158 return HttpResponseBadRequest('invalid')
160 try:
161 target = form.get_target()
163 device = Device.objects.get(pk=device_id)
164 device.sync_with(target)
166 except ValueError, e:
167 log('error while syncing device %s: %s' % (device_id, e))
169 return HttpResponseRedirect('/device/%s' % device_id)
172 @manual_gc
173 @login_required
174 @allowed_methods(['GET'])
175 def unsync(request, device_id):
176 dev = Device.objects.get(pk=device_id)
178 try:
179 dev.unsync()
180 except ValueError, e:
181 return show(request, device_id, e)
183 return HttpResponseRedirect('/device/%s' % device_id)
186 from mygpo.web import views
187 history = views.history