~ronnie.vd.c/loco-team-portal/494950_db_approved

« back to all changes in this revision

Viewing changes to loco_directory/common/launchpad.py

  • Committer: Daniel Holbach
  • Date: 2009-12-21 11:03:49 UTC
  • Revision ID: daniel.holbach@canonical.com-20091221110349-bo2zmme5i2e4egif
start at revision 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
 
 
3
from launchpadlib.launchpad import Launchpad
 
4
from launchpadlib.credentials import Credentials
 
5
from launchpadlib.errors import HTTPError
 
6
from launchpadlib.launchpad import EDGE_SERVICE_ROOT
 
7
 
 
8
from teams.models import *
 
9
 
 
10
import settings
 
11
 
 
12
import os
 
13
import sys
 
14
 
 
15
def lp_login(lp_instance=EDGE_SERVICE_ROOT):
 
16
    cachedir = os.path.join(settings.PROJECT_PATH, 'lp_data/cache')
 
17
    creddir = os.path.join(settings.PROJECT_PATH, 'lp_data/lp_credentials')
 
18
    if not os.path.isdir(creddir):
 
19
        os.makedirs(creddir)
 
20
    cred = os.path.join(creddir, 'loco-directory.credentials')
 
21
    
 
22
    if os.path.exists(cred):
 
23
        credentials = Credentials()
 
24
        credentials.load(open(cred))
 
25
        launchpad = Launchpad(credentials, lp_instance, cachedir)
 
26
    else:
 
27
        try:
 
28
            launchpad = Launchpad.get_token_and_login('loco-directory',
 
29
                                                      lp_instance, cachedir)
 
30
        except HTTPError, e:
 
31
            print >> sys.stderr, 'Error connecting to Launchpad: %s' % str(e)
 
32
            sys.exit(1)
 
33
        f = open(cred, 'w')
 
34
        os.chmod(cred, 0600)
 
35
        launchpad.credentials.save(f)
 
36
        f.close()
 
37
    return launchpad
 
38
 
 
39
def is_user_on_loco_council(user):
 
40
    lp = lp_login()
 
41
    try:
 
42
        team = lp.people['ubuntu-lococouncil']
 
43
    except:
 
44
        print >> sys.stderr, 'Something went wrong.'
 
45
        return False
 
46
    return unicode(user) in [a.name for a in team.members]
 
47
 
 
48
def is_admin_or_owner(username, team):
 
49
    # check just using the LD data
 
50
    if username == team.owner:
 
51
        return True
 
52
    if team.admins.filter(lpid=username):
 
53
        return True
 
54
    
 
55
    # more complicated check :-/
 
56
    lp = lp_login()
 
57
    try:
 
58
        lp_team = lp.people[team.lp_name]
 
59
    except:
 
60
        print >> sys.stderr, 'Something went wrong.'
 
61
        return False
 
62
    if lp_team.team_owner.is_team:
 
63
        if filter(lambda a: a.name == username, lp_team.team_owner.members):
 
64
            return True
 
65
    return False
 
66