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)
def get_expired_embargos(cls): """Get records for which the embargo period have expired.""" endpoint = current_app.config['RECORDS_REST_ENDPOINTS']['recid'] s = RecordsSearch( using=current_search_client, index=endpoint['search_index']).query( 'query_string', query='access_right:{0} AND embargo_date:{{* TO {1}}}'.format( cls.EMBARGOED, # Uses timestamp instead of date on purpose. datetime.utcnow().isoformat()), allow_leading_wildcard=False).fields([]) return [hit.meta.id for hit in s.scan()]
def get_expired_embargos(cls): """Get records for which the embargo period have expired.""" endpoint = current_app.config['RECORDS_REST_ENDPOINTS']['recid'] s = RecordsSearch( using=current_search_client, index=endpoint['search_index'] ).query( 'query_string', query='access_right:{0} AND embargo_date:{{* TO {1}}}'.format( cls.EMBARGOED, # Uses timestamp instead of date on purpose. datetime.utcnow().isoformat() ), allow_leading_wildcard=False ).fields([]) return [hit.meta.id for hit in s.scan()]
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.')