Пример #1
0
def index():
    formulaire = FormulairePublication()
    if formulaire.validate_on_submit():
        publication = Publication(corps=formulaire.publication.data,
                                  auteur=current_user)
        db.session.add(publication)
        db.session.commit()
        id = publication.id
        socketio.emit('nouvelle publication', {
            'id': id,
            'corps': publication.corps
        },
                      namespace='/chat')
        flash('Votre publication est en ligne!')
        return redirect(url_for('index'))
    utilisateur = current_user

    page = request.args.get('page', 1, type=int)
    publications = current_user.liste_publications_dont_je_suis_partisan(
    ).paginate(page, app.config['PUBLICATION_PAR_PAGE'], False)

    suivant = url_for('index', page=publications.next_num) \
        if publications.has_next else None
    precedant = url_for('index', page=publications.prev_num) \
        if publications.has_prev else None
    return render_template('index.html',
                           titre='Accueil',
                           utilisateur=utilisateur,
                           publications=publications.items,
                           formulaire=formulaire,
                           suivant=suivant,
                           precedant=precedant)
Пример #2
0
def index():
    if current_user is None or not current_user.is_authenticated:
        return redirect(url_for('login'))
    form = PublicationForm()
    if request.method == 'POST' and form.validate_on_submit():
        pub = Publication(body=form.publication.data, auteur=current_user)
        db.session.add(pub)
        db.session.commit()
        socketio.emit('nouvelle_publication', {'id': pub.id},
                      namespace='/chat')
        flash('Publication envoyée!')

    pubs = current_user.getPartisansPubs().paginate(
        request.args.get('page', 1, type=int),
        app.config['PUBLICATION_PAR_PAGE'], False)

    (next, prev) = getPages(pubs, 'index')

    return render_template('index.html',
                           title="Accueil",
                           user=current_user,
                           pubs=pubs.items,
                           form=form,
                           next=next,
                           prev=prev)
Пример #3
0
    def test_subbedPubs(self):
        usertb = []
        for i in range(4):
            usertb.append(
                Utilisateur(nom='u' + str(i),
                            email='u' + str(i) + '@info.cgg'))

        db.session.add_all(usertb)

        now = datetime.utcnow()
        pubstb = []
        for u in usertb:
            pubstb.append(
                Publication(body='Publication de ' + u.nom,
                            auteur=u,
                            creation=now + timedelta(seconds=1)))

        db.session.add_all(pubstb)

        usertb[0].userSub(usertb[1])
        usertb[0].userSub(usertb[3])
        usertb[1].userSub(usertb[2])
        usertb[2].userSub(usertb[3])

        db.session.commit()

        substb = []
        for u in usertb:
            substb.append(u.getPartisansPubs().all())

        self.assertEqual(substb[0], [pubstb[0], pubstb[1], pubstb[3]])
        self.assertEqual(substb[1], [pubstb[1], pubstb[2]])

        self.assertEqual(substb[2], [pubstb[2], pubstb[3]])
        self.assertEqual(substb[3], [pubstb[3]])
Пример #4
0
def get_publications():
    page = request.args.get('page', 1, type=int)
    par_page = min(request.args.get('par_page', 10, type=int), 100)
    data = Publication.to_collection_dict(Publication.query, page, par_page,
                                          'api.get_publications',
                                          "publications")

    return jsonify(data)
Пример #5
0
def creer_publication():
    jeton = request.json["jeton"]
    corps = request.json["corps"]

    print(jeton)
    print(corps)
    print("publier un nouveau message: " + corps)
    u = Utilisateur.query.filter_by(jeton=jeton).first_or_404()
    publication = Publication(corps=corps, auteur=u)
    db.session.add(publication)
    db.session.commit()

    id = publication.utilisateur_id
    socketio.emit('nouvelle_publication', {
        'id': id,
        'corps': publication.corps
    },
                  namespace='/chat')

    return jsonify(publication.to_dict())
Пример #6
0
    def test_publications_suivies(self):
        # create four users
        u1 = Utilisateur(nom='patate', courriel='*****@*****.**')
        u2 = Utilisateur(nom='tomate', courriel='*****@*****.**')
        u3 = Utilisateur(nom='salade', courriel='*****@*****.**')
        u4 = Utilisateur(nom='radis', courriel='*****@*****.**')
        db.session.add_all([u1, u2, u3, u4])

        # create four posts
        maintenant = datetime.utcnow()
        p1 = Publication(corps="publication from patate",
                         auteur=u1,
                         horodatage=maintenant + timedelta(seconds=1))
        p2 = Publication(corps="publication from tomate",
                         auteur=u2,
                         horodatage=maintenant + timedelta(seconds=4))
        p3 = Publication(corps="publication from salade",
                         auteur=u3,
                         horodatage=maintenant + timedelta(seconds=3))
        p4 = Publication(corps="publication from radis",
                         auteur=u4,
                         horodatage=maintenant + timedelta(seconds=2))
        db.session.add_all([p1, p2, p3, p4])
        db.session.commit()

        # setup the followers
        u1.devenir_partisan(u2)  # john follows susan
        u1.devenir_partisan(u4)  # john follows david
        u2.devenir_partisan(u3)  # susan follows mary
        u3.devenir_partisan(u4)  # mary follows david
        db.session.commit()

        # check the followed posts of each user
        f1 = u1.liste_publications_dont_je_suis_partisan().all()
        f2 = u2.liste_publications_dont_je_suis_partisan().all()
        f3 = u3.liste_publications_dont_je_suis_partisan().all()
        f4 = u4.liste_publications_dont_je_suis_partisan().all()
        self.assertEqual(f1, [p2, p4, p1])
        self.assertEqual(f2, [p2, p3])
        self.assertEqual(f3, [p3, p4])
        self.assertEqual(f4, [p4])
Пример #7
0
def get_pubs(id):
    return jsonify(Publication.from_id(id).to_dict())
Пример #8
0
def get_publications():
    page = request.args.get('page', 1, type=int)
    perp = min(request.args.get('par_page', 10, type=int), 100)
    return jsonify(
        Publication.to_collection_dict(Publication.query, page, perp,
                                       'api.get_publications'))
Пример #9
0
def cree_publication(id):
    current_user = token_auth.current_user()
    publication = Publication(corps=id.replace("\"", ""), auteur=current_user)
    db.session.add(publication)
    db.session.commit()
    return ("OK")