Make sure user cannot create more than 50 directories

This commit is contained in:
Son NK 2020-04-12 20:14:49 +02:00
parent b4f28a5156
commit e44860329b
3 changed files with 15 additions and 2 deletions

View file

@ -57,6 +57,9 @@ except Exception:
print("MAX_NB_EMAIL_FREE_PLAN is not set, use 5 as default value")
MAX_NB_EMAIL_FREE_PLAN = 5
# maximum number of directory a premium user can create
MAX_NB_DIRECTORY = 50
# allow to override postfix server locally
POSTFIX_SERVER = os.environ.get("POSTFIX_SERVER", "240.0.0.1")

View file

@ -3,7 +3,7 @@ from flask_login import login_required, current_user
from flask_wtf import FlaskForm
from wtforms import StringField, validators
from app.config import EMAIL_DOMAIN, ALIAS_DOMAINS
from app.config import EMAIL_DOMAIN, ALIAS_DOMAINS, MAX_NB_DIRECTORY
from app.dashboard.base import dashboard_bp
from app.extensions import db
from app.models import Directory
@ -46,6 +46,13 @@ def directory():
flash("Only premium plan can add directory", "warning")
return redirect(url_for("dashboard.directory"))
if current_user.nb_directory() >= MAX_NB_DIRECTORY:
flash(
f"You cannot have more than {MAX_NB_DIRECTORY} directories",
"warning",
)
return redirect(url_for("dashboard.directory"))
if new_dir_form.validate():
new_dir_name = new_dir_form.name.data.lower()

View file

@ -328,7 +328,7 @@ class User(db.Model, ModelMixin, UserMixin):
"""
sub = Subscription.get_by(user_id=self.id)
# TODO: sub is active only if sub.next_bill_date > now
# due to a bug on next_bill_date, wait until next month (April 8)
# due to a bug on next_bill_date, wait until next month (May 8)
# when all next_bill_date are correctly updated to add this check
if sub and sub.cancelled:
@ -353,6 +353,9 @@ class User(db.Model, ModelMixin, UserMixin):
return mailboxes
def nb_directory(self):
return Directory.query.filter_by(user_id=self.id).count()
def __repr__(self):
return f"<User {self.id} {self.name} {self.email}>"