Exemplo n.º 1
0
def edit_notification_agent(notification_agent_id):
    State.load()
    notification_agents = NotificationAgent.query.get_or_404(
        notification_agent_id)
    form = NotificationAgentForm()
    if form.validate_on_submit():
        notification_agents.id = notification_agent_id
        notification_agents.module = form.module.data
        notification_agents.name = form.name.data
        notification_agents.webhook_url = form.webhook_url.data
        notification_agents.username = form.username.data
        notification_agents.icon = form.icon.data
        notification_agents.channel = form.channel.data
        db.session.commit()

        State.refresh_notif_agents()

        flash('Your notification agent has been updated!', 'top_flash_success')
        return redirect(
            url_for('main.notification_agents',
                    notification_agent_id=notification_agents.id))
    elif request.method == 'GET':
        form.module.data = notification_agents.module
        form.name.data = notification_agents.name
        form.webhook_url.data = notification_agents.webhook_url
        form.username.data = notification_agents.username
        form.icon.data = notification_agents.icon
        form.channel.data = notification_agents.channel
    return render_template('create-notification-agent.html',
                           title='Update Notification Agent',
                           form=form,
                           legend='Update Notification Agent')
Exemplo n.º 2
0
def edit_source(source_id):
    State.load()
    source = Source.query.get_or_404(source_id)
    form = SourceForm()
    if form.validate_on_submit():
        if form.test.data:
            web_url = form.website.data

            Dict = {1: 'kijiji'}

            prime_source = prime.Source(module=Dict.get(form.module.data),
                                        module_properties={
                                            'url': web_url,
                                            'botname': "prime"
                                        })

            try:
                total_ads = prime.test_webui_source(prime_source).total_new_ads
            except:
                message = "Not a valid source"
            else:
                message = f"Found {total_ads} new ads" \
                    if total_ads != 1 else "Found 1 new ad"
            finally:
                if web_url == "":
                    message = "Not a valid source"
                flash(message, "notification")

        else:
            source.module = form.module.data
            source.name = form.name.data
            source.website = form.website.data
            source.location = form.location.data
            source.range = form.range.data
            # source.subreddit = form.subreddit.data
            db.session.commit()

            State.refresh_sources()

            flash('Your source has been updated!', 'top_flash_success')
            return redirect(url_for('main.sources', source_id=source.id))
    elif request.method == 'GET':
        form.module.data = source.module
        form.name.data = source.name
        form.website.data = source.website
        form.location.data = source.location
        form.range.data = source.range
        # form.subreddit.data = source.subreddit
    return render_template('create-source.html',
                           title='Update Source',
                           form=form,
                           legend='Update Source')
Exemplo n.º 3
0
def create_source():
    State.load()
    form = SourceForm()
    if form.validate_on_submit():
        if form.test.data:
            web_url = form.website.data

            Dict = {1: 'kijiji'}

            prime_source = prime.Source(module=Dict.get(form.module.data),
                                        module_properties={
                                            'url': web_url,
                                            'botname': "prime"
                                        })

            try:
                total_ads = prime.test_webui_source(prime_source).total_new_ads
            except:
                message = "Not a valid source"
            else:
                message = f"Found {total_ads} new ads" \
                    if total_ads != 1 else "Found 1 new ad"
            finally:
                if web_url == "":
                    message = "Not a valid source"
                flash(message, "notification")

        else:
            source = Source(
                module=form.module.data,
                name=form.name.data,
                website=form.website.data,
                location=form.location.data,
                range=form.range.data,
                # subreddit=form.subreddit.data
            )
            db.session.add(source)
            db.session.commit()

            State.refresh_sources()

            flash('Your source has been saved!', 'top_flash_success')
            return redirect(url_for('main.sources'))
    return render_template('create-source.html',
                           title='Create Source',
                           form=form,
                           legend='Create Source')
Exemplo n.º 4
0
def create_notification_agents():
    State.load()
    form = NotificationAgentForm()
    if form.validate_on_submit():
        if form.test.data:
            # This button will send a test notification to the Notification Agent.
            # Will need to add IF statements to send different notification messages to different agents.

            webhook_url = form.webhook_url.data
            if form.username.data:
                username = form.username.data
            else:
                username = "******"

            Dict = {1: 'discord'}

            test_notify = notifAgent.NotifAgent(module=Dict.get(
                form.module.data),
                                                module_properties={
                                                    'webhook': webhook_url,
                                                    'botname': username
                                                })
            notifAgent.test_notif_agent(test_notify)

            # Notification message on webui
            flash("A test message has been sent to your notification agent",
                  "notification")
        else:
            notification_agent = NotificationAgent(
                module=form.module.data,
                name=form.name.data,
                webhook_url=form.webhook_url.data,
                username=form.username.data,
                icon=form.icon.data,
                channel=form.channel.data)
            db.session.add(notification_agent)
            db.session.commit()

            State.refresh_notif_agents()

            flash('Your notification agent has been saved!',
                  'top_flash_success')
            return redirect(url_for('main.notification_agents'))
    return render_template('create-notification-agent.html',
                           title='Create Notification Agent',
                           form=form,
                           legend='Create Notification Agent')
Exemplo n.º 5
0
def delete_source(source_id):
    State.load()

    source = Source.query.get_or_404(source_id)
    tasks = Task.query.all()

    for task in tasks:
        task.source = [s for s in task.source if s != source_id]

        # if this causes a task to  go down to 0 sources, then it should be deleted.
        if len(task.source) == 0:
            db.session.delete(task)

    db.session.delete(source)
    db.session.commit()

    flash('Your source has been deleted.', 'top_flash_success')
    return redirect(url_for('main.sources'))
Exemplo n.º 6
0
def delete_notification_agent(notification_agent_id):
    State.load()

    notification_agent = NotificationAgent.query.get_or_404(
        notification_agent_id)
    tasks = Task.query.all()

    for task in tasks:
        task.notification_agent = [
            s for s in task.notification_agent if s != notification_agent_id
        ]

        # if this causes a task to  go down to 0 notification agents, then it should be deleted.
        if len(task.notification_agent) == 0:
            db.session.delete(task)

    db.session.delete(notification_agent)
    db.session.commit()

    State.refresh_notif_agents()

    flash('Your notification agent has been deleted.', 'top_flash_success')
    return redirect(url_for('main.notification_agents'))
Exemplo n.º 7
0
def create_notification_agents():
    State.load()
    form = NotificationAgentForm()
    if form.validate_on_submit():
        if form.submit.data:
            notification_agent = NotificationAgent(
                module=form.module.data,
                name=form.name.data,
                webhook_url=form.webhook_url.data,
                username=form.username.data,
                icon=form.icon.data)
            db.session.add(notification_agent)
            db.session.commit()

            State.refresh_notif_agents()

            flash('Your notification agent has been saved!',
                  'top_flash_success')
            return redirect(url_for('main.notification_agents'))
    return render_template('create-notification-agent.html',
                           title='Create Notification Agent',
                           form=form,
                           legend='Create Notification Agent')
Exemplo n.º 8
0
def edit_task(task_id):
    State.load()
    edit_task = Task.query.get_or_404(task_id)

    source_count = len(edit_task.source)
    notification_agent_count = len(edit_task.notification_agent)

    class LocalForm(TaskForm):
        pass

    LocalForm.sources = FieldList(FormField(AddSourceForm),
                                  min_entries=source_count)
    LocalForm.notification_agents = FieldList(
        FormField(AddNotificationAgentForm),
        min_entries=notification_agent_count)

    form = LocalForm()
    sourceform = AddSourceForm()
    notificationagentform = AddNotificationAgentForm()

    sourceform.source_select.choices = get_source_choices()
    notificationagentform.notification_agent_select.choices = get_notification_agents_choices(
    )

    #    if form.validate_on_submit():
    if form.submit.data:
        source_list = sorted(request.form.getlist('source_select'), key=int)
        for i in range(0, len(source_list)):
            source_list[i] = int(source_list[i])
        source_list = list(set(source_list))

        notification_agent_list = sorted(
            request.form.getlist('notification_agent_select'), key=int)
        for i in range(0, len(notification_agent_list)):
            notification_agent_list[i] = int(notification_agent_list[i])
        notification_agent_list = list(set(notification_agent_list))

        edit_task.id = task_id
        edit_task.name = form.name.data
        edit_task.frequency = form.frequency.data
        edit_task.source = source_list
        edit_task.notification_agent = notification_agent_list
        edit_task.colour_flag = form.colour_flag.data
        edit_task.must_contain = form.must_contain.data
        edit_task.exclude = form.exclude.data
        db.session.commit()

        State.refresh_tasks()
        task.refresh_cron()

        flash('Your task has been updated!', 'top_flash_success')
        return redirect(url_for('main.tasks', task_id=edit_task.id))
    elif request.method == 'GET':
        form.name.data = edit_task.name
        form.frequency.data = edit_task.frequency
        sourceform.source_select.data = edit_task.source
        notificationagentform.notification_agent_select.data = edit_task.notification_agent
        form.colour_flag.data = edit_task.colour_flag
        form.must_contain.data = edit_task.must_contain
        form.exclude.data = edit_task.exclude

    return render_template(
        'create-task.html',
        title='Update Task',
        form=form,
        sourceform=sourceform,
        notificationagentform=notificationagentform,
        source_data=edit_task.source,
        notification_agent_data=edit_task.notification_agent,
        action='edit',
        legend='Update Task')
Exemplo n.º 9
0
def create_tasks():
    State.load()

    form = TaskForm()
    sourceform = AddSourceForm()
    notificationagentform = AddNotificationAgentForm()

    sourceform.source_select.choices = get_source_choices()
    notificationagentform.notification_agent_select.choices = get_notification_agents_choices(
    )

    if form.colour_flag.data == "":
        cf = "#ff8c00"
    else:
        cf = form.colour_flag.data


#    if form.validate_on_submit():
    if form.submit.data:
        source_list = sorted(request.form.getlist('source_select'), key=int)
        for i in range(0, len(source_list)):
            source_list[i] = int(source_list[i])
        source_list = list(set(source_list))

        notification_agent_list = sorted(
            request.form.getlist('notification_agent_select'), key=int)
        for i in range(0, len(notification_agent_list)):
            notification_agent_list[i] = int(notification_agent_list[i])
        notification_agent_list = list(set(notification_agent_list))

        new_task = Task(name=form.name.data,
                        frequency=form.frequency.data,
                        source=source_list,
                        notification_agent=notification_agent_list,
                        colour_flag=cf,
                        must_contain=form.must_contain.data,
                        exclude=form.exclude.data)

        db.session.add(new_task)
        db.session.commit()

        State.refresh_tasks()
        cron.add(int(form.frequency.data), "minutes")

        if form.prime_count.data:
            prime = form.prime_count.data
        else:
            prime = 0

        prime_task = task.Task(source_ids=source_list,
                               notif_agent_ids=notification_agent_list,
                               include=[form.must_contain.data],
                               exclude=[form.exclude.data],
                               colour_flag=form.colour_flag.data)
        task.prime(prime_task, notify=True, recent_ads=int(prime))

        flash('Your task has been created!', 'top_flash_success')

        return redirect(url_for('main.tasks'))
    return render_template('create-task.html',
                           title='Create a Task',
                           form=form,
                           sourceform=sourceform,
                           notificationagentform=notificationagentform,
                           action='create',
                           legend='Create a Task')