Exemplo n.º 1
0
def enable_plugin(plugin):
    plugin = get_plugin_from_all(plugin)
    if not plugin.enabled:
        plugin_dir = os.path.join(
            os.path.abspath(os.path.dirname(os.path.dirname(__file__))),
            "plugins", plugin.identifier
        )

        disabled_file = os.path.join(plugin_dir, "DISABLED")

        try:
            if os.path.exists(disabled_file):
                os.remove(disabled_file)
                flash(_("Plugin is enabled. Please reload your app."),
                      "success")
            else:
                flash(_("Plugin is already enabled. Please reload  your app."),
                      "warning")

        except OSError:
            flash(_("If you are using a host which doesn't support writting "
                    "on the disk, this won't work - than you need to delete "
                    "the 'DISABLED' file by yourself."), "danger")

    else:
        flash(_("Couldn't enable Plugin."), "danger")

    return redirect(url_for("management.plugins"))
Exemplo n.º 2
0
def manage(group_id):
    """Manage your group."""
    group = Group.query.get_or_404(group_id)
    form = GroupForm(request.form, obj=group)

    if form.validate_on_submit():
        if group.can_edit(current_user):
            try:
                group.update(**form.data)
                flash(_('Group "%(name)s" was updated', name=group.name),
                      'success')
            except Exception as e:
                flash(str(e), 'error')
                return render_template(
                    "invenio_groups/new.html",
                    form=form,
                    group=group,
                )
        else:
            flash(
                _(
                    'You cannot edit group %(group_name)s',
                    group_name=group.name
                ),
                'error'
            )

    return render_template(
        "invenio_groups/new.html",
        form=form,
        group=group,
    )
Exemplo n.º 3
0
def edit_user(user_id):
    user = User.query.filter_by(id=user_id).first_or_404()

    if not can_edit_user(current_user):
        flash(_("You are not allowed to edit this user."), "danger")
        return redirect(url_for("management.users"))

    secondary_group_query = Group.query.filter(
        db.not_(Group.id == user.primary_group_id),
        db.not_(Group.banned),
        db.not_(Group.guest == True))

    form = EditUserForm(user)
    form.secondary_groups.query = secondary_group_query
    if form.validate_on_submit():
        form.populate_obj(user)
        user.primary_group_id = form.primary_group.data.id

        # Don't override the password
        if form.password.data:
            user.password = form.password.data

        user.save(groups=form.secondary_groups.data)

        flash(_("User successfully updated."), "success")
        return redirect(url_for("management.edit_user", user_id=user.id))

    return render_template("management/user_form.html", form=form,
                           title=_("Edit User"))
Exemplo n.º 4
0
def confirm(pid, record, template, **kwargs):
    """Confirm email address."""
    recid = int(pid.pid_value)

    token = request.view_args['token']

    # Validate token
    data = EmailConfirmationSerializer().validate_token(token)
    if data is None:
        flash(_("Invalid confirmation link."), category='danger')
        return redirect(url_for("invenio_records_ui.recid", pid_value=recid))

    # Validate request exists.
    r = AccessRequest.query.get(data['id'])
    if not r:
        abort(404)

    # Confirm email address.
    if r.status != RequestStatus.EMAIL_VALIDATION:
        abort(404)

    r.confirm_email()
    db.session.commit()
    flash(_("Email validated and access request submitted."), category='info')

    return redirect(url_for("invenio_records_ui.recid", pid_value=recid))
Exemplo n.º 5
0
 def publish(self, ids):
     try:
         controllers.set_journal_is_public_bulk(ids, True)
         # Adicionar mais contexto sobre as consequência dessa ação
         flash(_('Periódico(s) publicado(s) com sucesso!!'))
     except Exception as ex:
         flash(_('Ocorreu um erro tentando publicar o(s) periódico(s)!!'), 'error')
Exemplo n.º 6
0
def reset_password(token):
    """
    Handles the reset password process.
    """

    if not current_user.is_anonymous():
        return redirect(url_for("forum.index"))

    form = ResetPasswordForm()
    if form.validate_on_submit():
        user = User.query.filter_by(email=form.email.data).first()
        expired, invalid, data = user.verify_reset_token(form.token.data)

        if invalid:
            flash(_("Your Password Token is invalid."), "danger")
            return redirect(url_for("auth.forgot_password"))

        if expired:
            flash(_("Your Password Token is expired."), "danger")
            return redirect(url_for("auth.forgot_password"))

        if user and data:
            user.password = form.password.data
            user.save()
            flash(_("Your Password has been updated."), "success")
            return redirect(url_for("auth.login"))

    form.token.data = token
    return render_template("auth/reset_password.html", form=form)
Exemplo n.º 7
0
def validate_user(data):
    new_data = dict()
    email_filter = re.compile(
        "^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9]"
        "(?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9]"
        "(?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$"
    )

    if ('newPassword' in data and data['newPassword'] != "" and
            'confirmPassword' in data and data['confirmPassword'] != ""):

        if data['newPassword'] == data['confirmPassword']:
            new_data['password'] = encrypt_password(data['newPassword'])
        else:
            raise Exception(_("Passwords do not match."))

    if 'email' in data and data['email'] != "":
        if email_filter.match(data['email']):
            new_data['email'] = data['email']
        else:
            raise Exception(_("Invalid email address."))

    if 'role' in data and data['role'] != "":
        new_data['roles'] = int(data['role'])

    if 'active' in data and data['active'] != "":
        new_data['active'] = data['active']

    return new_data
Exemplo n.º 8
0
def init_menu():
    """Initialize menu before first request."""
    # Register breadcrumb root
    item = current_menu.submenu('breadcrumbs.settings')
    item.register('', _('Account'))
    item = current_menu.submenu('breadcrumbs.{0}'.format(
        current_app.config['SECURITY_BLUEPRINT_NAME']))

    if current_app.config.get('SECURITY_CHANGEABLE', True):
        item.register('', _('Change password'))

        # Register settings menu
        item = current_menu.submenu('settings.change_password')
        item.register(
            "{0}.change_password".format(
                current_app.config['SECURITY_BLUEPRINT_NAME']),
            # NOTE: Menu item text (icon replaced by a user icon).
            _('%(icon)s Change password',
                icon='<i class="fa fa-key fa-fw"></i>'),
            order=1)

        # Register breadcrumb
        item = current_menu.submenu('breadcrumbs.{0}.change_password'.format(
            current_app.config['SECURITY_BLUEPRINT_NAME']))
        item.register(
            "{0}.change_password".format(
                current_app.config['SECURITY_BLUEPRINT_NAME']),
            _("Change password"),
            order=0,
        )
Exemplo n.º 9
0
    def message(self):
        name, host, port = self.get_server_details()

        if self.backup_type == BACKUP.OBJECT:
            return _(
                "Backing up an object on the server '{0}' "
                "from database '{1}'..."
            ).format(
                "{0} ({1}:{2})".format(
                    name, host, port
                ),
                self.database
            )
        if self.backup_type == BACKUP.GLOBALS:
            return _("Backing up the global objects on "
                     "the server '{0}'...").format(
                "{0} ({1}:{2})".format(
                    name, host, port
                )
            )
        elif self.backup_type == BACKUP.SERVER:
            return _("Backing up the server '{0}'...").format(
                "{0} ({1}:{2})".format(
                    name, host, port
                )
            )
        else:
            # It should never reach here.
            return "Unknown Backup"
Exemplo n.º 10
0
    def validate_moderators(self, field):
        approved_moderators = list()

        if field.data:
            # convert the CSV string in a list
            moderators = field.data.split(",")
            # remove leading and ending spaces
            moderators = [mod.strip() for mod in moderators]
            for moderator in moderators:
                # Check if the usernames exist
                user = User.query.filter_by(username=moderator).first()

                # Check if the user has the permissions to moderate a forum
                if user:
                    if not (user.get_permissions()["mod"] or
                            user.get_permissions()["admin"] or
                            user.get_permissions()["super_mod"]):
                        raise ValidationError(
                            _("%(user)s is not in a moderators group.",
                              user=user.username)
                        )
                    else:
                        approved_moderators.append(user)
                else:
                    raise ValidationError(_("User %(moderator)s not found.",
                                            moderator=moderator))
            field.data = approved_moderators

        else:
            field.data = approved_moderators
Exemplo n.º 11
0
def handle_profile_form(form):
    """Handle profile update form."""
    form.process(formdata=request.form)

    if form.validate_on_submit():
        email_changed = False
        with db.session.begin_nested():
            # Update profile.
            current_userprofile.username = form.username.data
            current_userprofile.full_name = form.full_name.data
            db.session.add(current_userprofile)

            # Update email
            if current_app.config['USERPROFILES_EMAIL_ENABLED'] and \
               form.email.data != current_user.email:
                current_user.email = form.email.data
                current_user.confirmed_at = None
                db.session.add(current_user)
                email_changed = True
        db.session.commit()

        if email_changed:
            send_confirmation_instructions(current_user)
            # NOTE: Flash message after successful update of profile.
            flash(_('Profile was updated. We have sent a verification '
                    'email to %(email)s. Please check it.',
                    email=current_user.email),
                  category='success')
        else:
            # NOTE: Flash message after successful update of profile.
            flash(_('Profile was updated.'), category='success')
Exemplo n.º 12
0
def new_member(group_id):
    """Add (invite) new member."""
    group = Group.query.get_or_404(group_id)

    if group.can_invite_others(current_user):
        form = NewMemberForm()

        if form.validate_on_submit():
            emails = filter(None, form.data['emails'].splitlines())
            group.invite_by_emails(emails)
            flash(_('Requests sent!'), 'success')
            return redirect(url_for('.members', group_id=group.id))

        return render_template(
            "invenio_groups/new_member.html",
            group=group,
            form=form
        )

    flash(
        _(
            'You cannot invite users or yourself (i.e. join) to the group '
            '%(group_name)s',
            group_name=group.name
        ),
        'error'
    )
    return redirect(url_for('.index'))
Exemplo n.º 13
0
def approve(group_id, user_id):
    """Approve a user."""
    membership = Membership.query.get_or_404((user_id, group_id))
    group = membership.group

    if group.can_edit(current_user):
        try:
            membership.accept()
        except Exception as e:
            flash(str(e), 'error')
            return redirect(url_for('.requests', group_id=membership.group.id))

        flash(_('%(user)s accepted to %(name)s group.',
                user=membership.user.email,
                name=membership.group.name), 'success')
        return redirect(url_for('.requests', group_id=membership.group.id))

    flash(
        _(
            'You cannot approve memberships for the group %(group_name)s',
            group_name=group.name
        ),
        'error'
    )
    return redirect(url_for('.index'))
Exemplo n.º 14
0
def leave(group_id):
    """Leave group."""
    group = Group.query.get_or_404(group_id)

    if group.can_leave(current_user):
        try:
            group.remove_member(current_user)
        except Exception as e:
            flash(str(e), "error")
            return redirect(url_for('.index'))

        flash(
            _(
                'You have successfully left %(group_name)s group.',
                group_name=group.name
            ),
            'success'
        )
        return redirect(url_for('.index'))

    flash(
        _(
            'You cannot leave the group %(group_name)s',
            group_name=group.name
        ),
        'error'
    )
    return redirect(url_for('.index'))
Exemplo n.º 15
0
def delete_user(user_id=None):
    # ajax request
    if request.is_xhr:
        ids = request.get_json()["ids"]

        data = []
        for user in User.query.filter(User.id.in_(ids)).all():
            # do not delete current user
            if current_user.id == user.id:
                continue

            if user.delete():
                data.append({
                    "id": user.id,
                    "type": "delete",
                    "reverse": False,
                    "reverse_name": None,
                    "reverse_url": None
                })

        return jsonify(
            message="{} Users deleted.".format(len(data)),
            category="success",
            data=data,
            status=200
        )

    user = User.query.filter_by(id=user_id).first_or_404()
    if current_user.id == user.id:
        flash(_("You cannot delete yourself.", "danger"))
        return redirect(url_for("management.users"))

    user.delete()
    flash(_("User successfully deleted."), "success")
    return redirect(url_for("management.users"))
Exemplo n.º 16
0
    def details(self, cmd, args):
        # Fetch the server details like hostname, port, roles etc
        s = Server.query.filter_by(
            id=self.sid, user_id=current_user.id
        ).first()

        res = '<div class="h5">'

        res += html.safe_str(
            _(
                "Restoring backup on the server '{0}'..."
            ).format(
                "{0} ({1}:{2})".format(s.name, s.host, s.port)
            )
        )

        res += '</div><div class="h5"><b>'
        res += html.safe_str(
            _("Running command:")
        )
        res += '</b><br><span class="pg-bg-cmd enable-selection">'
        res += html.safe_str(cmd)
        res += self.cmd
        res += '</span></div>'

        return res
Exemplo n.º 17
0
    def __init__(self, app=None, test_param=None):
        """Extension initialization."""
        self.test_param = test_param

        _('A translation string')
        if app:
            self.init_app(app)
Exemplo n.º 18
0
    def details(self, cmd, args):
        # Fetch the server details like hostname, port, roles etc
        s = Server.query.filter_by(
            id=self.sid, user_id=current_user.id
        ).first()

        res = '<div>'
        res += _(
            "Copying table data '{0}.{1}' on database '{2}' "
            "for the server '{3}'..."
        ).format(
            html.safe_str(self.schema),
            html.safe_str(self.table),
            html.safe_str(self.database),
            "{0} ({1}:{2})".format(
                html.safe_str(s.name),
                html.safe_str(s.host),
                html.safe_str(s.port)
            )
        )

        res += '</div><div>'
        res += _("Running command:")
        res += '</b><br><span class="pg-bg-cmd enable-selection">'
        res += html.safe_str(self._cmd)
        res += '</span></div>'

        return res
Exemplo n.º 19
0
 def load_grants(self, data):
     """Load grants."""
     if not isinstance(data, list):
         raise ValidationError(_('Not a list.'))
     result = set()
     errors = set()
     for g in data:
         if not isinstance(g, dict):
             raise ValidationError(_('Element not an object.'))
         g = g.get('id')
         if not g:
             continue
         # FP7 project grant
         if not g.startswith('10.13039/'):
             g = '10.13039/501100000780::{0}'.format(g)
         # Check that the PID exists
         grant_pid = PersistentIdentifier.query.filter_by(
             pid_type='grant', pid_value=g).one_or_none()
         if not grant_pid or grant_pid.status != PIDStatus.REGISTERED:
             errors.add(g)
             continue
         result.add(g)
     if errors:
         raise ValidationError(
             'Invalid grant ID(s): {0}'.format(', '.join(errors)),
             field_names='grants')
     return [{'$ref': 'https://dx.zenodo.org/grants/{0}'.format(grant_id)}
             for grant_id in result] or missing
Exemplo n.º 20
0
def accessrequest(request_id):
    """Accept/reject access request."""
    r = AccessRequest.get_by_receiver(request_id, current_user)
    if not r or r.status != RequestStatus.PENDING:
        abort(404)

    form = ApprovalForm(request.form)

    if form.validate_on_submit():
        if form.accept.data:
            r.accept(message=form.data['message'],
                     expires_at=form.expires_at.data)
            flash(_("Request accepted."))
            return redirect(url_for(".index"))
        elif form.reject.data:
            r.reject(message=form.data['message'])
            flash(_("Request rejected."))
            return redirect(url_for(".index"))

    return render_template(
        "accessrequests/settings/request.html",
        accessrequest=r,
        record=get_record(r.recid),
        form=form,
    )
Exemplo n.º 21
0
def forget():
    '''忘记密码'''
    error = None
    form = ForgetPasswdForm()
    if form.validate_on_submit() and request.method == "POST":
        validate_code = session['validate_code']
        validate = form.code.data
        if validate.lower() != validate_code.lower():
            return jsonify(judge=False, error=_('The validate code is error'))
        else:
            exsited_email = User.query.filter_by(
                email=form.confirm_email.data).first()
            if exsited_email:
                '''email模板'''
                from random import sample
                from string import ascii_letters, digits
                npasswd = ''.join(sample(ascii_letters + digits, 8))
                exsited_email.passwd = generate_password_hash(npasswd)
                db.session.commit()
                html = render_template('templet/forget.html',
                                       confirm_url=npasswd)
                subject = "Please update your password in time"
                email_send(form.confirm_email.data, html, subject)
                flash(_(
                    'An email has been sent to you.Please receive and update your password in time'))
                return jsonify(judge=True, error=error)
            else:
                error = _('The email is error')
                return jsonify(judge=False, error=error)
    else:
        if form.errors:
            return return_errors(form)
        else:
            pass
        return render_template('auth/forget.html', form=form)
Exemplo n.º 22
0
    def message(self):
        # Fetch the server details like hostname, port, roles etc
        s = Server.query.filter_by(
            id=self.sid, user_id=current_user.id
        ).first()

        if self.backup_type == BACKUP.OBJECT:
            return _(
                "Backing up an object on the server '{0}' "
                "from database '{1}'..."
            ).format(
                "{0} ({1}:{2})".format(
                    s.name, s.host, s.port
                ),
                self.database
            )
        if self.backup_type == BACKUP.GLOBALS:
            return _("Backing up the global objects on "
                     "the server '{0}'...").format(
                "{0} ({1}:{2})".format(
                    s.name, s.host, s.port
                )
            )
        elif self.backup_type == BACKUP.SERVER:
            return _("Backing up the server '{0}'...").format(
                "{0} ({1}:{2})".format(
                    s.name, s.host, s.port
                )
            )
        else:
            # It should never reach here.
            return "Unknown Backup"
Exemplo n.º 23
0
    def node(self, gid, sid, rid):

        status, rset = self.conn.execute_2darray(
            render_template(
                self.sql_path + 'nodes.sql',
                rid=rid
            )
        )

        if not status:
            return internal_server_error(
                _(
                    "Error fetching role information from the database "
                    "server.\n{0}"
                ).format(rset)
            )

        for row in rset['rows']:
            return make_json_response(
                data=self.blueprint.generate_browser_node(
                    row['oid'], sid,
                    row['rolname'],
                    'icon-role' if row['rolcanlogin'] else 'icon-group',
                    can_login=row['rolcanlogin'],
                    is_superuser=row['rolsuper']
                ),
                status=200
            )

        return gone(_("Could not find the role information."))
Exemplo n.º 24
0
def report_markread(report_id=None):
    # mark single report as read
    if report_id:

        report = Report.query.filter_by(id=report_id).first_or_404()
        if report.zapped:
            flash(_("Report %(id)s is already marked as read.", id=report.id),
                  "success")
            return redirect(url_for("management.reports"))

        report.zapped_by = current_user.id
        report.zapped = datetime.utcnow()
        report.save()
        flash(_("Report %(id)s marked as read.", id=report.id), "success")
        return redirect(url_for("management.reports"))

    # mark all as read
    reports = Report.query.filter(Report.zapped == None).all()
    report_list = []
    for report in reports:
        report.zapped_by = current_user.id
        report.zapped = datetime.utcnow()
        report_list.append(report)

    db.session.add_all(report_list)
    db.session.commit()

    flash(_("All reports were marked as read."), "success")
    return redirect(url_for("management.reports"))
Exemplo n.º 25
0
    def feeds_import_from(self, feed_name, feed_lang):
        try:
            feeds = current_app.config['RSS_NEWS_FEEDS']

            if feed_lang not in list(feeds.keys()):
                msg = _('O idioma: %s, não consta no nosso cadastro de feeds.' % (
                    feed_lang))
                flash(msg, 'error')
            elif feed_name not in feeds[feed_lang]['display_name']:
                msg = _('O feed: "%s", no idioma: "%s", não consta no nosso cadastro de feeds.' % (
                    feed_name, feed_lang))
                flash(msg, 'error')
            else:
                feed_url = feeds[feed_lang]['url']
                imported_ok, error_msg = import_feed(feed_url, feed_lang)
                if imported_ok:
                    msg = _('O feed: %s [%s], foi importado com sucesso !!' % (
                        feed_name, feed_lang))
                    flash(msg)
                else:
                    # logger.error(error_msg)
                    msg = _('Ocorreu um erro tentando importar o feed: %s [%s].' % (
                        feed_name, feed_lang))
                    flash(msg, 'error')
        except Exception as ex:
            msg = _('Ocorreu um erro tentando atualizar os feed RSS!!, %s' % str(ex))
            flash(msg, 'error')
        return redirect(url_for('.index_view'))
Exemplo n.º 26
0
def pressrelease(url_seg, url_seg_issue, url_seg_article, lang_code):
    journal = controllers.get_journal_by_url_seg(url_seg)

    issue = controllers.get_issue_by_url_seg(journal.url_segment, url_seg_issue)

    if url_seg_article:
        article = controllers.get_article_by_url_seg(url_seg_article)
    else:
        article = None

    if not journal:
        abort(404, _('Periódico não encontrado'))

    if not issue:
        abort(404, _('Fascículo não encontrado'))

    press_release = controllers.get_press_release(journal, issue, lang_code, article)

    if not press_release:
        abort(404, _('Press Release não encontrado'))

    context = {
        'press_release': press_release
    }

    return render_template("includes/press_release.html", **context)
Exemplo n.º 27
0
 def __init__(self, app=None):
     """Extension initialization."""
     # TODO: This is an example of translation string with comment. Please
     # remove it.
     # NOTE: This is a note to a translator.
     _('A translation string')
     if app:
         self.init_app(app)
Exemplo n.º 28
0
def new_conversation():
    form = ConversationForm()
    to_user = request.args.get("to_user")

    message_count = Conversation.query.\
        filter(Conversation.user_id == current_user.id).\
        count()

    if message_count >= flaskbb_config["MESSAGE_QUOTA"]:
        flash(_("You cannot send any messages anymore because you have"
                "reached your message limit."), "danger")
        return redirect(url_for("message.inbox"))

    if request.method == "POST":
        if "save_message" in request.form and form.validate():
            to_user = User.query.filter_by(username=form.to_user.data).first()

            shared_id = uuid.uuid4()

            form.save(from_user=current_user.id,
                      to_user=to_user.id,
                      user_id=current_user.id,
                      unread=False,
                      as_draft=True,
                      shared_id=shared_id)

            flash(_("Message saved."), "success")
            return redirect(url_for("message.drafts"))

        if "send_message" in request.form and form.validate():
            to_user = User.query.filter_by(username=form.to_user.data).first()

            # this is the shared id between conversations because the messages
            # are saved on both ends
            shared_id = uuid.uuid4()

            # Save the message in the current users inbox
            form.save(from_user=current_user.id,
                      to_user=to_user.id,
                      user_id=current_user.id,
                      unread=False,
                      shared_id=shared_id)

            # Save the message in the recievers inbox
            form.save(from_user=current_user.id,
                      to_user=to_user.id,
                      user_id=to_user.id,
                      unread=True,
                      shared_id=shared_id)
            to_user.invalidate_cache(permissions=False)

            flash(_("Message sent."), "success")
            return redirect(url_for("message.sent"))
    else:
        form.to_user.data = to_user

    return render_template("message/message_form.html", form=form,
                           title=_("Compose Message"))
Exemplo n.º 29
0
def access_request(pid, record, template, **kwargs):
    """Create an access request."""
    recid = int(pid.pid_value)
    datastore = LocalProxy(
        lambda: current_app.extensions['security'].datastore)

    # Record must be in restricted access mode.
    if record.get('access_right') != 'restricted' or \
       not record.get('access_conditions'):
        abort(404)

    # Record must have an owner and owner must still exists.
    owners = record.get('owners', [])
    record_owners = [datastore.find_user(id=owner_id) for owner_id in owners]
    if not record_owners:
        abort(404)

    sender = None
    initialdata = dict()

    # Prepare initial form data
    if current_user.is_authenticated:
        sender = current_user
        initialdata['email'] = current_user.email
        if current_user.profile:
            initialdata['full_name'] = current_user.profile.full_name

    # Normal form validation
    form = AccessRequestForm(formdata=request.form, **initialdata)

    if form.validate_on_submit():
        accreq = AccessRequest.create(
            recid=recid,
            receiver=record_owners[0],
            sender_full_name=form.data['full_name'],
            sender_email=form.data['email'],
            justification=form.data['justification'],
            sender=sender
        )
        db.session.commit()

        if accreq.status == RequestStatus.EMAIL_VALIDATION:
            flash(_(
                "Email confirmation needed: We have sent you an email to "
                "verify your address. Please check the email and follow the "
                "instructions to complete the access request."),
                category='info')
        else:
            flash(_("Access request submitted."), category='info')
        return redirect(url_for('invenio_records_ui.recid', pid_value=recid))

    return render_template(
        template,
        pid=pid,
        record=record,
        form=form,
        owners=record_owners,
    )
Exemplo n.º 30
0
def curate(community):
    """Index page with uploader and list of existing depositions.

    :param community_id: ID of the community to curate.
    """
    if request.method == 'POST':
        action = request.json.get('action')
        recid = request.json.get('recid')

        # 'recid' is mandatory
        if not recid:
            return jsonify({'status': 'danger', 'msg': _('Unknown record')})
        if action not in ['accept', 'reject', 'remove']:
            return jsonify({'status': 'danger', 'msg': _('Unknown action')})

        # Resolve recid to a Record
        resolver = Resolver(
            pid_type='recid', object_type='rec', getter=Record.get_record)
        pid, record = resolver.resolve(recid)

        action_name = ""
        status = "success"
        # Perform actions
        try:
            if action == "accept":
                community.accept_record(record)
                action_name = "added to"
            elif action == "reject":
                community.reject_record(record)
                action_name = "rejected from"
                status = "info"
            elif action == "remove":
                community.remove_record(record)
                action_name = "removed from"
                status = "info"
        except CommunitiesError:
            return jsonify({
                'status': 'danger',
                'msg': _('record not in the curation list,'
                         ' please refresh the page.')})

        record.commit()
        db.session.commit()
        RecordIndexer().index_by_id(record.id)
        title = ""
        if "title_statement" in record \
            and "title" in record["title_statement"]:
            title = record["title_statement"]["title"]
        message = _('The record '
            '"{}" has been {} the community.').format(title, action_name)
        return jsonify({'status': status, 'msg': message})

    ctx = {'community': community}
    return render_template(
        current_app.config['COMMUNITIES_CURATE_TEMPLATE'],
        **ctx
    )
Exemplo n.º 31
0
import pytz
from dateutil import parser
from flask import current_app
from flask_babelex import gettext as _
from flask_security import current_user

import config
from pgadmin.model import Process, db
from io import StringIO

PROCESS_NOT_STARTED = 0
PROCESS_STARTED = 1
PROCESS_FINISHED = 2
PROCESS_TERMINATED = 3
PROCESS_NOT_FOUND = _("Could not find a process with the specified ID.")


def get_current_time(format='%Y-%m-%d %H:%M:%S.%f %z'):
    """
    Generate the current time string in the given format.
    """
    return datetime.utcnow().replace(tzinfo=pytz.utc).strftime(format)


class IProcessDesc(object, metaclass=ABCMeta):
    @abstractproperty
    def message(self):
        pass

    @abstractmethod
Exemplo n.º 32
0
class CollectForm(Form):
    name = StringField(_('Name:'), [DataRequired()])
    description = TextAreaField(_('Description:'))
    is_hidden = RadioField('Is_hidden:',
                           choices=[(0, 'is_hidden'), (1, 'is_public')],
                           coerce=int)
Exemplo n.º 33
0
    def start(self, cb=None):
        def which(program, paths):
            def is_exe(fpath):
                return os.path.exists(fpath) and os.access(fpath, os.X_OK)

            for path in paths:
                if not os.path.isdir(path):
                    continue
                exe_file = os.path.join(u_encode(path, fs_encoding), program)
                if is_exe(exe_file):
                    return file_quote(exe_file)
            return None

        if self.stime is not None:
            if self.etime is None:
                raise RuntimeError(_('The process has already been started.'))
            raise RuntimeError(
                _('The process has already finished and cannot be restarted.'))

        executor = file_quote(
            os.path.join(os.path.dirname(u_encode(__file__)),
                         'process_executor.py'))
        paths = os.environ['PATH'].split(os.pathsep)
        interpreter = None

        current_app.logger.info("Process Executor: Operating System Path %s",
                                str(paths))

        if os.name == 'nt':
            paths.insert(0, os.path.join(u_encode(sys.prefix), 'Scripts'))
            paths.insert(0, u_encode(sys.prefix))

            interpreter = which('pythonw.exe', paths)
            if interpreter is None:
                interpreter = which('python.exe', paths)

            current_app.logger.info(
                "Process Executor: Interpreter value in path: %s",
                str(interpreter))
            if interpreter is None and current_app.PGADMIN_RUNTIME:
                # We've faced an issue with Windows 2008 R2 (x86) regarding,
                # not honouring the environment variables set under the Qt
                # (e.g. runtime), and also setting PYTHONHOME same as
                # sys.executable (i.e. pgAdmin4.exe).
                #
                # As we know, we're running it under the runtime, we can assume
                # that 'venv' directory will be available outside of 'bin'
                # directory.
                #
                # We would try out luck to find python executable based on that
                # assumptions.
                bin_path = os.path.dirname(sys.executable)

                venv = os.path.realpath(os.path.join(bin_path, '..\\venv'))

                interpreter = which('pythonw.exe', [venv])
                if interpreter is None:
                    interpreter = which('python.exe', [venv])

                current_app.logger.info(
                    "Process Executor: Interpreter value in virtual "
                    "environment: %s", str(interpreter))

                if interpreter is not None:
                    # Our assumptions are proven right.
                    # Let's append the 'bin' directory to the PATH environment
                    # variable. And, also set PYTHONHOME environment variable
                    # to 'venv' directory.
                    os.environ['PATH'] = bin_path + ';' + os.environ['PATH']
                    os.environ['PYTHONHOME'] = venv
        else:
            # Let's not use sys.prefix in runtime.
            # 'sys.prefix' is not identified on *nix systems for some unknown
            # reason, while running under the runtime.
            # We're already adding '<installation path>/pgAdmin 4/venv/bin'
            # directory in the PATH environment variable. Hence - it will
            # anyway be the redundant value in paths.
            if not current_app.PGADMIN_RUNTIME:
                paths.insert(0, os.path.join(u_encode(sys.prefix), 'bin'))
            python_binary_name = 'python{0}'.format(sys.version_info[0])
            interpreter = which(u_encode(python_binary_name), paths)

        p = None
        cmd = [
            interpreter if interpreter is not None else 'python', executor,
            self.cmd
        ]
        cmd.extend(self.args)

        current_app.logger.info(
            "Executing the process executor with the arguments: %s", str(cmd))

        # Make a copy of environment, and add new variables to support
        env = os.environ.copy()
        env['PROCID'] = self.id
        env['OUTDIR'] = self.log_dir
        env['PGA_BGP_FOREGROUND'] = "1"

        if self.env:
            env.update(self.env)

        if cb is not None:
            cb(env)

        if os.name == 'nt':
            DETACHED_PROCESS = 0x00000008
            from subprocess import CREATE_NEW_PROCESS_GROUP

            # We need to redirect the standard input, standard output, and
            # standard error to devnull in order to allow it start in detached
            # mode on
            stdout = os.devnull
            stderr = stdout
            stdin = open(os.devnull, "r")
            stdout = open(stdout, "a")
            stderr = open(stderr, "a")

            p = Popen(cmd,
                      close_fds=False,
                      env=env,
                      stdout=stdout.fileno(),
                      stderr=stderr.fileno(),
                      stdin=stdin.fileno(),
                      creationflags=(CREATE_NEW_PROCESS_GROUP
                                     | DETACHED_PROCESS))
        else:

            def preexec_function():
                import signal
                # Detaching from the parent process group
                os.setpgrp()
                # Explicitly ignoring signals in the child process
                signal.signal(signal.SIGINT, signal.SIG_IGN)

            # if in debug mode, wait for process to complete and
            # get the stdout and stderr of popen.
            if config.CONSOLE_LOG_LEVEL <= logging.DEBUG:
                p = Popen(cmd,
                          close_fds=True,
                          stdout=PIPE,
                          stderr=PIPE,
                          stdin=None,
                          preexec_fn=preexec_function,
                          env=env)

                output, errors = p.communicate()
                output = output.decode() \
                    if hasattr(output, 'decode') else output
                errors = errors.decode() \
                    if hasattr(errors, 'decode') else errors
                current_app.logger.debug(
                    'Process Watcher Out:{0}'.format(output))
                current_app.logger.debug(
                    'Process Watcher Err:{0}'.format(errors))
            else:
                p = Popen(cmd,
                          close_fds=True,
                          stdout=None,
                          stderr=None,
                          stdin=None,
                          preexec_fn=preexec_function,
                          env=env)

        self.ecode = p.poll()

        # Execution completed immediately.
        # Process executor cannot update the status, if it was not able to
        # start properly.
        if self.ecode is not None and self.ecode != 0:
            # There is no way to find out the error message from this process
            # as standard output, and standard error were redirected to
            # devnull.
            p = Process.query.filter_by(pid=self.id,
                                        user_id=current_user.id).first()
            p.start_time = p.end_time = get_current_time()
            if not p.exit_code:
                p.exit_code = self.ecode
            p.process_state = PROCESS_FINISHED
            db.session.commit()
        else:
            # Update the process state to "Started"
            p = Process.query.filter_by(pid=self.id,
                                        user_id=current_user.id).first()
            p.process_state = PROCESS_STARTED
            db.session.commit()
Exemplo n.º 34
0
 def validate_email(self, field):
     email = Utilisateur.select().where(
         Utilisateur.email == field.data).first()
     if email:
         raise ValidationError(_("This E-Mail Address is already taken."))
Exemplo n.º 35
0
class RegisterForm(BaseForm):
    email = StringField(_('Email:'), [DataRequired(), Email()])
Exemplo n.º 36
0
def _process_analysis(event, form_id, location_id=None, tag=None):
    form = forms.fget_or_404(id=form_id)
    location = locations.fget_or_404(id=location_id) \
        if location_id else locations.root(event.location_set_id)

    template_name = ''
    tags = []
    breadcrumbs = [_('Process Data'), form.name]
    grouped = False
    display_tag = None
    event = g.event
    filter_class = filters.make_submission_analysis_filter(event, form)

    location_ids = models.LocationPath.query.with_entities(
        models.LocationPath.descendant_id).filter_by(
            ancestor_id=location.id, location_set_id=event.location_set_id)

    # set the correct template and fill out the required data
    if form.form_type in ['CHECKLIST', 'SURVEY']:
        if tag:
            template_name = 'process_analysis/checklist_summary_breakdown.html'
            tags.append(tag)
            display_tag = tag
            grouped = True
        else:
            template_name = 'process_analysis/checklist_summary.html'
            form._populate_field_cache()
            tags.extend([
                f['tag'] for f in form._field_cache.values()
                if f['analysis_type'] != 'N/A'
            ])
            grouped = False

        query_kwargs = {'event': event, 'form': form}
        if not form.untrack_data_conflicts and form.form_type == 'CHECKLIST':
            query_kwargs['submission_type'] = 'M'
        else:
            query_kwargs['submission_type'] = 'O'
        queryset = submissions.find(**query_kwargs).filter(
            models.Submission.location_id.in_(location_ids),
            models.Submission.quarantine_status != 'A')
    else:
        grouped = True
        queryset = submissions.find(event=event, form=form).filter(
            models.Submission.location_id.in_(location_ids))
        template_name = 'process_analysis/critical_incident_summary.html'

        if tag:
            # a slightly different filter, one prefiltering
            # on the specified tag
            display_tag = tag
            template_name = 'process_analysis/critical_incidents_locations.html'  # noqa
            filter_class = \
                filters.make_incident_location_filter(event, form, tag)

    # create data filter
    filter_set = filter_class(queryset, request.args)
    breadcrumb_data = analysis_breadcrumb_data(form, location, display_tag)

    if breadcrumb_data['tag']:
        breadcrumbs.extend([{
            'text':
            location.name,
            'url':
            url_for('process_analysis.process_analysis_with_location_and_tag',
                    form_id=breadcrumb_data['form'].id,
                    location_id=location.id,
                    tag=breadcrumb_data['tag'])
            if idx < len(breadcrumb_data.get('locations', [])) - 1 else ''
        } for idx, location in enumerate(breadcrumb_data.get('locations', []))
                            ])
    else:
        breadcrumbs.extend([{
            'text':
            location.name,
            'url':
            url_for('process_analysis.process_analysis_with_location',
                    form_id=breadcrumb_data['form'].id,
                    location_id=location.id)
            if idx < len(breadcrumb_data.get('locations', [])) - 1 else ''
        } for idx, location in enumerate(breadcrumb_data.get('locations', []))
                            ])

    # set up template context
    context = {}
    context['dataframe'] = make_submission_dataframe(filter_set.qs, form)
    context['breadcrumbs'] = breadcrumbs
    context['display_tag'] = display_tag
    context['filter_form'] = filter_set.form
    context['form'] = form
    context['location'] = location
    context['field_groups'] = OrderedDict()
    context['navigation_data'] = analysis_navigation_data(
        form, location, display_tag)

    # processing for incident forms
    if form.form_type == 'INCIDENT':
        if display_tag:
            context['form_field'] = form.get_field_by_tag(display_tag)
            context['location_types'] = location_types.find(is_political=True)
            context['incidents'] = filter_set.qs
        else:
            incidents_summary = generate_incidents_data(
                form, filter_set.qs, location, grouped, tags)
            context['incidents_summary'] = incidents_summary

        detail_visible = False
        for group in form.data['groups']:
            process_fields = sorted([
                field for field in group['fields'] if
                field['analysis_type'] != 'N/A' and field['type'] != 'boolean'
            ],
                                    key=itemgetter('tag'))
            context['field_groups'][group['name']] = process_fields
            if process_fields:
                detail_visible = True

        context['detail_visible'] = detail_visible
    else:
        for group in form.data['groups']:
            process_fields = sorted([
                field
                for field in group['fields'] if field['analysis_type'] != 'N/A'
            ],
                                    key=itemgetter('tag'))
            context['field_groups'][group['name']] = process_fields

        process_summary = generate_process_data(form,
                                                filter_set.qs,
                                                location,
                                                grouped=True,
                                                tags=tags)
        context['process_summary'] = process_summary

    return render_template(template_name, **context)
Exemplo n.º 37
0
def init_menu():
    """Create the header menus."""
    item = current_menu.submenu('main.menu')
    item.register(
        endpoint=None,
        text='{icon} <span class="{visible}">{menu}'.format(
            icon='<i class="fa fa-bars"></i>',
            visible='visible-md-inline visible-lg-inline',
            menu=_('Menu')
        ),
        order=0
    )

    order = 10

    def return_language(lang):
        return dict(lang_code=lang)

    def hide_language(lang):
        return current_i18n.language != lang

    for language_item in current_i18n.get_locales():
        item = current_menu.submenu(
            'main.menu.lang_{language}'.format(
                language=language_item.language))
        item.register(
            endpoint='invenio_i18n.set_lang',
            endpoint_arguments_constructor=partial(
                return_language, language_item.language),
            text='{icon} {language}'.format(
                icon='<i class="fa fa-language"></i>',
                language=_(i18n_to_str(language_item.language))
            ),
            visible_when=partial(hide_language, language_item.language),
            order=order
        )
        order += 1

    item = current_menu.submenu('main.menu.help')
    item.register(
        endpoint='rero_ils.help',
        text='{icon} {help}'.format(
            icon='<i class="fa fa-info"></i>',
            help=_('Help')
        ),
        order=100
    )

    item = current_menu.submenu('main.profile')
    account = _('My Account')
    if current_user.is_authenticated:
        patron = Patron.get_patron_by_email(current_user.email)
        if patron:
            account = patron.initial
    item.register(
        endpoint=None,
        text='{icon} <span class="{visible}">{account}</span>'.format(
            icon='<i class="fa fa-user"></i>',
            visible='visible-md-inline visible-lg-inline',
            account=account
        ),
        order=1
    )

    item = current_menu.submenu('main.profile.login')
    item.register(
        endpoint='security.login',
        endpoint_arguments_constructor=lambda: dict(next=request.path),
        visible_when=lambda: not current_user.is_authenticated,
        text='{icon} {login}'.format(
            icon='<i class="fa fa-sign-in"></i>',
            login=_('Login')
        ),
        order=1
    )

    item = current_menu.submenu('main.profile.logout')
    item.register(
        endpoint='security.logout',
        visible_when=lambda: current_user.is_authenticated,
        text='{icon} {logout}'.format(
            icon='<i class="fa fa-sign-out"></i>',
            logout=_('Logout')
        ),
        order=1
    )

    item = current_menu.submenu('main.profile.signup')
    item.register(
        endpoint='security.register',
        visible_when=lambda: not current_user.is_authenticated,
        text='{icon} {signup}'.format(
            icon='<i class="fa fa-user-plus"></i>',
            signup=_('Sign Up')
        ),
        order=2
    )
Exemplo n.º 38
0
class ProfileForm(Form):
    introduce = TextAreaField(_('Introduce:'), [Length(max=256)])
    school = StringField(_('School:'), [Length(max=256)])
    word = TextAreaField(_('Signature:'), [Length(max=256)])
Exemplo n.º 39
0
    def sql(self, gid, sid, jid):
        """
        This function will generate sql for sql panel
        """
        SQL = render_template("/".join([self.template_path, 'properties.sql']),
                              jid=jid,
                              conn=self.conn,
                              last_system_oid=0)
        status, res = self.conn.execute_dict(SQL)
        if not status:
            return internal_server_error(errormsg=res)

        if len(res['rows']) == 0:
            return gone(_("Could not find the object on the server."))

        row = res['rows'][0]

        status, res = self.conn.execute_dict(
            render_template(
                "/".join([self.template_path, 'steps.sql']),
                jid=jid,
                conn=self.conn,
                has_connstr=self.manager.db_info['pgAgent']['has_connstr']))
        if not status:
            return internal_server_error(errormsg=res)

        row['jsteps'] = res['rows']

        status, res = self.conn.execute_dict(
            render_template("/".join([self.template_path, 'schedules.sql']),
                            jid=jid,
                            conn=self.conn))
        if not status:
            return internal_server_error(errormsg=res)

        row['jschedules'] = res['rows']
        for schedule in row['jschedules']:
            schedule['jscexceptions'] = []
            if schedule['jexid']:
                idx = 0
                for exc in schedule['jexid']:
                    # Convert datetime.time object to string
                    if isinstance(schedule['jextime'][idx], time):
                        schedule['jextime'][idx] = \
                            schedule['jextime'][idx].strftime("%H:%M:%S")
                    schedule['jscexceptions'].append({
                        'jexid':
                        exc,
                        'jexdate':
                        schedule['jexdate'][idx],
                        'jextime':
                        schedule['jextime'][idx]
                    })
                    idx += 1
            del schedule['jexid']
            del schedule['jexdate']
            del schedule['jextime']

        return ajax_response(response=render_template(
            "/".join([self.template_path, 'create.sql']),
            jid=jid,
            data=row,
            conn=self.conn,
            fetch_id=False,
            has_connstr=self.manager.db_info['pgAgent']['has_connstr']))
Exemplo n.º 40
0
    def delete(self, gid, sid, did, scid, pkgid=None, only_sql=False):
        """
        This function will drop the object

        Args:
          gid: Server Group ID
          sid: Server ID
          did: Database ID
          scid: Schema ID
          pkgid: Package ID
          only_sql: Return SQL only if True

        Returns:

        """

        if pkgid is None:
            data = request.form if request.form else json.loads(
                request.data, encoding='utf-8'
            )
        else:
            data = {'ids': [pkgid]}

        # Below will decide if it's simple drop or drop with cascade call

        cascade = self._check_cascade_operation()

        try:
            for pkgid in data['ids']:
                sql = render_template(
                    "/".join([self.template_path, self._PROPERTIES_SQL]),
                    scid=scid,
                    pkgid=pkgid)
                status, res = self.conn.execute_dict(sql)
                if not status:
                    return internal_server_error(errormsg=res)

                elif not res['rows']:
                    return make_json_response(
                        success=0,
                        errormsg=_(
                            'Error: Object not found.'
                        ),
                        info=self.not_found_error_msg()
                    )

                res['rows'][0]['schema'] = self.schema

                sql = render_template("/".join([self.template_path,
                                                self._DELETE_SQL]),
                                      data=res['rows'][0],
                                      cascade=cascade)

                if only_sql:
                    return sql

                status, res = self.conn.execute_scalar(sql)
                if not status:
                    return internal_server_error(errormsg=res)

            return make_json_response(
                success=1,
                info=_("Package dropped")
            )

        except Exception as e:
            return internal_server_error(errormsg=str(e))
Exemplo n.º 41
0
    def create(self, gid, sid, did, scid):
        """
        Create the package.

        Args:
          gid: Server Group ID
          sid: Server ID
          did: Database ID
          scid: Schema ID

        Returns:

        """
        required_args = [
            'name',
            'pkgheadsrc'
        ]

        data = request.form if request.form else json.loads(
            request.data, encoding='utf-8'
        )

        for arg in required_args:
            if arg not in data:
                return make_json_response(
                    status=400,
                    success=0,
                    errormsg=_(
                        "Could not find the required parameter ({})."
                    ).format(arg)
                )
        data['schema'] = self.schema

        sql, name = self.getSQL(data=data, scid=scid, pkgid=None)

        status, msg = self.conn.execute_scalar(sql)
        if not status:
            return internal_server_error(errormsg=msg)

        # We need oid of newly created package.
        sql = render_template(
            "/".join([
                self.template_path, self._OID_SQL
            ]),
            name=data['name'], scid=scid
        )

        sql = sql.strip('\n').strip(' ')
        if sql and sql != "":
            status, pkgid = self.conn.execute_scalar(sql)
            if not status:
                return internal_server_error(errormsg=pkgid)

        return jsonify(
            node=self.blueprint.generate_browser_node(
                pkgid,
                scid,
                data['name'],
                icon=self.node_icon
            )
        )
Exemplo n.º 42
0
class SearchForm(Form):
    search = StringField(_('search'), validators=[DataRequired()])
Exemplo n.º 43
0
class SortForm(Form):
    within = SelectField(_('Choice'), coerce=int, choices=WITHIN)
    orderby = SelectField('orderby', coerce=int, choices=ORDERBY)
    desc = SelectField('Up and Down', coerce=int, choices=DESC)
Exemplo n.º 44
0
class LoginForm(BaseForm):
    remember = BooleanField(_('Remember me'), default=False)
Exemplo n.º 45
0
        def wrap(self, **kwargs):

            data = None
            if request.data:
                data = json.loads(request.data, encoding='utf-8')
            else:
                data = dict()
                req = request.args or request.form

                for key in req:

                    val = req[key]
                    if key in [
                            u'rolcanlogin', u'rolsuper', u'rolcreatedb',
                            u'rolcreaterole', u'rolinherit', u'rolreplication',
                            u'rolcatupdate', u'variables', u'rolmembership',
                            u'seclabels'
                    ]:
                        data[key] = json.loads(val, encoding='utf-8')
                    else:
                        data[key] = val

            if (u'rid' not in kwargs or kwargs['rid'] == -1) and \
                    u'rolname' not in data:
                return precondition_required(_("Name must be specified."))

            if u'rolvaliduntil' in data:
                # Make date explicit so that it works with every
                # postgres database datestyle format
                try:
                    if data[u'rolvaliduntil'] is not None and \
                            data[u'rolvaliduntil'] != '' and \
                            len(data[u'rolvaliduntil']) > 0:
                        data[u'rolvaliduntil'] = dateutil_parser.parse(
                            data[u'rolvaliduntil']).isoformat()
                except Exception:
                    return precondition_required(_("Date format is invalid."))

            if u'rolconnlimit' in data:
                # If roleconnlimit is empty string then set it to -1
                if data[u'rolconnlimit'] == '':
                    data[u'rolconnlimit'] = -1

                if data[u'rolconnlimit'] is not None:
                    data[u'rolconnlimit'] = int(data[u'rolconnlimit'])
                    if type(data[u'rolconnlimit']) != int or \
                            data[u'rolconnlimit'] < -1:
                        return precondition_required(
                            _("Connection limit must be an integer value "
                              "or equal to -1."))

            if u'rolmembership' in data:
                if u'rid' not in kwargs or kwargs['rid'] == -1:
                    msg = _("""
Role membership information must be passed as an array of JSON objects in the
following format:

rolmembership:[{
    role: [rolename],
    admin: True/False
    },
    ...
]""")
                    if type(data[u'rolmembership']) != list:
                        return precondition_required(msg)

                    data[u'members'] = []
                    data[u'admins'] = []

                    for r in data[u'rolmembership']:
                        if type(r) != dict or u'role' not in r or \
                                u'admin' not in r:
                            return precondition_required(msg)
                        else:
                            if r[u'admin']:
                                data[u'admins'].append(r[u'role'])
                            else:
                                data[u'members'].append(r[u'role'])
                else:
                    msg = _("""
Role membership information must be passed as a string representing an array of
JSON objects in the following format:
rolmembership:{
    'added': [{
        role: [rolename],
        admin: True/False
        },
        ...
        ],
    'deleted': [{
        role: [rolename],
        admin: True/False
        },
        ...
        ],
    'updated': [{
        role: [rolename],
        admin: True/False
        },
        ...
        ]
""")
                    if type(data[u'rolmembership']) != dict:
                        return precondition_required(msg)

                    data[u'members'] = []
                    data[u'admins'] = []
                    data[u'revoked_admins'] = []
                    data[u'revoked'] = []

                    if u'added' in data[u'rolmembership']:
                        roles = (data[u'rolmembership'])[u'added']

                        if type(roles) != list:
                            return precondition_required(msg)

                        for r in roles:
                            if type(r) != dict or \
                                    u'role' not in r or \
                                    u'admin' not in r:
                                return precondition_required(msg)

                            if r[u'admin']:
                                data[u'admins'].append(r[u'role'])
                            else:
                                data[u'members'].append(r[u'role'])

                    if u'deleted' in data[u'rolmembership']:
                        roles = (data[u'rolmembership'])[u'deleted']

                        if type(roles) != list:
                            return precondition_required(msg)

                        for r in roles:
                            if type(r) != dict or u'role' not in r:
                                return precondition_required(msg)

                            data[u'revoked'].append(r[u'role'])

                    if u'changed' in data[u'rolmembership']:
                        roles = (data[u'rolmembership'])[u'changed']

                        if type(roles) != list:
                            return precondition_required(msg)

                        for r in roles:
                            if type(r) != dict or \
                                    u'role' not in r or \
                                    u'admin' not in r:
                                return precondition_required(msg)

                            if not r[u'admin']:
                                data[u'revoked_admins'].append(r[u'role'])
                            else:
                                data[u'admins'].append(r[u'role'])

            if self.manager.version >= 90200 and u'seclabels' in data:
                if u'rid' not in kwargs or kwargs['rid'] == -1:
                    msg = _("""
Security Label must be passed as an array of JSON objects in the following
format:
seclabels:[{
    provider: <provider>,
    label: <label>
    },
    ...
]""")
                    if type(data[u'seclabels']) != list:
                        return precondition_required(msg)

                    for s in data[u'seclabels']:
                        if type(s) != dict or \
                                u'provider' not in s or \
                                u'label' not in s:
                            return precondition_required(msg)
                else:
                    msg = _("""
Security Label must be passed as an array of JSON objects in the following
format:
seclabels:{
    'added': [{
        provider: <provider>,
        label: <label>
        },
        ...
        ],
    'deleted': [{
        provider: <provider>,
        label: <label>
        },
        ...
        ],
    'updated': [{
        provider: <provider>,
        label: <label>
        },
        ...
        ]
""")
                    seclabels = data[u'seclabels']
                    if type(seclabels) != dict:
                        return precondition_required(msg)

                    if u'added' in seclabels:
                        new_seclabels = seclabels[u'added']

                        if type(new_seclabels) != list:
                            return precondition_required(msg)

                        for s in new_seclabels:
                            if type(s) != dict or \
                                    u'provider' not in s or \
                                    u'label' not in s:
                                return precondition_required(msg)

                    if u'deleted' in seclabels:
                        removed_seclabels = seclabels[u'deleted']

                        if type(removed_seclabels) != list:
                            return precondition_required(msg)

                        for s in removed_seclabels:
                            if (type(s) != dict or u'provider' not in s):
                                return precondition_required(msg)

                    if u'changed' in seclabels:
                        changed_seclabels = seclabels[u'deleted']

                        if type(changed_seclabels) != list:
                            return precondition_required(msg)

                        for s in changed_seclabels:
                            if type(s) != dict or \
                                    u'provider' not in s and \
                                    u'label' not in s:
                                return precondition_required(msg)

            if u'variables' in data:
                if u'rid' not in kwargs or kwargs['rid'] == -1:
                    msg = _("""
Configuration parameters/variables must be passed as an array of JSON objects
in the following format in create mode:
variables:[{
    database: <database> or null,
    name: <configuration>,
    value: <value>
    },
    ...
]""")
                    if type(data[u'variables']) != list:
                        return precondition_required(msg)

                    for r in data[u'variables']:
                        if type(r) != dict or u'name' not in r or \
                                u'value' not in r:
                            return precondition_required(msg)
                else:
                    msg = _("""
Configuration parameters/variables must be passed as an array of JSON objects
in the following format in update mode:
rolmembership:{
    'added': [{
        database: <database> or null,
        name: <configuration>,
        value: <value>
        },
        ...
        ],
    'deleted': [{
        database: <database> or null,
        name: <configuration>,
        value: <value>
        },
        ...
        ],
    'updated': [{
        database: <database> or null,
        name: <configuration>,
        value: <value>
        },
        ...
        ]
""")
                    variables = data[u'variables']
                    if type(variables) != dict:
                        return precondition_required(msg)

                    if u'added' in variables:
                        new_vars = variables[u'added']

                        if type(new_vars) != list:
                            return precondition_required(msg)

                        for v in new_vars:
                            if type(v) != dict or u'name' not in v or \
                                    u'value' not in v:
                                return precondition_required(msg)

                    if u'deleted' in variables:
                        delete_vars = variables[u'deleted']

                        if type(delete_vars) != list:
                            return precondition_required(msg)

                        for v in delete_vars:
                            if type(v) != dict or u'name' not in v:
                                return precondition_required(msg)

                    if u'changed' in variables:
                        new_vars = variables[u'changed']

                        if type(new_vars) != list:
                            return precondition_required(msg)

                        for v in new_vars:
                            if type(v) != dict or u'name' not in v or \
                                    u'value' not in v:
                                return precondition_required(msg)

            self.request = data

            return f(self, **kwargs)
Exemplo n.º 46
0
class JobModule(CollectionNodeModule):
    NODE_TYPE = 'pga_job'
    COLLECTION_LABEL = _("pgAgent Jobs")

    def get_nodes(self, gid, sid):
        """
        Generate the collection node
        """
        if self.show_node:
            yield self.generate_browser_collection_node(sid)

    @property
    def script_load(self):
        """
        Load the module script for server, when any of the server-group node is
        initialized.
        """
        return servers.ServerModule.NODE_TYPE

    def BackendSupported(self, manager, **kwargs):
        if hasattr(self, 'show_node') and not self.show_node:
            return False

        conn = manager.connection()

        if manager.server_type == 'gpdb':
            return False

        status, res = conn.execute_scalar("""
SELECT
    has_table_privilege(
      'pgagent.pga_job', 'INSERT, SELECT, UPDATE'
    ) has_priviledge
WHERE EXISTS(
    SELECT has_schema_privilege('pgagent', 'USAGE')
    WHERE EXISTS(
        SELECT cl.oid FROM pg_class cl
        LEFT JOIN pg_namespace ns ON ns.oid=relnamespace
        WHERE relname='pga_job' AND nspname='pgagent'
    )
)
""")
        if status and res:
            status, res = conn.execute_dict("""
SELECT EXISTS(
        SELECT 1 FROM information_schema.columns
        WHERE
            table_schema='pgagent' AND table_name='pga_jobstep' AND
            column_name='jstconnstr'
    ) has_connstr""")

            manager.db_info['pgAgent'] = res['rows'][0]
            return True
        return False

    @property
    def csssnippets(self):
        """
        Returns a snippet of css to include in the page
        """
        snippets = [
            render_template("browser/css/collection.css",
                            node_type=self.node_type,
                            _=_),
            render_template("pga_job/css/pga_job.css",
                            node_type=self.node_type,
                            _=_)
        ]

        for submodule in self.submodules:
            snippets.extend(submodule.csssnippets)

        return snippets

    @property
    def module_use_template_javascript(self):
        """
        Returns whether Jinja2 template is used for generating the javascript
        module.
        """
        return False
Exemplo n.º 47
0
def delete_prefix(id):
    """Delete authors prefix settings."""
    AuthorsPrefixSettings.delete(id)
    return jsonify(msg=_('Success'))
Exemplo n.º 48
0
class RegisterForm(Form):
    username = StringField(
        _("Username"),
        validators=[
            DataRequired(message=_("A Username is required.")), is_username
        ])

    email = StringField(
        _("E-Mail Address"),
        validators=[
            DataRequired(message=_("A E-Mail Address is required.")),
            Email(message=_("Invalid E-Mail Address."))
        ])

    name = StringField(_('name'))
    firstname = StringField(_('firstname'))

    password = PasswordField(_('Password'),
                             validators=[
                                 InputRequired(),
                                 EqualTo('confirm_password',
                                         message=_('Passwords must match.'))
                             ])

    confirm_password = PasswordField(_('Confirm Password'))

    submit = SubmitField(_("Register"))

    def validate_username(self, field):
        user = Utilisateur.select().where(
            Utilisateur.pseudo == field.data).first()
        if user:
            raise ValidationError(_("This Username is already taken."))

    def validate_email(self, field):
        email = Utilisateur.select().where(
            Utilisateur.email == field.data).first()
        if email:
            raise ValidationError(_("This E-Mail Address is already taken."))

    def save(self):

        Utilisateur.create(nom=self.name.data,
                           prenom=self.firstname.data,
                           pseudo=self.username.data,
                           email=self.email.data,
                           password=Utilisateur.hash_password(
                               self.password.data))
Exemplo n.º 49
0
            context['field_groups'][group['name']] = process_fields

        process_summary = generate_process_data(form,
                                                filter_set.qs,
                                                location,
                                                grouped=True,
                                                tags=tags)
        context['process_summary'] = process_summary

    return render_template(template_name, **context)


@route(bp, '/process_summary/<int:form_id>')
@register_menu(bp,
               'main.analyses',
               _('Data Summary'),
               order=4,
               visible_when=lambda: len(get_analysis_menu()) > 0 and
               permissions.view_process_analysis.can())
@register_menu(bp,
               'main.analyses.incidents_analysis',
               _('Incidents Data'),
               dynamic_list_constructor=partial(get_process_analysis_menu,
                                                'INCIDENT'),
               visible_when=lambda: len(get_process_analysis_menu('INCIDENT'))
               > 0 and permissions.view_process_analysis.can())
@register_menu(bp,
               'main.analyses.process_analysis',
               _('Process Data'),
               dynamic_list_constructor=partial(get_process_analysis_menu,
                                                'CHECKLIST'),
Exemplo n.º 50
0
class LoginForm(Form):
    login = StringField('Username')
    password = PasswordField('Password')
    submit = SubmitField(_("Login"))
Exemplo n.º 51
0
            def wrapped(self, **kwargs):
                self.manager = get_driver(
                    PG_DEFAULT_DRIVER).connection_manager(kwargs['sid'])
                self.conn = self.manager.connection()

                driver = get_driver(PG_DEFAULT_DRIVER)
                self.qtIdent = driver.qtIdent

                if not self.conn.connected():
                    return precondition_required(
                        _("Connection to the server has been lost."))

                self.sql_path = 'roles/sql/#{0}#'.format(self.manager.version)

                self.alterKeys = [
                    u'rolcanlogin', u'rolsuper', u'rolcreatedb',
                    u'rolcreaterole', u'rolinherit', u'rolreplication',
                    u'rolconnlimit', u'rolvaliduntil', u'rolpassword'
                ] if self.manager.version >= 90200 else [
                    u'rolcanlogin', u'rolsuper', u'rolcreatedb',
                    u'rolcreaterole', u'rolinherit', u'rolconnlimit',
                    u'rolvaliduntil', u'rolpassword'
                ]

                check_permission = False
                fetch_name = False
                forbidden_msg = None

                if action in ['drop', 'update']:
                    if 'rid' in kwargs:
                        fetch_name = True
                        check_permission = True

                    if action == 'drop':
                        forbidden_msg = _(
                            "The current user does not have permission to drop"
                            " the role.")
                    else:
                        forbidden_msg = _(
                            "The current user does not have permission to "
                            "update the role.")
                elif action == 'create':
                    check_permission = True
                    forbidden_msg = _(
                        "The current user does not have permission to create "
                        "the role.")
                elif action == 'msql' and 'rid' in kwargs:
                    fetch_name = True

                if check_permission:
                    user = self.manager.user_info

                    if not user['is_superuser'] and \
                            not user['can_create_role']:
                        if action != 'update' or 'rid' in kwargs:
                            if kwargs['rid'] != -1:
                                if user['id'] != kwargs['rid']:
                                    return forbidden(forbidden_msg)

                if fetch_name:
                    status, res = self.conn.execute_dict(
                        render_template(self.sql_path + 'permission.sql',
                                        rid=kwargs['rid'],
                                        conn=self.conn))

                    if not status:
                        return internal_server_error(
                            _("Error retrieving the role information.\n{0}").
                            format(res))

                    if len(res['rows']) == 0:
                        return gone(
                            _("Could not find the role on the database "
                              "server."))

                    row = res['rows'][0]

                    self.role = row['rolname']
                    self.rolCanLogin = row['rolcanlogin']
                    self.rolCatUpdate = row['rolcatupdate']
                    self.rolSuper = row['rolsuper']

                return f(self, **kwargs)
Exemplo n.º 52
0
    def sql(self, gid, sid, did, scid, tid, fkid=None):
        """
        This function generates sql to show in the sql pane for the selected
        foreign key.

        Args:
          gid: Server Group ID
          sid: Server ID
          did: Database ID
          scid: Schema ID
          tid: Table ID
          fkid: Foreign key constraint ID

        Returns:

        """

        SQL = render_template("/".join([self.template_path, 'properties.sql']),
                              tid=tid,
                              conn=self.conn,
                              cid=fkid)
        status, res = self.conn.execute_dict(SQL)
        if not status:
            return internal_server_error(errormsg=res)
        if len(res['rows']) == 0:
            return gone(_("""Could not find the foreign key."""))

        data = res['rows'][0]
        data['schema'] = self.schema
        data['table'] = self.table

        sql = render_template("/".join(
            [self.template_path, 'get_constraint_cols.sql']),
                              tid=tid,
                              keys=zip(data['confkey'], data['conkey']),
                              confrelid=data['confrelid'])

        status, res = self.conn.execute_dict(sql)

        if not status:
            return internal_server_error(errormsg=res)

        columns = []
        for row in res['rows']:
            columns.append({
                "local_column": row['conattname'],
                "references": data['confrelid'],
                "referenced": row['confattname']
            })

        data['columns'] = columns

        SQL = render_template("/".join([self.template_path, 'get_parent.sql']),
                              tid=data['columns'][0]['references'])
        status, res = self.conn.execute_2darray(SQL)

        if not status:
            return internal_server_error(errormsg=res)

        data['remote_schema'] = res['rows'][0]['schema']
        data['remote_table'] = res['rows'][0]['table']

        SQL = render_template("/".join([self.template_path, 'create.sql']),
                              data=data)

        sql_header = u"-- Constraint: {0}\n\n-- ".format(data['name'])

        sql_header += render_template("/".join(
            [self.template_path, 'delete.sql']),
                                      data=data)
        sql_header += "\n"

        SQL = sql_header + SQL

        return ajax_response(response=SQL)
Exemplo n.º 53
0
 def validate_username(self, field):
     user = Utilisateur.select().where(
         Utilisateur.pseudo == field.data).first()
     if user:
         raise ValidationError(_("This Username is already taken."))
Exemplo n.º 54
0
        if captcha_data.lower() != captcha.lower():
            self.captcha.errors.append(_('The captcha is error'))
            return False

        return True


class RegisterForm(BaseForm):
    email = StringField(_('Email:'), [DataRequired(), Email()])


class LoginForm(BaseForm):
    remember = BooleanField(_('Remember me'), default=False)


WITHIN = [(0, _('All Topics')), (1, _('One Day')), (2, _('One Week')),
          (3, _('One Month')), (4, _('One Year'))]

ORDERBY = [(0, _('Publish')), (1, _('Author'))]

DESC = [(0, _('Desc')), (1, _('Asc'))]


class SortForm(Form):
    within = SelectField(_('Choice'), coerce=int, choices=WITHIN)
    orderby = SelectField('orderby', coerce=int, choices=ORDERBY)
    desc = SelectField('Up and Down', coerce=int, choices=DESC)


class SearchForm(Form):
    search = StringField(_('search'), validators=[DataRequired()])
Exemplo n.º 55
0
def create_backup_objects_job(sid):
    """
    Args:
        sid: Server ID

        Creates a new job for backup task
        (Backup Database(s)/Schema(s)/Table(s))

    Returns:
        None
    """
    if request.form:
        # Convert ImmutableDict to dict
        data = dict(request.form)
        data = json.loads(data['data'][0], encoding='utf-8')
    else:
        data = json.loads(request.data, encoding='utf-8')

    # Remove ratio from data in case of empty string
    if 'ratio' in data and data['ratio'] == '':
        data.pop("ratio")

    try:
        backup_file = filename_with_file_manager_path(data['file'])
    except Exception as e:
        return bad_request(errormsg=str(e))

    # Fetch the server details like hostname, port, roles etc
    server = Server.query.filter_by(id=sid, user_id=current_user.id).first()

    if server is None:
        return make_json_response(
            success=0, errormsg=_("Could not find the specified server."))

    # To fetch MetaData for the server
    from pgadmin.utils.driver import get_driver
    driver = get_driver(PG_DEFAULT_DRIVER)
    manager = driver.connection_manager(server.id)
    conn = manager.connection()
    connected = conn.connected()

    if not connected:
        return make_json_response(
            success=0, errormsg=_("Please connect to the server first."))

    utility = manager.utility('backup')
    args = [
        '--file', backup_file, '--host', server.host, '--port',
        str(server.port), '--username', server.username, '--no-password'
    ]

    def set_param(key, param):
        if key in data and data[key]:
            args.append(param)

    def set_value(key, param, value):
        if key in data:
            if value:
                if value is True and data[key]:
                    args.append(param)
                    args.append(data[key])
                else:
                    args.append(param)
                    args.append(value)

    set_param('verbose', '--verbose')
    set_param('dqoute', '--quote-all-identifiers')
    set_value('role', '--role', True)
    if data['format'] is not None:
        if data['format'] == 'custom':
            args.extend(['--format=c'])

            set_param('blobs', '--blobs')
            set_value('ratio', '--compress', True)

        elif data['format'] == 'tar':
            args.extend(['--format=t'])

            set_param('blobs', '--blobs')

        elif data['format'] == 'plain':
            args.extend(['--format=p'])
            if 'only_data' in data and data['only_data']:
                args.append('--data-only')
                set_param('disable_trigger', '--disable-triggers')
            else:
                set_param('only_schema', '--schema-only')
                set_param('dns_owner', '--no-owner')
                set_param('include_create_database', '--create')
                set_param('include_drop_database', '--clean')
        elif data['format'] == 'directory':
            args.extend(['--format=d'])

    set_param('pre_data', '--section=pre-data')
    set_param('data', '--section=data')
    set_param('post_data', '--section=post-data')
    set_param('dns_privilege', '--no-privileges')
    set_param('dns_tablespace', '--no-tablespaces')
    set_param('dns_unlogged_tbl_data', '--no-unlogged-table-data')
    set_param('use_insert_commands', '--inserts')
    set_param('use_column_inserts', '--column-inserts')
    set_param('disable_quoting', '--disable-dollar-quoting')
    set_param('with_oids', '--oids')
    set_param('use_set_session_auth', '--use-set-session-authorization')

    set_value('encoding', '--encoding', True)
    set_value('no_of_jobs', '--jobs', True)

    for s in data['schemas']:
        args.extend(['--schema', s])

    for s, t in data['tables']:
        args.extend(['--table', driver.qtIdent(conn, s, t)])

    args.append(data['database'])

    try:
        p = BatchProcess(
            desc=BackupMessage(BACKUP.OBJECT,
                               sid,
                               data['file'].encode('utf-8') if hasattr(
                                   data['file'], 'encode') else data['file'],
                               *args,
                               database=data['database']),
            cmd=utility,
            args=args)
        manager.export_password_env(p.id)
        p.set_env_variables(server)
        p.start()
        jid = p.id
    except Exception as e:
        current_app.logger.exception(e)
        return make_json_response(status=410, success=0, errormsg=str(e))

    # Return response
    return make_json_response(data={'job_id': jid, 'Success': 1})
Exemplo n.º 56
0
def create_backup_job(sid):
    """
    Args:
        sid: Server ID

        Creates a new job for backup task (Backup Server/Globals)

    Returns:
        None
    """
    if request.form:
        # Convert ImmutableDict to dict
        data = dict(request.form)
        data = json.loads(data['data'][0], encoding='utf-8')
    else:
        data = json.loads(request.data, encoding='utf-8')

    try:
        backup_file = filename_with_file_manager_path(data['file'])
    except Exception as e:
        return bad_request(errormsg=str(e))

    # Fetch the server details like hostname, port, roles etc
    server = Server.query.filter_by(id=sid, user_id=current_user.id).first()

    if server is None:
        return make_json_response(
            success=0, errormsg=_("Could not find the specified server."))

    # To fetch MetaData for the server
    from pgadmin.utils.driver import get_driver
    driver = get_driver(PG_DEFAULT_DRIVER)
    manager = driver.connection_manager(server.id)
    conn = manager.connection()
    connected = conn.connected()

    if not connected:
        return make_json_response(
            success=0, errormsg=_("Please connect to the server first."))

    utility = manager.utility('backup_server')

    args = [
        '--file', backup_file, '--host', server.host, '--port',
        str(server.port), '--username', server.username, '--no-password',
        '--database', server.maintenance_db
    ]

    if 'role' in data and data['role']:
        args.append('--role')
        args.append(data['role'])
    if 'verbose' in data and data['verbose']:
        args.append('--verbose')
    if 'dqoute' in data and data['dqoute']:
        args.append('--quote-all-identifiers')
    if data['type'] == 'global':
        args.append('--globals-only')

    try:
        p = BatchProcess(desc=BackupMessage(
            BACKUP.SERVER if data['type'] != 'global' else BACKUP.GLOBALS,
            sid, data['file'].encode('utf-8') if hasattr(
                data['file'], 'encode') else data['file'], *args),
                         cmd=utility,
                         args=args)
        manager.export_password_env(p.id)
        p.set_env_variables(server)
        p.start()
        jid = p.id
    except Exception as e:
        current_app.logger.exception(e)
        return make_json_response(status=410, success=0, errormsg=str(e))
    # Return response
    return make_json_response(data={'job_id': jid, 'success': 1})
Exemplo n.º 57
0
from datetime import datetime

from flask_wtf import Form, RecaptchaField
from wtforms import (StringField, PasswordField, BooleanField, HiddenField,
                     SubmitField)
from wtforms.validators import (DataRequired, InputRequired, Email, EqualTo,
                                regexp, ValidationError)
from flask_babelex import lazy_gettext as _
from models import Utilisateur

USERNAME_RE = r'^[\w.+-]+$'
is_username = regexp(USERNAME_RE,
                     message=_("You can only use letters, numbers or dashes."))


class LoginForm(Form):
    login = StringField('Username')
    password = PasswordField('Password')
    submit = SubmitField(_("Login"))


class RegisterForm(Form):
    username = StringField(
        _("Username"),
        validators=[
            DataRequired(message=_("A Username is required.")), is_username
        ])

    email = StringField(
        _("E-Mail Address"),
        validators=[
Exemplo n.º 58
0
def index():
    return bad_request(errormsg=_("This URL cannot be called directly."))
Exemplo n.º 59
0
class BabelForm(Form):
    timezone = SelectField(_('Timezone:'), coerce=str, choices=timezone)
    locale = SelectField(_('Locale:'), coerce=str, choices=locale)
Exemplo n.º 60
0
class ReplyForm(Form):
    content = TextAreaField(_('Content:'), [DataRequired()])