3 from django
.db
import transaction
, IntegrityError
5 from mygpo
.users
.settings
import STORE_UA
6 from mygpo
.users
.models
import Client
10 logger
= logging
.getLogger(__name__
)
13 def get_device(user
, uid
, user_agent
, undelete
=True):
15 Loads or creates the device indicated by user, uid.
17 If the device has been deleted and undelete=True, it is undeleted.
20 store_ua
= user
.profile
.settings
.get_wksetting(STORE_UA
)
22 # list of fields to update -- empty list = no update
26 with transaction
.atomic():
27 client
= Client(id=uuid
.uuid1(), user
=user
, uid
=uid
)
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")
44 client
.save(update_fields
=update_fields
)