Exemple #1
0
def delete(user_id):
    """Delete spam."""
    # Only admin can access this view
    if not Permission(ActionNeed('admin-access')).can():
        abort(403)

    user = User.query.get(user_id)
    deleteform = DeleteSpamForm()
    communities = Community.query.filter_by(id_user=user.id)

    rs = RecordsSearch(index='records').query(
        Q('query_string', query="owners: {0}".format(user.id)))
    rec_count = rs.count()

    ctx = {
        'user': user,
        'form': deleteform,
        'is_new': False,
        'communities': communities,
        'rec_count': rec_count,
    }

    if deleteform.validate_on_submit():

        if deleteform.remove_all_communities.data:
            for c in communities:
                if not c.deleted_at:
                    if not c.description.startswith('--SPAM--'):
                        c.description = '--SPAM--' + c.description
                    if c.oaiset:
                        db.session.delete(c.oaiset)
                    c.delete()
            db.session.commit()
        if deleteform.deactivate_user.data:
            _datastore.deactivate_user(user)
            db.session.commit()
        # delete_record function commits the session internally
        # for each deleted record
        if deleteform.remove_all_records.data:
            for r in rs.scan():
                delete_record(r.meta.id, 'spam', int(current_user.get_id()))

        flash("Spam removed", category='success')
        return redirect(url_for('.delete', user_id=user.id))
    else:
        records = islice(rs.scan(), 10)
        ctx.update(records=records)
        return render_template('zenodo_spam/delete.html', **ctx)
Exemple #2
0
def delete(user_id):
    """Delete spam."""
    # Only admin can access this view
    if not Permission(ActionNeed('admin-access')).can():
        abort(403)

    user = User.query.get(user_id)
    deleteform = DeleteSpamForm()
    communities = Community.query.filter_by(id_user=user.id)

    rs = RecordsSearch(index='records').query(
        Q('query_string', query="owners: {0}".format(user.id)))
    rec_count = rs.count()

    ctx = {
        'user': user,
        'form': deleteform,
        'is_new': False,
        'communities': communities,
        'rec_count': rec_count,
    }

    if deleteform.validate_on_submit():

        if deleteform.remove_all_communities.data:
            for c in communities:
                if not c.deleted_at:
                    if not c.description.startswith('--SPAM--'):
                        c.description = '--SPAM--' + c.description
                    if c.oaiset:
                        db.session.delete(c.oaiset)
                    c.delete()
            db.session.commit()
        if deleteform.deactivate_user.data:
            _datastore.deactivate_user(user)
            db.session.commit()
        # delete_record function commits the session internally
        # for each deleted record
        if deleteform.remove_all_records.data:
            for r in rs.scan():
                delete_record(r.meta.id, 'spam', int(current_user.get_id()))

        flash("Spam removed", category='success')
        return redirect(url_for('.delete', user_id=user.id))
    else:
        records = islice(rs.scan(), 10)
        ctx.update(records=records)
        return render_template('zenodo_spam/delete.html', **ctx)
Exemple #3
0
def dump_operation_logs(outfile_name, year):
    """Dumps operation log records in a given file.

    :param outfile: JSON operation log output file.
    """
    click.secho('Dumps operation log records:', fg='green')
    index_name = OperationLog.index_name
    if year is not None:
        index_name = f'{index_name}-{year}'
    search = RecordsSearch(index=index_name)

    index_count = 0
    outfile = JsonWriter(outfile_name)
    with click.progressbar(search.scan(), length=search.count()) as bar:
        for oplg in bar:
            outfile.write(str(oplg.to_dict()))
            index_count += 1
    click.echo(f'created {index_count} operation logs.')
Exemple #4
0
 def spam_check(self):
     """Checks deposit metadata for spam content."""
     try:
         if current_app.config.get('ZENODO_SPAM_MODEL_LOCATION'):
             task = check_metadata_for_spam.delay(str(self.id))
             spam_proba = task.get(
                 timeout=current_app.config['ZENODO_SPAM_CHECK_TIMEOUT'])
         else:
             spam_proba = 0
         if spam_proba > current_app.config['ZENODO_SPAM_THRESHOLD']:
             if not Permission(ActionNeed('admin-access')).can():
                 rs = RecordsSearch(index='records').query(
                     Q('query_string',
                       query="owners:{}".format(self['owners'][0])))
                 if not rs.count():
                     current_app.config['ZENODO_SPAM_HANDLING_ACTIONS'](
                         self)
     except HTTPException:
         raise
     except Exception:
         current_app.logger.exception(u'Could not check deposit for spam')