Add question ownership and sharing

Questions now have a created_by field linking to the user who created them.
Users only see questions they own or that have been shared with them.
Includes share dialog, user search, bulk sharing, and export/import
respects ownership.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-03 09:43:04 -04:00
parent 02fcbad9ba
commit 69992f1be9
15 changed files with 836 additions and 70 deletions

View File

@@ -80,9 +80,22 @@ class Question(db.Model):
end_time = db.Column(db.Integer, nullable=True) # End time in seconds
created_at = db.Column(db.DateTime, default=datetime.utcnow)
created_by = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=True)
# Relationships
creator = db.relationship('User', backref='questions')
game_questions = db.relationship('GameQuestion', back_populates='question', cascade='all, delete-orphan')
shares = db.relationship('QuestionShare', backref='question', cascade='all, delete-orphan')
def is_visible_to(self, user):
"""Check if this question is visible to the given user"""
if self.created_by == user.id:
return True
return any(s.shared_with_user_id == user.id for s in self.shares)
def is_owned_by(self, user):
"""Check if this question is owned by the given user"""
return self.created_by == user.id
def to_dict(self, include_answer=False):
"""Convert question to dictionary"""
@@ -96,13 +109,43 @@ class Question(db.Model):
'start_time': self.start_time,
'end_time': self.end_time,
'category': self.category,
'created_at': self.created_at.isoformat() if self.created_at else None
'created_at': self.created_at.isoformat() if self.created_at else None,
'created_by': self.created_by,
'creator_name': (self.creator.name or self.creator.preferred_username) if self.creator else None,
}
if include_answer:
data['answer'] = self.answer
return data
class QuestionShare(db.Model):
"""Junction table for sharing questions between users"""
__tablename__ = 'question_shares'
id = db.Column(db.Integer, primary_key=True)
question_id = db.Column(db.Integer, db.ForeignKey('questions.id'), nullable=False)
shared_with_user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
shared_by_user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
created_at = db.Column(db.DateTime, default=datetime.utcnow)
shared_with_user = db.relationship('User', foreign_keys=[shared_with_user_id], backref='shared_questions')
shared_by_user = db.relationship('User', foreign_keys=[shared_by_user_id])
__table_args__ = (
db.UniqueConstraint('question_id', 'shared_with_user_id', name='unique_question_share'),
)
def to_dict(self):
return {
'id': self.id,
'question_id': self.question_id,
'shared_with_user_id': self.shared_with_user_id,
'shared_with_user_name': (self.shared_with_user.name or self.shared_with_user.preferred_username) if self.shared_with_user else None,
'shared_by_user_id': self.shared_by_user_id,
'created_at': self.created_at.isoformat() if self.created_at else None,
}
class Game(db.Model):
"""Game model representing a trivia game session"""
__tablename__ = 'games'