# Coverage for modules/box__default/auth/view.py : 31%

## 113 statements

Hot-keys on this page

r m x p toggle line displays

j k next/prev highlighted chunk

0 (zero) top of page

1 (one) first highlighted chunk

1 import datetime

2 import json

3 import os

4

5 from flask import Blueprint

6 from flask import current_app

7 from flask import flash

8 from flask import redirect

9 from flask import render_template

10 from flask import request

11 from flask import url_for

12 from flask_login import current_user

13 from flask_login import login_required

14 from flask_login import login_user

15 from flask_login import logout_user

16 from sqlalchemy import func

17

18 from .forms import LoginForm

19 from .forms import RegistrationForm

20 from .models import User

21 from shopyo.api.email import send_async_email

22 from shopyo.api.html import notify_danger

23 from shopyo.api.html import notify_success

24 from shopyo.api.html import notify_warning

25 from shopyo.api.module import ModuleHelp

26 from shopyo.api.security import get_safe_redirect

27

28

29 mhelp = ModuleHelp(__file__, __name__)

30 globals()[mhelp.blueprint_str] = mhelp.blueprint

31 module_blueprint = globals()[mhelp.blueprint_str]

32

33

34@module_blueprint.route("/register", methods=["GET", "POST"])

35 def register():

36

37 context = {}

38 reg_form = RegistrationForm()

39

40 if reg_form.validate_on_submit():

41 email = reg_form.email.data

42 password = reg_form.password.data

43 user = User.create(email=email, password=password)

44 login_user(user)

45

46 is_disabled = False

47

48 if "EMAIL_CONFIRMATION_DISABLED" in current_app.config:

49 is_disabled = current_app.config["EMAIL_CONFIRMATION_DISABLED"]

50

51 if is_disabled is True:

52 user.is_email_confirmed = True

53 user.email_confirm_date = datetime.datetime.now()

54 user.update()

55 else:

56 token = user.generate_confirmation_token()

57 template = "auth/emails/activate_user"

58 subject = "Please confirm your email"

59 context.update({"token": token, "user": user})

60 send_async_email(email, subject, template, **context)

61 flash(notify_success("A confirmation email has been sent via email."))

62

63 return redirect(url_for("dashboard.index"))

64

65 context["form"] = reg_form

66 return render_template("auth/register.html", **context)

67

68

69@module_blueprint.route("/confirm/<token>")

70@login_required

71 def confirm(token):

72

73 if current_user.is_email_confirmed:

74 flash(notify_warning("Account already confirmed."))

75 return redirect(url_for("dashboard.index"))

76

77 if current_user.confirm_token(token):

78 flash(notify_success("You have confirmed your account. Thanks!"))

79 return redirect(url_for("dashboard.index"))

80

81 flash(notify_warning("The confirmation link is invalid/expired."))

82 return redirect(url_for("auth.unconfirmed"))

83

84

85@module_blueprint.route("/resend")

86@login_required

87 def resend():

88

89 if current_user.is_email_confirmed:

90 return redirect(url_for("dashboard.index"))

91

92 token = current_user.generate_confirmation_token()

93 template = "auth/emails/activate_user"

94 subject = "Please confirm your email"

95 context = {"token": token, "user": current_user}

96 send_async_email(current_user.email, subject, template, **context)

97 flash(notify_success("A new confirmation email has been sent."))

98 return redirect(url_for("auth.unconfirmed"))

99

100

101@module_blueprint.route("/unconfirmed")

102@login_required

103 def unconfirmed():

104 if current_user.is_email_confirmed:

105 return redirect(url_for("dashboard.index"))

106 flash(notify_warning("Please confirm your account!"))

107 return render_template("auth/unconfirmed.html")

108

109

110@module_blueprint.route("/login", methods=["GET", "POST"])

111 def login():

112 context = {}

113 login_form = LoginForm()

114 context["form"] = login_form

115 if login_form.validate_on_submit():

116 email = login_form.email.data

117 password = login_form.password.data

118 user = User.query.filter(func.lower(User.email) == func.lower(email)).first()

119 if user is None or not user.check_password(password):

120 flash(notify_danger("please check your user id and password"))

121 return redirect(url_for("auth.login"))

122 login_user(user)

123 if "next" not in request.form:

124 next_url = url_for("dashboard.index")

125

126 else:

127 if request.form["next"] == "":

128 next_url = url_for("dashboard.index")

129 else:

130 next_url = get_safe_redirect(request.form["next"])

131 return redirect(next_url)

132 return render_template("auth/login.html", **context)

133

134

135@module_blueprint.route("/logout", methods=["GET"])

136@login_required

137 def logout():

138 logout_user()

139 flash(notify_success("Successfully logged out"))

140

141 if "next" not in request.args:

142 next_url = url_for("dashboard.index")

143 else:

144 if request.args.get("next") == "":

145 next_url = url_for("dashboard.index")

146 else:

147 next_url = get_safe_redirect(request.args.get("next"))

148 return redirect(next_url)