1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
# -*- coding: utf-8 -*-
from launchpadlib.launchpad import Launchpad
from django.conf import settings
import os
import urllib2
def is_debug_user(username):
try:
if settings.DEBUG and username in settings.DEBUG_USERS:
return True
except AttributeError:
return False
return False
def lp_login(lp_instance='production'):
cachedir = os.path.join(settings.PROJECT_PATH, 'lp_data/cache')
client_ident = getattr(settings, 'LP_PROJECT_NAME', "LoCo Team Portal")
try:
launchpad = Launchpad.login_anonymously(client_ident, lp_instance, cachedir)
except:
try:
# Support for launchpadlib pre 1.5.4.
launchpad = Launchpad.login(client_ident, "", "", lp_instance, cachedir)
except:
# Launchpad might be offline.
return None
return launchpad
def is_user_on_loco_council(user):
if not user.is_authenticated():
return False
if is_debug_user(user.username):
return True
if user.is_staff:
return True
lc_in_groups = user.groups.filter(name__exact='ubuntu-lococouncil')
return (lc_in_groups.count() == 1)
def is_team_member(user, team):
if not user.is_authenticated():
return False
if is_debug_user(user.username):
return True
user_in_groups = user.groups.filter(name__exact=team.lp_name)
return (user_in_groups.count() >= 1)
def is_admin_or_owner(username, team):
# check just using the LD data
if username == team.owner_profile.user.username:
return True
if team.admin_profiles.filter(user__username=username):
return True
if is_debug_user(username):
return True
return False
def get_mugshot_url(url):
# Not ideal, but until LP #713873
# or similar, we are in a hard spot.
# When this bug is fixed, this can be made cleaner.
try:
con = urllib2.urlopen(url)
con.close()
return url
except urllib2.HTTPError, e:
# 404 or some other issue that means we should default to False
return "https://launchpad.net/@@/person-logo"
def get_user_timezone(username, lp=None):
timezone = 'UTC'
if username is None:
return timezone
if not lp:
lp = lp_login()
if not lp:
return timezone
try:
lp_user = lp.people[username]
timezone = lp_user.timezone
except:
pass
return timezone
def get_permanent_openid_from_username(username):
import urllib
from openid.consumer.discover import OpenIDServiceEndpoint
url = "https://launchpad.net/~%s" % username
f = urllib.urlopen(url)
html = f.read()
services = OpenIDServiceEndpoint().fromHTML(url, html)
if services is not None and len(services) > 0:
services[0].local_id = services[0].local_id.replace('launchpad.net', 'ubuntu.com')
return services[0]
else:
return None
def set_user_openid(user, force=False):
from django_openid_auth.models import UserOpenID
openids = UserOpenID.objects.filter(user=user)
if len(openids) == 0 or force:
if len(openids) == 0:
openid_assoc = UserOpenID(user=user)
else:
openid_assoc = openids[0]
openid = get_permanent_openid_from_username(user.username)
if openid is not None:
claimed_by = UserOpenID.objects.filter(claimed_id=openid.local_id)
if bool(claimed_by):
if force:
claimed_by.delete()
else:
return False
openid_assoc.claimed_id = openid.local_id
openid_assoc.display_id = openid.local_id
openid_assoc.save()
return True
return False
|