Merge pull request #793 from gpodder/remove-advertise
[mygpo.git] / mygpo / api / backend.py
bloba7620ac3b2f43955aa3b344c4b9aabba754b26ef
1 import uuid
3 from django.db import transaction, IntegrityError
5 from mygpo.users.settings import STORE_UA
6 from mygpo.users.models import Client
8 import logging
10 logger = logging.getLogger(__name__)
13 def get_device(user, uid, user_agent, undelete=True):
14 """
15 Loads or creates the device indicated by user, uid.
17 If the device has been deleted and undelete=True, it is undeleted.
18 """
20 store_ua = user.profile.settings.get_wksetting(STORE_UA)
22 # list of fields to update -- empty list = no update
23 update_fields = []
25 try:
26 with transaction.atomic():
27 client = Client(id=uuid.uuid1(), user=user, uid=uid)
28 client.clean_fields()
29 client.clean()
30 client.save()
32 except IntegrityError:
33 client = Client.objects.get(user=user, uid=uid)
35 if client.deleted and undelete:
36 client.deleted = False
37 update_fields.append("deleted")
39 if store_ua and user_agent and client.user_agent != user_agent:
40 client.user_agent = user_agent
41 update_fields.append("user_agent")
43 if update_fields:
44 client.save(update_fields=update_fields)
46 return client