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
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', {
41 'deleted_devices': deleted_devices
,
42 }, context_instance
=RequestContext(request
))
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', {
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
))
70 @allowed_methods(['GET', 'POST'])
71 def edit(request
, device_id
):
73 device
= get_object_or_404(Device
, id=device_id
, user
=request
.user
)
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']
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.')
92 device_form
= DeviceForm({
98 return render_to_response('device-edit.html', {
100 'device_form': device_form
,
102 'error_message': error_message
,
103 }, context_instance
=RequestContext(request
))
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
120 @allowed_methods(['POST'])
121 def delete(request
, device_id
):
122 device
= Device
.objects
.get(pk
=device_id
)
123 device
.deleted
= True
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()
139 return HttpResponseRedirect('/devices/')
143 def undelete(request
, device_id
):
144 device
= get_object_or_404(Device
, pk
=device_id
, user
=request
.user
)
146 device
.deleted
= False
149 return HttpResponseRedirect('/device/%s' % device
.id)
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')
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
)
174 @allowed_methods(['GET'])
175 def unsync(request
, device_id
):
176 dev
= Device
.objects
.get(pk
=device_id
)
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