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

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

# Copyright 2013-2015 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. 

"""Views for the :mod:`distro_tracker.accounts` app.""" 

from django.conf import settings 

from django.core.exceptions import ValidationError 

from django.db.models import Prefetch 

from django.http import ( 

Http404, 

HttpResponseBadRequest, 

HttpResponseForbidden 

) 

from django.shortcuts import get_object_or_404, render, resolve_url 

from django.urls import reverse_lazy 

from django.utils.html import format_html 

from django.views.generic.base import View 

 

from distro_tracker.accounts.models import UserEmail 

from distro_tracker.core.models import ( 

EmailSettings, 

Keyword, 

Subscription, 

get_web_package 

) 

from distro_tracker.core.utils import ( 

distro_tracker_render_to_string, 

render_to_json_response 

) 

from distro_tracker.core.utils.http import safe_redirect 

 

from django_email_accounts import views as email_accounts_views 

from django_email_accounts.views import LoginRequiredMixin 

 

 

class ConfirmationRenderMixin(object): 

def get_confirmation_email_content(self, confirmation): 

return distro_tracker_render_to_string( 

self.confirmation_email_template, 

{'confirmation': confirmation} 

) 

 

 

class LoginView(email_accounts_views.LoginView): 

success_url = reverse_lazy('dtracker-accounts-profile') 

 

 

class LogoutView(email_accounts_views.LogoutView): 

success_url = reverse_lazy('dtracker-index') 

 

 

class RegisterUser(ConfirmationRenderMixin, email_accounts_views.RegisterUser): 

success_url = reverse_lazy('dtracker-accounts-register-success') 

 

confirmation_email_subject = '{name} Registration Confirmation'.format( 

name=settings.GET_INSTANCE_NAME()) 

confirmation_email_from_address = settings.DISTRO_TRACKER_CONTACT_EMAIL 

 

 

class RegistrationConfirmation(email_accounts_views.RegistrationConfirmation): 

success_url = reverse_lazy('dtracker-accounts-profile') 

message = 'You have successfully registered to the {name}'.format( 

name=settings.GET_INSTANCE_NAME()) 

 

 

class ResetPasswordView(ConfirmationRenderMixin, 

email_accounts_views.ResetPasswordView): 

success_url = reverse_lazy('dtracker-accounts-profile') 

 

 

class ForgotPasswordView(ConfirmationRenderMixin, 

email_accounts_views.ForgotPasswordView): 

success_url = reverse_lazy('dtracker-accounts-password-reset-success') 

email_subject = '{name} Password Reset Confirmation'.format( 

name=settings.GET_INSTANCE_NAME()) 

email_from_address = settings.DISTRO_TRACKER_CONTACT_EMAIL 

 

 

class ChangePersonalInfoView(email_accounts_views.ChangePersonalInfoView): 

success_url = reverse_lazy('dtracker-accounts-profile-modify') 

 

 

class PasswordChangeView(email_accounts_views.PasswordChangeView): 

success_url = reverse_lazy('dtracker-accounts-profile-password-change') 

 

 

class AccountProfile(email_accounts_views.AccountProfile): 

pass 

 

 

class ManageAccountEmailsView(ConfirmationRenderMixin, 

email_accounts_views.ManageAccountEmailsView): 

success_url = reverse_lazy('dtracker-accounts-manage-emails') 

merge_accounts_url = reverse_lazy('dtracker-accounts-merge-confirmation') 

 

confirmation_email_subject = 'Add Email To {name} Account'.format( 

name=settings.GET_INSTANCE_NAME()) 

confirmation_email_from_address = settings.DISTRO_TRACKER_CONTACT_EMAIL 

 

 

class AccountMergeConfirmView(ConfirmationRenderMixin, 

email_accounts_views.AccountMergeConfirmView): 

success_url = reverse_lazy('dtracker-accounts-merge-confirmed') 

confirmation_email_subject = 'Merge {name} Accounts'.format( 

name=settings.GET_INSTANCE_NAME()) 

confirmation_email_from_address = settings.DISTRO_TRACKER_CONTACT_EMAIL 

 

 

class AccountMergeFinalize(email_accounts_views.AccountMergeFinalize): 

success_url = reverse_lazy('dtracker-accounts-merge-finalized') 

 

 

class AccountMergeConfirmedView(email_accounts_views.AccountMergeConfirmedView): 

template_name = 'accounts/tracker-accounts-merge-confirmed.html' 

 

 

class ConfirmAddAccountEmail(email_accounts_views.ConfirmAddAccountEmail): 

pass 

 

 

class SubscriptionsView(LoginRequiredMixin, View): 

""" 

Displays a user's subscriptions. 

 

This includes both direct package subscriptions and team memberships. 

""" 

template_name = 'accounts/subscriptions.html' 

 

def get(self, request): 

user = request.user 

keyword_qs = Keyword.objects.order_by('name') 

# Ensure we have EmailSettings for all emails 

for user_email in UserEmail.objects.filter(user=user): 

EmailSettings.objects.get_or_create(user_email=user_email) 

user_emails = UserEmail.objects.filter(user=user).order_by( 

'email' 

).prefetch_related( 

Prefetch( 

'emailsettings__subscription_set___keywords', 

queryset=keyword_qs 

), 

Prefetch( 

'emailsettings__default_keywords', 

queryset=keyword_qs 

) 

) 

# Map users emails to the subscriptions of that email 

153 ↛ exitline 162 didn't finish the lambda on line 162 subscriptions = [ 

{ 

'email': user_email, 

'subscriptions': sorted([ 

subscription for subscription 

in user_email.emailsettings.subscription_set.all() 

], key=lambda sub: sub.package.name), 

'team_memberships': sorted([ 

membership for membership in user_email.membership_set.all() 

], key=lambda m: m.team.name) 

} 

for user_email in user_emails 

] 

# Initializing session variable if not set. 

request.session.setdefault('selected_emails', [str(user_emails[0])]) 

return render(request, self.template_name, { 

'subscriptions': subscriptions, 

'selected_emails': request.session['selected_emails'] 

}) 

 

 

class UserEmailsView(LoginRequiredMixin, View): 

""" 

Returns a JSON encoded list of the currently logged in user's emails. 

""" 

def get(self, request): 

user = request.user 

return render_to_json_response([ 

email.email for email in user.emails.all() 

]) 

 

 

class SubscribeUserToPackageView(LoginRequiredMixin, View): 

""" 

Subscribes the user to a package. 

 

The user whose email address is provided must currently be logged in. 

""" 

def post(self, request): 

package = request.POST.get('package', None) 

emails = request.POST.getlist('email', None) 

 

195 ↛ 196line 195 didn't jump to line 196, because the condition on line 195 was never true if not package or not emails: 

raise Http404 

 

# Remember selected emails via session variable 

request.session['selected_emails'] = emails 

 

# Check whether the logged in user is associated with the given emails 

users_emails = [e.email for e in request.user.emails.all()] 

for email in emails: 

if email not in users_emails: 

return HttpResponseForbidden() 

 

_pkg = get_web_package(package) 

_err = None 

 

if _pkg: 

try: 

for email in emails: 

Subscription.objects.create_for( 

package_name=package, 

email=email) 

except ValidationError as e: 

_err = e.message 

else: 

_err = format_html( 

"Package {pkg} does not exist.", 

pkg=package, 

) 

 

if request.is_ajax(): 

json_result = {'status': 'ok'} 

if _err is not None: 

json_result = { 

'status': 'failed', 

'error': _err, 

} 

return render_to_json_response(json_result) 

else: 

233 ↛ 235line 233 didn't jump to line 235, because the condition on line 233 was never false if _err: 

return HttpResponseBadRequest(_err) 

_next = request.POST.get('next', None) 

return safe_redirect( 

_next, 

resolve_url('dtracker-package-page', package_name=package), 

) 

 

 

class UnsubscribeUserView(LoginRequiredMixin, View): 

""" 

Unsubscribes the currently logged in user from the given package. 

An email can be optionally provided in which case only the given email is 

unsubscribed from the package, if the logged in user owns it. 

""" 

def post(self, request): 

if 'package' not in request.POST: 

raise Http404 

 

package = request.POST['package'] 

user = request.user 

 

if 'email' not in request.POST: 

# Unsubscribe all the user's emails from the package 

user_emails = UserEmail.objects.filter(user=user) 

qs = Subscription.objects.filter( 

email_settings__user_email__in=user_emails, 

package__name=package) 

else: 

# Unsubscribe only the given email from the package 

qs = Subscription.objects.filter( 

email_settings__user_email__email=request.POST['email'], 

package__name=package) 

 

qs.delete() 

 

269 ↛ 274line 269 didn't jump to line 274, because the condition on line 269 was never false if request.is_ajax(): 

return render_to_json_response({ 

'status': 'ok', 

}) 

else: 

_next = request.POST.get('next', None) 

return safe_redirect( 

_next, 

resolve_url('dtracker-package-page', package_name=package), 

) 

 

 

class UnsubscribeAllView(LoginRequiredMixin, View): 

""" 

The view unsubscribes the currently logged in user from all packages. 

If an optional ``email`` POST parameter is provided, only removes all 

subscriptions for the given emails. 

""" 

def post(self, request): 

user = request.user 

289 ↛ 290line 289 didn't jump to line 290, because the condition on line 289 was never true if 'email' not in request.POST: 

emails = user.emails.all() 

else: 

emails = user.emails.filter(email__in=request.POST.getlist('email')) 

 

# Remove all the subscriptions 

Subscription.objects.filter( 

email_settings__user_email__in=emails).delete() 

 

298 ↛ 303line 298 didn't jump to line 303, because the condition on line 298 was never false if request.is_ajax(): 

return render_to_json_response({ 

'status': 'ok', 

}) 

else: 

_next = request.POST.get('next', None) 

return safe_redirect( 

_next, 

resolve_url('dtracker-index'), 

) 

 

 

class ChooseSubscriptionEmailView(LoginRequiredMixin, View): 

""" 

Lets the user choose which email to subscribe to a package with. 

This is an alternative view when JS is disabled and the appropriate choice 

cannot be offered in a popup. 

""" 

template_name = 'accounts/choose-email.html' 

 

def get(self, request): 

if 'package' not in request.GET: 

raise Http404 

 

if not get_web_package(request.GET['package']): 

raise Http404 

 

return render(request, self.template_name, { 

'package': request.GET['package'], 

'emails': request.user.emails.all(), 

}) 

 

 

class ModifyKeywordsView(LoginRequiredMixin, View): 

""" 

Lets the logged-in user modify their default keywords or 

subscription-specific keywords. 

""" 

def get_keywords(self, keywords): 

""" 

:returns: :class:`Keyword <distro_tracker.core.models.Keyword>` 

instances for the given keyword names. 

""" 

return Keyword.objects.filter(name__in=keywords) 

 

def modify_default_keywords(self, email, keywords): 

try: 

user_email = UserEmail.objects.get(user=self.user, email=email) 

except (UserEmail.DoesNotExist): 

return HttpResponseForbidden() 

 

email_settings, _ = \ 

EmailSettings.objects.get_or_create(user_email=user_email) 

email_settings.default_keywords.set(self.get_keywords(keywords)) 

 

return self.render_response() 

 

def modify_subscription_keywords(self, email, package, keywords): 

try: 

user_email = UserEmail.objects.get(user=self.user, email=email) 

except (UserEmail.DoesNotExist): 

return HttpResponseForbidden() 

 

email_settings, _ = \ 

EmailSettings.objects.get_or_create(user_email=user_email) 

subscription = get_object_or_404( 

Subscription, email_settings__user_email=user_email, 

package__name=package) 

 

subscription.keywords.clear() 

for keyword in self.get_keywords(keywords): 

subscription.keywords.add(keyword) 

 

return self.render_response() 

 

def render_response(self): 

374 ↛ 379line 374 didn't jump to line 379, because the condition on line 374 was never false if self.request.is_ajax(): 

return render_to_json_response({ 

'status': 'ok', 

}) 

else: 

_next = self.request.POST.get('next', None) 

return safe_redirect( 

_next, 

resolve_url('dtracker-index'), 

) 

 

def post(self, request): 

386 ↛ 387line 386 didn't jump to line 387, because the condition on line 386 was never true if 'email' not in request.POST or 'keyword[]' not in request.POST: 

raise Http404 

 

self.user = request.user 

self.request = request 

email = request.POST['email'] 

keywords = request.POST.getlist('keyword[]') 

 

if 'package' in request.POST: 

return self.modify_subscription_keywords( 

email, request.POST['package'], keywords) 

else: 

return self.modify_default_keywords(email, keywords) 

 

def get(self, request): 

if 'email' not in request.GET: 

raise Http404 

email = request.GET['email'] 

 

try: 

user_email = request.user.emails.get(email=email) 

except UserEmail.DoesNotExist: 

return HttpResponseForbidden() 

 

if 'package' in request.GET: 

package = request.GET['package'] 

subscription = get_object_or_404( 

Subscription, email_settings__user_email=user_email, 

package__name=package) 

context = { 

'post': { 

'email': email, 

'package': package, 

}, 

'package': package, 

'user_keywords': subscription.keywords.all(), 

} 

else: 

context = { 

'post': { 

'email': email, 

}, 

'user_keywords': 

user_email.emailsettings.default_keywords.all(), 

} 

 

context.update({ 

'keywords': Keyword.objects.order_by('name').all(), 

'email': email, 

}) 

 

return render(request, 'accounts/modify-subscription.html', context)