From 1c2635045bd0d4213ed5cf1e802a8949e682a592 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Mon, 9 Jul 2018 13:03:24 +0200 Subject: [PATCH] Make User.profile a single object instead of a list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Because it’s a one-to-one relationship anyway, what’s the use of lists here? --- calsocial/__init__.py | 8 +++----- calsocial/models.py | 2 +- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/calsocial/__init__.py b/calsocial/__init__.py index 9be5906..631f5bd 100644 --- a/calsocial/__init__.py +++ b/calsocial/__init__.py @@ -256,12 +256,10 @@ class CalendarSocialApp(Flask): from .models import Profile, User - try: - profile = Profile.query.join(User).filter(User.username == username.lower()).one() - except NoResultFound: + profile = current_user.profile + + if profile is None: abort(404) - except MultipleResultsFound: - abort(500) return render_template('profile-details.html', profile=profile) diff --git a/calsocial/models.py b/calsocial/models.py index ba07ded..2f3feba 100644 --- a/calsocial/models.py +++ b/calsocial/models.py @@ -167,7 +167,7 @@ class Profile(db.Model): # pylint: disable=too-few-public-methods #: The ID of the local user this profile belongs to user_id = db.Column(db.Integer(), db.ForeignKey('users.id'), nullable=True, unique=True) - user = db.relationship(User, backref=db.backref('profile', lazy='dynamic')) + user = db.relationship(User, backref=db.backref('profile', uselist=False)) #: The username this profile belongs to. If ``None``, `user_id` must be set username = db.Column(db.String(length=50), nullable=True)