9274651eb326497b518e8f78036470fbfd0f8e7a
[mygpo.git] / mygpo / web / views / device.py
blob9274651eb326497b518e8f78036470fbfd0f8e7a
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, HttpResponseNotAllowed, 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
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 def edit(request, device_id):
81 device = get_object_or_404(Device, id=device_id, user=request.user)
82 success = False
83 error_message = ''
85 if request.method == 'POST':
86 device_form = DeviceForm(request.POST)
88 if device_form.is_valid():
89 device.name = device_form.cleaned_data['name']
90 device.type = device_form.cleaned_data['type']
91 device.uid = device_form.cleaned_data['uid']
92 try:
93 device.save()
94 success = True
95 except IntegrityError, ie:
96 device = Device.objects.get(pk=device_id)
97 error_message = _('You can\'t use the same Device ID for two devices.')
99 else:
100 device_form = DeviceForm({
101 'name': device.name,
102 'type': device.type,
103 'uid' : device.uid
106 return render_to_response('device-edit.html', {
107 'device': device,
108 'device_form': device_form,
109 'success': success,
110 'error_message': error_message,
111 }, context_instance=RequestContext(request))
116 @manual_gc
117 @login_required
118 def opml(request, device_id):
119 device = get_object_or_404(Device, id=device_id, user=request.user)
121 response = simple.format_podcast_list(simple.get_subscriptions(request.user, device.uid), 'opml', request.user.username)
122 response['Content-Disposition'] = 'attachment; filename=%s.opml' % device.uid
123 return response
126 @manual_gc
127 @login_required
128 def delete(request, device_id):
129 if request.method != 'POST':
130 return HttpResponseNotAllowed(['POST'])
132 device = Device.objects.get(pk=device_id)
133 device.deleted = True
134 device.save()
136 return HttpResponseRedirect('/devices/')
139 def delete_permanently(request, device_id):
141 device = get_object_or_404(Device, pk=device_id, user=request.user)
143 SubscriptionAction.objects.filter(device=device).delete()
144 EpisodeAction.objects.filter(device=device).delete()
145 BackendSubscription.objects.filter(device=device).delete()
146 Listener.objects.filter(device=device).delete()
147 device.delete()
149 return HttpResponseRedirect('/devices/')
151 @manual_gc
152 @login_required
153 def undelete(request, device_id):
154 device = get_object_or_404(Device, pk=device_id, user=request.user)
156 device.deleted = False
157 device.save()
159 return HttpResponseRedirect('/device/%s' % device.id)
162 @manual_gc
163 @login_required
164 def sync(request, device_id):
166 if request.method != 'POST':
167 return HttpResponseNotAllowed(['POST'])
169 form = SyncForm(request.POST)
170 if not form.is_valid():
171 return HttpResponseBadRequest('invalid')
173 try:
174 target = form.get_target()
176 device = Device.objects.get(pk=device_id)
177 device.sync_with(target)
179 except ValueError, e:
180 log('error while syncing device %s: %s' % (device_id, e))
182 return HttpResponseRedirect('/device/%s' % device_id)
185 @manual_gc
186 @login_required
187 def unsync(request, device_id):
188 if request.method != 'GET':
189 return HttpResponseNotAllowed(['GET'])
191 dev = Device.objects.get(pk=device_id)
193 try:
194 dev.unsync()
195 except ValueError, e:
196 return show(request, device_id, e)
198 return HttpResponseRedirect('/device/%s' % device_id)
201 from mygpo.web import views
202 history = views.history