def newTopic(subject_id):
    if login_session.get('user_id'):
        files = os.listdir('./static/articles')

        subject = session.query(Subject).filter_by(id=subject_id).one()
        if request.method == 'POST':
            newTopic = Topic(name=request.form['name'],
                             description=request.form['description'],
                             subject_id=subject_id,
                             user_id=login_session['user_id'],
                             article=request.form['article'])
            session.add(newTopic)
            session.commit()
            flash("New topic created!")
            return(redirect(url_for('topics', subject_id=subject_id)))
        else:
            return(render_template('newtopic.html', subject=subject,
                                   files=files, login_session=login_session))
    else:
        subjects = session.query(Subject)
        subject = session.query(Subject).filter_by(id=subject_id).one()
        topics = session.query(Topic).filter_by(subject_id=subject_id)
        topic = Topic(name='Topics')
        dir = 'common_files'
        article = 'Not Authorized.md'
        with open('./static/{dir}/{article}'
                  .format(dir=dir, article=article), 'r')as file:
            content = file.read()
            content = Markup(markdown.markdown(content))
        return(render_template('notauthorized.html', subjects=subjects,
                               topics=topics, subject=subject, topic=topic,
                               content=content, login_session=login_session))
def deleteTopic(subject_id, topic_id):
    subject_owner_id = (session.query(Subject)
                        .filter_by(id=subject_id).one().user_id)
    topic_owner_id = session.query(Topic).filter_by(id=topic_id).one().user_id
    if (login_session.get('user_id') == subject_owner_id or
       login_session.get('user_id') == topic_owner_id):
        deleteTopic = session.query(Topic).filter_by(subject_id=subject_id,
                                                     id=topic_id).one()
        if request.method == 'POST':
            session.delete(deleteTopic)
            session.commit()
            flash("Topic deleted!")
            return(redirect(url_for('topics', subject_id=subject_id)))
        else:
            subject = session.query(Subject).filter_by(id=subject_id).one()
            return(render_template('deletetopic.html', subject=subject,
                                   topic=deleteTopic,
                                   login_session=login_session))
    else:
        subjects = session.query(Subject)
        subject = session.query(Subject).filter_by(id=subject_id).one()
        topics = session.query(Topic).filter_by(subject_id=subject_id)
        topic = Topic(name='Topics')
        dir = 'common_files'
        article = 'Not Authorized.md'
        with open('./static/{dir}/{article}'.format(dir=dir, article=article),
                  'r') as file:
            content = file.read()
            content = Markup(markdown.markdown(content))
        return(render_template('notauthorized.html', subjects=subjects,
                               topics=topics, subject=subject, topic=topic,
                               content=content, login_session=login_session))
def editSubject(subject_id):
    owner_id = session.query(Subject).filter_by(id=subject_id).one().user_id
    if login_session.get('user_id') == owner_id:
        editSubject = session.query(Subject).filter_by(id=subject_id).one()
        if request.method == 'POST':
            editSubject.name = request.form['name']
            session.add(editSubject)
            session.commit()
            flash("Subject edited")
            return(redirect(url_for('subjects')))
        else:
            return(render_template('editsubject.html', subject=editSubject,
                                   login_session=login_session))
    else:
        subjects = session.query(Subject)
        subject = session.query(Subject).filter_by(id=subject_id).one()
        topics = session.query(Topic).filter_by(subject_id=subject_id)
        topic = Topic(name='Topics')
        dir = 'common_files'
        article = 'Not Authorized.md'
        with open('./static/{dir}/{article}'
                  .format(dir=dir, article=article), 'r')as file:
            content = file.read()
            content = Markup(markdown.markdown(content))
        return(render_template('notauthorized.html', subjects=subjects,
                               topics=topics, subject=subject, topic=topic,
                               content=content, login_session=login_session))
def newSubject():
    if login_session.get('user_id'):
        if request.method == 'POST':
            newSubject = Subject(name=request.form['name'],
                                 user_id=login_session['user_id'])

            session.add(newSubject)
            session.commit()
            flash("New subject created!")
            return(redirect(url_for('subjects')))
        else:
            subjects = session.query(Subject)
            return(render_template('newsubject.html',
                                   login_session=login_session,
                                   subjects=subjects))
    else:
        subjects = session.query(Subject)
        subject = Subject(name='Subjects')
        topics = False
        topic = Topic()
        dir = 'common_files'
        article = 'Not Authorized.md'
        with open('./static/{dir}/{article}'
                  .format(dir=dir, article=article), 'r') as file:
            content = file.read()
            content = Markup(markdown.markdown(content))
        return(render_template('notauthorized.html', subjects=subjects,
                               topics=topics, subject=subject, topic=topic,
                               content=content, login_session=login_session))
Exemplo n.º 5
0
def add_model_topics(model, topics_def):
    for topic_number, words in topics_def:
        try:
            new_topic = Topic(number=topic_number + 1, model_id=model.id) # + 1 for harmonizing with pyLDAviz
            db_session.add(new_topic)
            db_session.commit()
            print("Topic {} added successfully for model {}.".format(str(topic_number), model.name))
            add_topic_words(model, new_topic, words)
        except (exc.IntegrityError, AssertionError,
                AttributeError, NameError) as e:
            db_session.rollback()
            print("Topic error: {}".format(e))
Exemplo n.º 6
0
def NewStory(category_id):
    chosen_category = session.query(Category).filter_by(id=category_id).one()
    if 'username' not in login_session:
        return redirect('/login')
    if request.method == 'POST':
        user_id = getUserID(login_session['email'])
        newTopic = Topic(title=request.form['title'],
                         summary=request.form['summary'],
                         content=request.form['content'],
                         category_id=category_id,
                         user_id=user_id)
        session.add(newTopic)
        flash('New topic [ %s ] successfully created!' % newTopic.title)
        session.commit()
        return redirect(url_for('Categories', category_id=category_id))
    else:
        return render_template('newShowTopics.html')
def editTopic(subject_id, topic_id):
    subject_owner_id = (session.query(Subject)
                        .filter_by(id=subject_id).one().user_id)
    topic_owner_id = session.query(Topic).filter_by(id=topic_id).one().user_id
    if (login_session.get('user_id') == topic_owner_id or
       login_session.get('user_id') == subject_owner_id):
            files = os.listdir('./static/articles')
            editTopic = session.query(Topic).filter_by(subject_id=subject_id,
                                                       id=topic_id).one()
            if request.method == 'POST':
                if request.form['name']:
                    editTopic.name = request.form['name']
                    editTopic.description = request.form['description']
                    editTopic.article = request.form['article']
                    session.add(editTopic)
                    session.commit()
                    flash("Subject topic edited!")
                return(redirect(url_for('topics', subject_id=subject_id)))
            else:
                subject = session.query(Subject).filter_by(id=subject_id).one()
                return(render_template('edittopic.html', subject=subject,
                                       topic=editTopic,
                                       login_session=login_session,
                                       files=files))
    else:
        subjects = session.query(Subject)
        subject = session.query(Subject).filter_by(id=subject_id).one()
        topics = session.query(Topic).filter_by(subject_id=subject_id)
        topic = Topic(name='Topics')
        dir = 'common_files'
        article = 'Not Authorized.md'
        with open('./static/{dir}/{article}'
                  .format(dir=dir, article=article), 'r') as file:
            content = file.read()
            content = Markup(markdown.markdown(content))
        return(render_template('notauthorized.html', subjects=subjects,
                               topics=topics, subject=subject, topic=topic,
                               content=content, login_session=login_session))
# and represents a "staging zone" for all the objects loaded into the
# database session object. Any change made against the objects in the
# session won't be persisted into the database until you call
# session.commit(). If you're not happy about the changes, you can
# revert all of them back to the last commit by calling
# session.rollback()
session = DBSession()

#Subject
subject1 = Subject(user_id=1, name="English")
session.add(subject1)
session.commit()

topic1 = Topic(user_id=1,
               name="Question Words",
               description="A list of verbs that are \
irregular in the Past Tense",
               subject=subject1,
               article='')

session.add(topic1)
session.commit()

topic2 = Topic(user_id=1,
               name="Past Tense Irregular Verbs",
               description="Who, When, Why,\
 What, Which, How",
               subject=subject1,
               article='')

session.add(topic2)
session.commit()