user.py 1.3 KB
Newer Older
Linus Lewandowski's avatar
Linus Lewandowski committed
1
from django.urls import reverse
Linus Lewandowski's avatar
Linus Lewandowski committed
2 3
from django.utils.translation import gettext_lazy as _

4 5
from .models import ExternalIdentity

Linus Lewandowski's avatar
Linus Lewandowski committed
6

7
class UserMixin:
Linus Lewandowski's avatar
Linus Lewandowski committed
8

Linus Lewandowski's avatar
Linus Lewandowski committed
9 10 11 12
	@property
	def url(self):
		return reverse('extauth:account-home', args=[self.id])

13 14 15 16 17 18 19 20 21 22 23 24 25
	@property
	def emails(self):
		return [ei.email for ei in self.externalidentity_set.filter(provider__protocol="")]

	@emails.setter
	def emails(self, val):
		for ei in self.external_identities:
			if not ei.email in val:
				ei.delete()

		eis = [ExternalIdentity.objects.forced_get(email=email) for email in val]
		for ei in eis:
			ei.user = self
26
			ei.trusted = True
27 28
			ei.save()

29 30
	@property
	def email(self):
31 32
		# TODO Provide a way to choose the main email address.
		try:
33
			return self.emails[0]
34
		except IndexError:
35
			return ''
Linus Lewandowski's avatar
Linus Lewandowski committed
36

37 38 39
	@property
	def external_identities(self):
		return self.externalidentity_set.all()
Linus Lewandowski's avatar
Linus Lewandowski committed
40

41 42 43
	@property
	def trusted_external_identities(self):
		return self.externalidentity_set.filter(trusted=True)
Linus Lewandowski's avatar
Linus Lewandowski committed
44

45 46 47 48 49 50
	@property
	def login_with(self):
		login_with = list(self.trusted_external_identities.exclude(provider__protocol=""))
		if self.has_usable_password():
			login_with.append(_("password"))
		return login_with
Linus Lewandowski's avatar
Linus Lewandowski committed
51

52 53 54 55
	@property
	def reset_password_with(self):
		reset_password_with = list(self.trusted_external_identities.filter(provider__protocol=""))
		return reset_password_with