[UserSettings] refactor, add tests
[mygpo.git] / mygpo / api / backend.py
blobd683b29460652ffc1133c77448778d04cf01d977
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 import uuid
20 from mygpo.users.settings import STORE_UA
21 from mygpo.users.models import Client
24 def get_device(user, uid, user_agent, undelete=True):
25 """
26 Loads or creates the device indicated by user, uid.
28 If the device has been deleted and undelete=True, it is undeleted.
29 """
31 store_ua = user.profile.settings.get_wksetting(STORE_UA)
33 save = False
35 client, created = Client.objects.get_or_create(user=user, uid=uid,
36 defaults = {
37 'id': uuid.uuid1()
40 if client.deleted and undelete:
41 client.deleted = False
42 save = True
44 if store_ua and user_agent and client.user_agent != user_agent:
45 client.user_agent = user_agent
47 if save:
48 client.save()
50 return client