Esempio n. 1
0
 def archive(self, user_id, conc_id, revoke=False):
     data = self.db.get(mk_key(conc_id))
     stored_user_id = data.get('user_id', None)
     if user_id != stored_user_id:
         raise ForbiddenException(
             'Cannot change status of a concordance belonging to another user'
         )
     pass  # we don't have to do anything here as we archive all the queries by default
Esempio n. 2
0
def ask_corpus_access(ctrl, request):
    ans = {}
    plugin_api = getattr(ctrl, '_plugin_api')
    with plugins.runtime.CORPARCH as ca:
        if plugin_api.user_is_anonymous:
            raise ForbiddenException('Anonymous user cannot send the request')
        status = ca.send_request_email(
            corpus_id=request.form['corpusId'],
            plugin_api=plugin_api,
            custom_message=request.form['customMessage'])
    if status is False:
        ans['error'] = _(
            'Failed to send e-mail. Please try again later or contact system administrator'
        )
    return ans
Esempio n. 3
0
    def archive(self, user_id, conc_id, revoke=False):
        key = self._mk_key(conc_id)
        data = self.open(conc_id)
        if data is None:
            raise UserActionException('Concordance key \'%s\' not found.' % (conc_id,))
        stored_user_id = data.get('user_id', None)
        if user_id != stored_user_id:
            raise ForbiddenException(
                'Cannot change status of a concordance belonging to another user')

        if revoke:
            self._db.set(key, data)
            self._archive_backend.revoke(key)
        else:
            self._archive_backend.archive(data, key)
Esempio n. 4
0
    def archive(self, user_id, conc_id, revoke=False):
        archive_db = self.find_key_db(conc_id)
        if archive_db:
            cursor = archive_db.cursor()
            cursor.execute(
                'SELECT id, data, created integer, num_access, last_access FROM archive WHERE id = ? LIMIT 1',
                (conc_id, ))
            row = cursor.fetchone()
            archived_rec = json.loads(row[1]) if row else None
        else:
            cursor = None
            archived_rec = None

        if revoke:
            if archived_rec:
                cursor.execute('DELETE FROM archive WHERE id = ?', (conc_id, ))
                ans = 1
            else:
                raise NotFoundException(
                    'Concordance {0} not archived'.format(conc_id))
        else:
            cursor = self._latest_archive.cursor(
            )  # writing to the latest archive
            data = self.db.get(mk_key(conc_id))
            if data is None and archived_rec is None:
                raise NotFoundException(
                    'Concordance {0} not found'.format(conc_id))
            elif archived_rec:
                ans = 0
            else:
                stored_user_id = data.get('user_id', None)
                if user_id != stored_user_id:
                    raise ForbiddenException(
                        'Cannot change status of a concordance belonging to another user'
                    )
                curr_time = time.time()
                cursor.execute(
                    'INSERT OR IGNORE INTO archive (id, data, created, num_access) VALUES (?, ?, ?, ?)',
                    (conc_id, json.dumps(data), curr_time, 0))
                archived_rec = data
                ans = 1
        self._latest_archive.commit()
        return ans, archived_rec
Esempio n. 5
0
    def ajax_get_corp_details(self, request):
        corpname = request.args['corpname']
        with plugins.runtime.AUTH as auth:
            _, acc, _ = auth.corpus_access(self.session_get('user'), corpname)
            if not acc:
                raise ForbiddenException(
                    'No access to corpus {0}'.format(corpname))
            corp_conf_info = self.get_corpus_info(corpname)
            corpus = self.cm.get_corpus(request.args['corpname'])
            ans = CorpusDetail(corpname=corpus.get_conf('NAME')
                               if corpus.get_conf('NAME') else corpus.corpname,
                               description=corp_conf_info.description,
                               size=corpus.size,
                               attrlist=[],
                               structlist=[],
                               web_url=corp_conf_info.web
                               if corp_conf_info is not None else '',
                               citation_info=corp_conf_info.citation_info,
                               keywords=[])

        with plugins.runtime.CORPARCH as cp:
            ans.keywords = [
                KeyWord(name=name, color=cp.get_label_color(ident))
                for (ident, name) in corp_conf_info.metadata.keywords
            ]

        try:
            ans.attrlist = [
                AttrStruct(name=item,
                           size=int(corpus.get_attr(item).id_range()))
                for item in corpus.get_posattrs()
            ]
        except RuntimeError as e:
            logging.getLogger(__name__).warning(f'{e}')
            ans.attrlist = ErrorInfo(error=translate('Failed to load'))

        ans.structlist = [
            AttrStruct(name=item, size=int(corpus.get_struct(item).size()))
            for item in corpus.get_structs()
        ]

        return ans
Esempio n. 6
0
    def archive(self, user_id, conc_id, revoke=False):
        cursor = self._archive.cursor()
        cursor.execute(
            'SELECT id, data, created, num_access, last_access FROM kontext_conc_persistence WHERE id = %s LIMIT 1',
            (conc_id, ))
        row = cursor.fetchone()
        archived_rec = json.loads(row['data']) if row is not None else None

        if revoke:
            if archived_rec:
                cursor.execute(
                    'DELETE FROM kontext_conc_persistence WHERE id = %s',
                    (conc_id, ))
                ans = 1
            else:
                raise NotFoundException(
                    'Archive revoke error - concordance {0} not archived'.
                    format(conc_id))
        else:
            cursor = self._archive.cursor()
            data = self.db.get(mk_key(conc_id))
            if data is None and archived_rec is None:
                raise NotFoundException(
                    'Archive store error - concordance {0} not found'.format(
                        conc_id))
            elif archived_rec:
                ans = 0
            else:
                stored_user_id = data.get('user_id', None)
                if user_id != stored_user_id:
                    raise ForbiddenException(
                        'Cannot change status of a concordance belonging to another user'
                    )
                cursor.execute(
                    'INSERT IGNORE INTO kontext_conc_persistence (id, data, created, num_access) '
                    'VALUES (%s, %s, %s, %s)',
                    (conc_id, json.dumps(data), get_iso_datetime(), 0))
                archived_rec = data
                ans = 1
        self._archive.commit()
        return ans, archived_rec