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

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

# Copyright 2013 The Distro Tracker Developers 

# See the COPYRIGHT file at the top-level directory of this distribution and 

# at https://deb.li/DTAuthors 

# 

# This file is part of Distro Tracker. It is subject to the license terms 

# in the LICENSE file found in the top-level directory of this 

# distribution and at https://deb.li/DTLicense. No part of Distro Tracker, 

# including this file, may be copied, modified, propagated, or distributed 

# except according to the terms contained in the LICENSE file. 

""" 

Implements all commands which deal with teams. 

""" 

 

from distro_tracker.core.models import Team, UserEmail 

from distro_tracker.core.utils import get_or_none 

from distro_tracker.mail.control.commands.base import Command 

from distro_tracker.mail.control.commands.confirmation import needs_confirmation 

 

 

@needs_confirmation 

class JoinTeam(Command): 

""" 

Command which lets users join an existing public team. 

""" 

META = { 

'description': """join-team <team-slug> [<email>] 

Adds <email> to team with the slug given by <team-slug>. If 

<email> is not given, it adds the From address email to the team. 

If the team is not public or it does not exist, a warning is 

returned.""", 

'name': 'join-team', 

} 

REGEX_LIST = ( 

r'\s+(?P<team_slug>\S+)(?:\s+(?P<email>\S+))?$', 

) 

 

def __init__(self, team_slug, email): 

super(JoinTeam, self).__init__() 

self.user_email = email 

self.team_slug = team_slug 

 

def get_team_and_user(self): 

team = get_or_none(Team, slug=self.team_slug) 

if not team: 

self.error('Team with the slug "%s" does not exist.', 

self.team_slug) 

return 

if not team.public: 

self.error("The given team is not public. " 

"Please contact %s if you wish to join", 

team.owner.main_email) 

return 

 

user_email, _ = UserEmail.objects.get_or_create(email=self.user_email) 

if user_email in team.members.all(): 

self.warning("You are already a member of the team.") 

return 

 

return team, user_email 

 

def pre_confirm(self): 

if not self.validate_email(self.user_email): 

self.warning('%s is not a valid email.', self.user_email) 

return False 

 

packed = self.get_team_and_user() 

if packed is None: 

return False 

 

self.reply('A confirmation mail has been sent to %s', self.user_email) 

return True 

 

def get_command_text(self): 

return super(JoinTeam, self).get_command_text( 

self.team_slug, self.user_email) 

 

def handle(self): 

packed = self.get_team_and_user() 

79 ↛ 80line 79 didn't jump to line 80, because the condition on line 79 was never true if packed is None: 

return 

team, user_email = packed 

team.add_members([user_email]) 

self.reply('You have successfully joined the team "%s"', team) 

 

 

@needs_confirmation 

class LeaveTeam(Command): 

""" 

Command which lets users leave a team they are already a member of. 

""" 

META = { 

'description': """leave-team <team-slug> [<email>] 

Removes <email> from the team with the slug given by <team-slug>. If 

<email> is not given, it uses the From address email. 

If the user is not a member of the team, a warning is returned.""", 

'name': 'leave-team', 

} 

REGEX_LIST = ( 

r'\s+(?P<team_slug>\S+)(?:\s+(?P<email>\S+))?$', 

) 

 

def __init__(self, team_slug, email): 

super(LeaveTeam, self).__init__() 

self.user_email = email 

self.team_slug = team_slug 

 

def get_team_and_user(self): 

team = get_or_none(Team, slug=self.team_slug) 

if not team: 

self.error('Team with the slug "%s" does not exist.', 

self.team_slug) 

return 

user_email = get_or_none(UserEmail, email=self.user_email) 

if not user_email or user_email not in team.members.all(): 

self.warning("You are not a member of the team.") 

return 

 

return team, user_email 

 

def pre_confirm(self): 

packed = self.get_team_and_user() 

if packed is None: 

return False 

 

self.reply('A confirmation mail has been sent to %s', self.user_email) 

return True 

 

def get_command_text(self): 

return super(LeaveTeam, self).get_command_text( 

self.team_slug, self.user_email) 

 

def handle(self): 

packed = self.get_team_and_user() 

134 ↛ 135line 134 didn't jump to line 135, because the condition on line 134 was never true if packed is None: 

return 

team, user_email = packed 

team.remove_members([user_email]) 

self.reply('You have successfully left the team "%s" (slug: %s)', 

team, team.slug) 

 

 

class ListTeamPackages(Command): 

""" 

Lists all the packages of a particular team, provided that the team is 

public or the email doing the query is a member of the team. 

""" 

META = { 

'description': """list-team-packages <team-slug> 

Lists all packages of the team with the slug given by <team-slug>. 

If the team is private, the packages are returned only if the From email 

is a member of the team.""", 

'name': 'list-team-packages', 

} 

REGEX_LIST = ( 

r'\s+(?P<team_slug>\S+)$', 

) 

 

def __init__(self, team_slug): 

super(ListTeamPackages, self).__init__() 

self.team_slug = team_slug 

 

@property 

def user_email(self): 

return self.context['email'] 

 

def get_team(self): 

team = get_or_none(Team, slug=self.team_slug) 

168 ↛ 169line 168 didn't jump to line 169, because the condition on line 168 was never true if not team: 

self.error('Team with the slug "%s" does not exist.', 

self.team_slug) 

return 

return team 

 

def get_command_text(self): 

return super(ListTeamPackages, self).get_command_text( 

self.team_slug) 

 

def handle(self): 

team = self.get_team() 

180 ↛ 181line 180 didn't jump to line 181, because the condition on line 180 was never true if not team: 

return 

if not team.public: 

user_email = get_or_none(UserEmail, email=self.user_email) 

if not user_email or user_email not in team.members.all(): 

self.error( 

"The team is private. " 

"Only team members can see its packages.") 

return 

 

self.reply("Packages found in team %s:", team) 

self.list_reply(package for package in 

team.packages.all().order_by('name')) 

 

 

class WhichTeams(Command): 

""" 

Returns a list of teams that the given email is a member of. 

""" 

META = { 

'description': """which-teams [<email>] 

Lists all teams that <email> is a member of. If <email> is not given, the 

sender's email is used.""", 

'name': 'which-teams', 

} 

REGEX_LIST = ( 

r'(?:\s+(?P<email>\S+))?$', 

) 

 

def __init__(self, email): 

super(WhichTeams, self).__init__() 

self.user_email = email 

 

def handle(self): 

user_email = get_or_none(UserEmail, email=self.user_email) 

 

if not user_email or user_email.teams.count() == 0: 

self.warning("%s is not a member of any team.", self.user_email) 

else: 

self.reply("Teams that %s is a member of:", self.user_email) 

self.list_reply( 

team.slug 

for team in user_email.teams.all().order_by('name'))