Пример #1
0
def test_channels_available_for_user():
    u = User.query.get(1)
    assert len(channels_available_for_user(u.id)) == 1
    user = User(id=3,
                name="test",
                first_name="utilisateur3",
                email="*****@*****.**")
    db.session.add(user)
    assert len(channels_available_for_user(user.id)) == 0
Пример #2
0
def new_post():
    user_id = session.get("user_id", "") if session.get("logged_in",
                                                        False) else -1
    list_of_channels = channels_available_for_user(user_id)
    extraForms = {}
    for elem in list_of_channels:
        m = elem.module
        plugin = import_module(m)
        extraForms[elem.name] = plugin.get_template_new()
        clas = get_instance_from_module_path(m)
        unaivalable_fields = ','.join(clas.FIELDS_UNAVAILABLE)
        setattr(elem, "unavailablefields", unaivalable_fields)

    if request.method == "GET":

        post_form_validations = dict()

        print(post_form_validations)
        return render_template('new.html',
                               extra_forms=extraForms,
                               l_chan=list_of_channels,
                               post_form_validations=post_form_validations)
    else:
        create_a_post(request.form)
        return redirect(url_for('index'))
Пример #3
0
def search():
    user_id = session.get('user_id', '') if session.get('logged_in',
                                                        False) else -1
    l_chan = channels_available_for_user(user_id)
    if request.method == 'GET':
        return render_template('search.html', l_chan=l_chan, publishing=[])
    else:
        user = User.query.get(user_id) if session.get("logged_in",
                                                      False) else None
        print(user)
        pattern = request.form.get('search_word')
        chan = request.form.getlist('search_chan')
        status = request.form.getlist('post_status')
        loc = request.form.getlist('search_loc')
        date_from = request.form.get('date_from')
        date_until = request.form.get('date_until')
        search_type = request.form.get('search_type') == 'keyword'
        search_table = request.form.get('search_table')
        print(search_table)
        filter_parameter = make_filter_parameter(user_id, pattern, chan,
                                                 status, loc, date_from,
                                                 date_until, search_type,
                                                 search_table)
        search_result = query_maker(filter_parameter)
        return render_template(
            'search.html',
            l_chan=l_chan,
            publishing=search_result,
            post=True,
            moderate_channel=[
                x.id for x in get_moderate_channels_for_user(user)
            ],
            search_table=search_table,
            user_id=user.id)
Пример #4
0
def new_post():
    user_id = session.get("user_id", "") if session.get("logged_in",
                                                        False) else -1
    list_of_channels = channels_available_for_user(user_id)

    ictv_chans = []

    for elem in list_of_channels:
        m = elem.module

        clas = get_instance_from_module_path(m)
        unavailable_fields = ','.join(clas.FIELDS_UNAVAILABLE)
        setattr(elem, "unavailablefields", unavailable_fields)

        if 'ictv_data_form' in unavailable_fields:
            ictv_chans.append(elem)

    if request.method == "GET":
        ictv_data = None
        if len(ictv_chans) != 0:
            from plugins.ictv import process_ictv_channels
            ictv_data = process_ictv_channels(ictv_chans)

        return render_template('new.html',
                               l_chan=list_of_channels,
                               ictv_data=ictv_data,
                               new=True)
    else:
        # Save as draft
        # FIXME Maybe refactor the code so that this part is not too confusing?
        create_a_post(request.form)
        flash("The post was successfully saved as draft", category='success')
        return redirect(url_for('index'))
Пример #5
0
def stats_channel():
    user_id = session.get("user_id", "") if session.get("logged_in", False) else -1
    list_of_channels = channels_available_for_user(user_id)
    label = []
    count = []
    for c in list_of_channels:
        label.append(c.name)
        count.append(c.count)
    return {'label' : label, 'count' : count}
def test_channels_available_for_user():
    u = User.query.get(63)
    #assert len(channels_available_for_user(u.id))==1
    #TEAM6: MODIFICATION FOR PDF CHANNELS AVAILABLE FOR EVERY USER
    #u = User.query.get(1)
    pdf_channels = db.session.query(Channel).filter(
        Channel.module == "superform.plugins.pdf")
    pdf_channels_number = 0
    if (pdf_channels is not None):
        for chan in pdf_channels:
            pdf_channels_number += 1

    assert len(channels_available_for_user(u.id)) == 1 + pdf_channels_number
    user = User(id=3,
                name="test",
                first_name="utilisateur3",
                email="*****@*****.**")
    db.session.add(user)
    assert len(channels_available_for_user(user.id)) == 0 + pdf_channels_number
Пример #7
0
def records():
    """
    This methods is called for the creation of the Records page
    """
    # FIXME Essayez de suivre le pattern PRG (post-redirect-get) pour éviter des misbehaviors
    # FIXME en cas de rechargement de la page
    # Check if there is any publishing to pass as archived
    publishings = db.session.query(Publishing).filter(Publishing.state == 1)\
        .filter(Publishing.date_until <= datetime.datetime.now())
    publishings.update({Publishing.state: 2})
    db.session.commit()

    # Check if a user is an admin
    admin = session.get("admin", False) if session.get("logged_in",
                                                       False) else False

    # Check if a post has been send to delete an archive
    if request.method == "POST" and request.form.get('@action',
                                                     '') == "delete":
        if admin:
            id = request.form.get("id")
            idc = request.form.get("idc")
            pub = db.session.query(Publishing).filter(
                Publishing.post_id == id, Publishing.channel_id == idc)
            pub.delete()
            db.session.commit()
        else:
            # TODO, it seems like we have some cheater here
            pass

    user_id = session.get("user_id", "") if session.get("logged_in",
                                                        False) else -1
    list_of_channels = channels_available_for_user(user_id)

    # Query all the archived publishings
    archives = db.session.query(Publishing)\
        .filter(Publishing.state >= 1)\
        .filter(Publishing.date_until <= str(datetime.datetime.now())[0:19])

    # Take all archives and format the dates entries
    records = []
    for a in archives:
        allowed = False
        for channel in list_of_channels:
            if channel.id == a.channel_id:
                allowed = True
                break

        if allowed:
            date_from = str_converter(a.date_from)
            date_until = str_converter(a.date_until)
            records.append((a, date_from, date_until))

    return render_template('records.html', records=records, admin=admin)
Пример #8
0
def new_post():
    user_id = session.get("user_id", "") if session.get("logged_in", False) else -1
    list_of_channels = channels_available_for_user(user_id)
    for elem in list_of_channels:
        m = elem.module
        clas = get_instance_from_module_path(m)
        unaivalable_fields = ','.join(clas.FIELDS_UNAVAILABLE)
        setattr(elem, "unavailablefields", unaivalable_fields)

    if request.method == "GET":
        return render_template('new.html', l_chan=list_of_channels)
    else:
        create_a_post(request.form)
        return redirect(url_for('index'))
Пример #9
0
def copy_new_post(post_id):
    """
    This method copy the content of a post (defined by his post_id) and open the new post tab with all the informations
    of the original post copied in it (and with the title modified)
    :param post_id: id of the original post to be copied
    :return:
    """
    user_id = session.get("user_id", "") if session.get("logged_in",
                                                        False) else -1
    list_of_channels = channels_available_for_user(user_id)

    ictv_chans = []

    for elem in list_of_channels:
        m = elem.module
        clas = get_instance_from_module_path(m)
        unavailable_fields = '.'.join(clas.FIELDS_UNAVAILABLE)
        setattr(elem, "unavailablefields", unavailable_fields)

        if 'ictv_data_form' in unavailable_fields:
            ictv_chans.append(elem)

    # Query the data from the original post
    original_post = db.session.query(Post).filter(Post.id == post_id).first()
    post = Post(user_id=user_id,
                title="Copy of " + original_post.title,
                description=original_post.description,
                link_url=original_post.link_url,
                image_url=original_post.image_url,
                date_from=original_post.date_from,
                date_until=original_post.date_until)
    if request.method == "GET":
        ictv_data = None
        if len(ictv_chans) != 0:
            from plugins.ictv import process_ictv_channels
            ictv_data = process_ictv_channels(ictv_chans)

        post.date_from = str_converter(post.date_from)
        post.date_until = str_converter(post.date_until)
        return render_template('new.html',
                               l_chan=list_of_channels,
                               ictv_data=ictv_data,
                               post=post,
                               new=True)
    else:
        create_a_post(request.form)
        flash("The post was successfully copied.", category='success')
        return redirect(url_for('index'))
Пример #10
0
def new_post(dial_data=None):
    user_id = session.get("user_id", "") if session.get("logged_in", False) else -1
    list_of_channels = channels_available_for_user(user_id)
    ictv_chans = []

    for elem in list_of_channels:
        m = elem.module
        clas = get_instance_from_module_path(m)
        unavailable_fields = ','.join(clas.FIELDS_UNAVAILABLE)
        setattr(elem, "unavailablefields", unavailable_fields)

        if 'ictv_data_form' in unavailable_fields:
            ictv_chans.append(elem)

    if request.method == "GET" or dial_data is not None:  # when clicking on the new post tab
        ictv_data = None
        if len(ictv_chans) != 0:
            from superform.plugins.ictv import process_ictv_channels
            ictv_data = process_ictv_channels(ictv_chans)

        # set default date
        default_date = {'from': date.today(), 'until': date.today() + timedelta(days=7)}
        post_form_validations = get_post_form_validations()

        # dial
        dial_title = ""
        dial_description = ""
        dial_link = ""
        if dial_data is not None:
            dial_title = dial_data[0]
            dial_description = dial_data[1]
            dial_link = dial_data[2]

        return render_template('new.html', l_chan=list_of_channels, ictv_data=ictv_data,
                               post_form_validations=post_form_validations,
                               date=default_date, dial_title=dial_title, dial_description=dial_description,
                               dial_link=dial_link)
    else:
        # Save as draft
        create_a_post(request.form)
        flash("The post was successfully saved as draft", category='success')
        return redirect(url_for('index'))
Пример #11
0
def new_post():
    user_id = session.get("user_id", "") if session.get("logged_in",
                                                        False) else -1
    module = []
    list_of_channels = channels_available_for_user(user_id)
    for elem in list_of_channels:
        module = {"name": elem.name, "module": elem.module}
        m = elem.module
        clas = get_instance_from_module_path(m)
        unaivalable_fields = ','.join(clas.FIELDS_UNAVAILABLE)
        setattr(elem, "unavailablefields", unaivalable_fields)

    if request.method == "GET":
        #v = db.session.query(Channel).filter(Channel.name == elem.channel_id).first()
        return render_template('new.html',
                               l_chan=list_of_channels,
                               modules=module)
    else:
        create_a_post(request.form)
        return redirect(url_for('archival.update_now'))
Пример #12
0
def new_post():
    user_id = session.get("user_id", "") if session.get("logged_in", False) else -1
    list_of_channels = channels_available_for_user(user_id)
    extraForms = {}
    for elem in list_of_channels:
        m = elem.module
        plugin = import_module(m)
        extraForms[elem.name] = plugin.get_template_new()
        clas = get_instance_from_module_path(m)
        unavailable_fields = ','.join(clas.FIELDS_UNAVAILABLE)
        setattr(elem, "unavailablefields", unavailable_fields)
        setattr(elem, "plugin_name", str(m))

    if request.method == "GET":  # when clicking on the new post tab
        # set default date
        default_date = {'from': date.today(), 'until': date.today() + timedelta(days=7)}

        post_form_validations = get_post_form_validations()

        return render_template('new.html', extra_forms=extraForms, l_chan=list_of_channels, post_form_validations=post_form_validations,
                               date=default_date)
    else:
        create_a_post(request.form)
        return redirect(url_for('index'))
Пример #13
0
def edit_post(post_id):
    """
    This method allow the editing the content of a post (defined by his post_id) and opens the new post tab with all the information
    about the post in it
    :param post_id: id of the post to be edited
    :return:
    """
    user_id = session.get("user_id", "") if session.get("logged_in",
                                                        False) else -1
    list_of_channels = channels_available_for_user(user_id)

    ictv_chans = []

    for elem in list_of_channels:
        m = elem.module
        clas = get_instance_from_module_path(m)
        unavailable_fields = '.'.join(clas.FIELDS_UNAVAILABLE)
        setattr(elem, "unavailablefields", unavailable_fields)

        if 'ictv_data_form' in unavailable_fields:
            ictv_chans.append(elem)

    # Query the data from the post
    post = db.session.query(Post).filter(Post.id == post_id).first()
    # Query the publishing of the post
    list_publishing = db.session.query(Publishing).filter(
        Publishing.post_id == post_id)

    # Get list of channels with publishing not yet publish or validated
    # and list of channels with not yet publishing
    list_chan_id_selected = []
    list_already_pub = []
    for pub in list_publishing:
        list_chan_id_selected.append(pub.channel_id)
        if pub.state == State.PUBLISHED.value or pub.state == State.VALIDATED.value:
            list_already_pub.append(pub.channel_id)
    list_chan_selected = []
    list_chan_not_selected = []
    for chan in list_of_channels:
        if list_chan_id_selected.__contains__(chan.id):
            if not list_already_pub.__contains__(chan.id):
                list_chan_selected.append(chan)
        else:
            list_chan_not_selected.append(chan)

    if request.method == "GET":
        ictv_data = None
        if len(ictv_chans) != 0:
            from plugins.ictv import process_ictv_channels
            ictv_data = process_ictv_channels(ictv_chans)

        post.date_from = str_converter(post.date_from)
        post.date_until = str_converter(post.date_until)
        return render_template('new.html',
                               l_chan=list_chan_selected,
                               ictv_data=ictv_data,
                               post=post,
                               new=False,
                               l_chan_not=list_chan_not_selected)
    else:
        modify_a_post(request.form, post_id)
        flash("The post was successfully edited.", category='success')
        return redirect(url_for('index'))
Пример #14
0
def publish_edit_post(post_id):
    """
    Method called in the edition page, when the user clicks on the 'Save and publish' button.
    It is called with an http post request.
    The method will save the changes to the post fields in the database.
    :param post_id: The id of the post in the database
    :return: An http error/sucess code
    """
    data = request.get_json(force=True)
    current_user_id = session.get("user_id", "")
    post = db.session.query(Post).filter(Post.id == post_id,
                                         Post.user_id == current_user_id).first()  # retrieve old post
    if post is None:
        return render_template('403.html'), 403
    pubs = db.session.query(Publishing).filter(Publishing.post_id == post_id).all()  # retrieve old publishings
    list_of_channels = channels_available_for_user(current_user_id)
    list_of_channels_name = list()

    for pub in pubs:
        p = (db.session.query(Channel).filter((Channel.id == pub.channel_id))).first()
        if p in list_of_channels:
            list_of_channels.remove(p)

    for elem in list_of_channels:
        list_of_channels_name.append(elem.name)

    for d in data:  # d is a post/publication
        name = d.get('name')
        fields = d.get('fields')

        if name == 'General':
            keys = fields.keys()
            for k in keys:
                if (k == 'date_from') and (fields.get(k) is ''):
                    setattr(post, k, date.today())
                elif (k == 'date_until') and (fields.get(k) is ''):
                    setattr(post, k, date.today() + timedelta(days=7))
                elif k in {'date_from', 'date_until'}:
                    setattr(post, k, datetime_converter(fields.get(k)))
                else:
                    setattr(post, k, fields.get(k))
            db.session.commit()
        elif name in list_of_channels_name:
            for cha in list_of_channels:
                if cha.name == name:
                    create_a_publishing_edit(post, cha, d)
        else:
            for pub in pubs:
                chans = db.session.query(Channel).filter(Channel.id == pub.channel_id).all()
                try:
                    from importlib import import_module
                    chan = db.session.query(Channel).filter(Channel.id == pub.channel_id).first()
                    plugin = import_module(chan.module)
                    can_edit = plugin.can_edit(pub, chan.config)
                except AttributeError:
                    can_edit = False
                if pub.state == State.VALIDATED.value and can_edit:
                    """
                    We use the state 66 to indicate if a post who is already publish, is edited.
                    Don't use this state for anything else !!!
                    """
                    setattr(pub, 'state', 66)
                for chn in chans:
                    if chn.name == name:
                        keys = fields.keys()
                        for k in keys:
                            if (k == 'date_from') and (fields.get(k) is ''):
                                setattr(pub, k, date.today())
                            elif (k == 'date_until') and (fields.get(k) is ''):
                                setattr(pub, k, date.today() + timedelta(days=7))
                            elif k in {'date_from', 'date_until'}:
                                setattr(pub, k, datetime_converter(fields.get(k)))
                            elif k in {'start_hour', 'end_hour'}:
                                setattr(pub, k, time_converter(fields.get(k)))
                            else:
                                setattr(pub, k, fields.get(k))
                        db.session.commit()

    return ('', 200)
Пример #15
0
def create_data_json(post_id):
    """
    This method is called when loading the edition page.
    It is responsible for getting the post and publication data from the database and encoding it inside a json.
    :param post_id: The id of the post in the database
    :return: The data related to the post/publication, in json format
    """
    current_user_id = session.get("user_id", "")

    json_output = dict()
    dic_second = dict()

    query_post = db.session.query(Post).filter(Post.id == post_id, Post.user_id == current_user_id).first()
    query_pubs = db.session.query(Publishing).filter(Publishing.post_id == post_id).all()
    list_of_channels = channels_available_for_user(current_user_id)

    fields = dict((col, getattr(query_post, col)) for col in query_post.__table__.columns.keys())
    entries_to_delete = ('id', 'user_id', 'date_created')
    for entries in entries_to_delete:
        del fields[entries]

    dic_second["fields"] = fields
    json_output["default"] = dic_second

    module = list()
    for pub in query_pubs:
        channel = dict()
        p = (db.session.query(Channel).filter((Channel.id == pub.channel_id))).first()
        if p in list_of_channels:
            list_of_channels.remove(p)
        try:
            from importlib import import_module
            plugin = import_module(p.module)
            can_edit = plugin.can_edit(pub, p.config)
        except AttributeError:
            can_edit = False
        if not (pub.state == State.VALIDATED.value and not can_edit):
            elem = dict((col, getattr(p, col)) for col in p.__table__.columns.keys())
            for e in elem:
                if e == "module" or e == "name":
                    channel[e] = elem[e]
            fields = dict()
            for col in pub.__table__.columns.keys():
                try:
                    val = getattr(pub, col)
                    if col == "state":
                        channel[col] = val
                    elif not col == "post_id" and not col == "channel_id":
                        fields[col] = val
                except AttributeError as error:
                    pass
            channel["fields"] = fields
            module.append(channel)

    for chan in list_of_channels:
        channel = dict()
        channel["name"] = chan.name
        channel["module"] = chan.module
        channel["state"] = -1
        module.append(channel)

    json_output["channels"] = module
    return jsonify(json_output)