diff --git a/app/models/__init__.py b/app/models/__init__.py index 9849a04..fd484d2 100644 --- a/app/models/__init__.py +++ b/app/models/__init__.py @@ -3,3 +3,4 @@ from app.models.thread import Thread from app.models.forum import Forum from app.models.topic import Topic from app.models.notification import Notification +from app.models.program import Program diff --git a/app/models/program.py b/app/models/program.py new file mode 100644 index 0000000..b16a484 --- /dev/null +++ b/app/models/program.py @@ -0,0 +1,43 @@ +from app import db +from app.models.post import Post + +class Program(Post): + __tablename__ = 'program' + __mapper_args__ = {'polymorphic_identity': __tablename__} + + # ID of underlying Post object + id = db.Column(db.Integer, db.ForeignKey('post.id'), primary_key=True) + + # Program name + title = db.Column(db.Unicode(128)) + + # TODO: Category (games/utilities/lessons) + # TODO: Tags + # TODO: Compatible calculator models + + thread_id = db.Column(db.Integer,db.ForeignKey('thread.id'),nullable=False) + thread = db.relationship('Thread', foreign_keys=thread_id) + + # TODO: Number of views, statistics, attached files, etc + + def __init__(self, author, title, thread): + """ + Create a Program. + + Arguments: + author -- post author (User, though only Members can post) + title -- program title (unicode string) + thread -- discussion thread attached to the topic + """ + + Post.__init__(self, author) + self.title = title + self.thread = thread + + @staticmethod + def from_topic(topic): + p = Program(topic.author, topic.title, topic.thread) + topic.promotion = p + + def __repr__(self): + return f'' diff --git a/app/models/topic.py b/app/models/topic.py index 414d7ca..2c656d3 100644 --- a/app/models/topic.py +++ b/app/models/topic.py @@ -3,13 +3,22 @@ from app.models.post import Post class Topic(Post): __tablename__ = 'topic' - __mapper_args__ = {'polymorphic_identity': __tablename__} - # ID of the underlying [Post] object id = db.Column(db.Integer, db.ForeignKey('post.id'), primary_key=True) + __mapper_args__ = { + 'polymorphic_identity': __tablename__, + 'primary_key': id, + 'inherit_condition': id == Post.id + } + + # Post that the topic was promoted into. If this is not None, then the + # topic was published into a project and a redirection should be emitted + promotion_id = db.Column(db.Integer,db.ForeignKey('post.id'),nullable=True) + promotion = db.relationship('Post', foreign_keys=promotion_id) + # Topic title - title = db.Column(db.Unicode(32)) + title = db.Column(db.Unicode(128)) # Parent forum forum_id = db.Column(db.Integer, db.ForeignKey('forum.id'), nullable=False) diff --git a/app/routes/__init__.py b/app/routes/__init__.py index 9bc4576..de98e92 100644 --- a/app/routes/__init__.py +++ b/app/routes/__init__.py @@ -4,3 +4,4 @@ from app.routes import index, search, users, tools from app.routes.account import login, account, notification from app.routes.admin import index, groups, account, trophies, forums from app.routes.forum import index, topic +from app.routes.programs import index diff --git a/app/routes/programs/index.py b/app/routes/programs/index.py new file mode 100644 index 0000000..7d267c1 --- /dev/null +++ b/app/routes/programs/index.py @@ -0,0 +1,8 @@ +from app import app, db +from app.models.program import Program +from app.utils.render import render + +@app.route('/programmes') +def program_index(): + programs = Program.query.all() + return render('/programs/index.html') diff --git a/app/templates/programs/index.html b/app/templates/programs/index.html new file mode 100644 index 0000000..f141ddf --- /dev/null +++ b/app/templates/programs/index.html @@ -0,0 +1,21 @@ +{% extends "base/base.html" %} + +{% block title %} +

Programmes de Planète Casio

+{% endblock %} + +{% block content %} +
+

Tous les programmes

+ + + + {% for p in programs %} + + + + + {% endfor %} +
IDNomAuteurPublié le
{{ p.id }}{{ p.name }}{{ p.author.name }}{{ p.date_created }}
+
+{% endblock %} diff --git a/migrations/versions/c5561fa6af4e_add_a_base_model_for_programs.py b/migrations/versions/c5561fa6af4e_add_a_base_model_for_programs.py new file mode 100644 index 0000000..632f980 --- /dev/null +++ b/migrations/versions/c5561fa6af4e_add_a_base_model_for_programs.py @@ -0,0 +1,35 @@ +"""add a base model for programs + +Revision ID: c5561fa6af4e +Revises: c7779a558510 +Create Date: 2020-08-01 14:52:52.878440 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'c5561fa6af4e' +down_revision = 'c7779a558510' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('program', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('title', sa.Unicode(length=128), nullable=True), + sa.Column('thread_id', sa.Integer(), nullable=False), + sa.ForeignKeyConstraint(['id'], ['post.id'], ), + sa.ForeignKeyConstraint(['thread_id'], ['thread.id'], ), + sa.PrimaryKeyConstraint('id') + ) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('program') + # ### end Alembic commands ### diff --git a/migrations/versions/c7779a558510_add_promotion_information_to_topics.py b/migrations/versions/c7779a558510_add_promotion_information_to_topics.py new file mode 100644 index 0000000..40d8783 --- /dev/null +++ b/migrations/versions/c7779a558510_add_promotion_information_to_topics.py @@ -0,0 +1,30 @@ +"""add promotion information to topics + +Revision ID: c7779a558510 +Revises: 001d2eaf0413 +Create Date: 2020-08-01 11:27:23.298821 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'c7779a558510' +down_revision = '001d2eaf0413' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('topic', sa.Column('promotion_id', sa.Integer(), nullable=True)) + op.create_foreign_key(None, 'topic', 'post', ['promotion_id'], ['id']) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_constraint(None, 'topic', type_='foreignkey') + op.drop_column('topic', 'promotion_id') + # ### end Alembic commands ###