Ejemplo n.º 1
0
    def reorder(self, id, **params):
        if getparam('cancel') == '1':
            redirect(url("coll-view", id=id))
        coll = get_or_404(Collection, id)

        context = {}
        if cherrypy.request.method == "POST":
            # FIXME - check csrf token

            order, ordering = coll_order_from_request()

            if getparam('submit') == '1':
                # save order
                coll.autoorder.ordering = ordering
                Collection.objects.set(coll)
                Collection.objects.flush()
                redirect(url("coll-view", id=id))
        else:
            order = []
            for field, dir in coll.autoorder.ordering:
                order.append((len(order), field, dir))

        if not order:
            order.append((0, '', ''))

        context['coll'] = coll
        context['collorder'] = order
        return render("coll-reorder.html", context)
Ejemplo n.º 2
0
def tmpl_from_request(id, params):
    context = {}

    params, remcoll = handle_create_collection(params)
    validator = RecordValidator(params, remcoll)
    if validator.invalid_collections:
        return dict(invalid_collections=validator.invalid_collections)

    newid = params.get('newid')
    ok = True
    try:
        # FIXME - check csrf token
        record = validator.validate()
        tmpl = Template(newid, None, record)
    except ValidationError, e:
        context['error'] = e.errormsg
        ok = False
        orig_tmpl = get_or_404(Template, id)
        record = dict(inner_xml=params.get('inner_xml', None),
                      collections=validator.collections,
                      fieldnums=orig_tmpl.record.fieldnums,
                      walkfields=orig_tmpl.record.walkfields,
                     )
        tmpl = dict(record=record,
                    id=newid,
                   )
Ejemplo n.º 3
0
    def delete(self, id, **params):
        coll = get_or_404(Collection, id)
        if cherrypy.request.method == "POST":
            # FIXME - check csrf token
            if id:
                if params.get('delete_conf', '') == '':
                    redirect(url("colls-list"))

                coll = get_or_404(Collection, id)

                # Remove the collection's parents
                coll.set_parents(())
                Collection.objects.set(coll)

                # Remove the collection from its childrens parent lists.
                child_colls = coll.children
                for child in child_colls:
                    cparents = set(child.parents)
                    cparents.remove(id)
                    child.set_parents(cparents)
                    Collection.objects.set(child)

                # Iterate through the records in the collection, and remove the
                # collection from them.
                SearchCollection.checkpoint().wait()
                for record in SearchCollection.doc_type('record') \
                   .field.coll.is_in(id)[:]:
                    try:
                        record = record.object
                    except KeyError:
                        # Record is already gone - can't update it.
                        # (This shouldn't happen, but do this check for
                        # robustness.)
                        continue
                    record.collections = filter(lambda x: x != id, record.collections)
                    Record.objects.set(record)

                Collection.objects.remove(id)
                Record.objects.flush()
                Collection.objects.flush()
                gonext()
                redirect(url("colls-list", id=coll.id))
        elif cherrypy.request.method == "GET":
            context = dict(coll=coll)
            return render("coll-delete-confirm.html", context)

        raise cherrypy.HTTPError(404)
Ejemplo n.º 4
0
 def copy(self, id, **params):
     record = copy.deepcopy(get_or_404(Record, id))
     del record.id
     Record.objects.set(record)
     Record.objects.flush()
     redirect(url("record-edit", id=record.id,
                  tmplid=params.get('tmplid', ''),
                  copyof=record.title))
Ejemplo n.º 5
0
    def _check_delete(self, id, page):
        params = cherrypy.request.params
        if params.get('delete', '') != '':
            record = get_or_404(Record, id)
            context = dict(record=record, page=page)
            return render("record-delete-confirm.html", context)

        if cherrypy.request.method == "POST":
            if params.get('delete_conf', '') != '':
                record = get_or_404(Record, id)
                #print "Old collections", record.collections
                record.collections = ()
                # Set the record, to remove the collections 
                Record.objects.set(record)
                Record.objects.remove(id)
                Record.objects.flush()
                Collection.objects.flush()
                gonext()
                redirect(url("search", act='search'))
Ejemplo n.º 6
0
 def gallery(self, id, show=None, offset=None):
     record = get_or_404(Record, id)
     images = record.media('image')
     if offset is None:
         offset = 0
         if show is not None:
             for num, image in enumerate(images):
                 if image.src == show:
                     offset = num
                     break
     else:
         offset = int(offset)
     context = dict(record=record, images=images, offset=offset)
     return render("record-gallery.html", context)
Ejemplo n.º 7
0
    def _check_delete(self, id, page):
        params = cherrypy.request.params
        if params.get('delete', '') != '':
            tmpl = get_or_404(Template, id)
            context = dict(tmpl=tmpl, page=page)
            return render("tmpl-delete-confirm.html", context)

        if cherrypy.request.method == "POST":
            if params.get('delete_conf', '') != '':
                try:
                    Template.objects.remove(id)
                    Template.objects.flush()
                except KeyError:
                    raise cherrypy.HTTPError(404)
                gonext()
                redirect(url("tmpls-list"))
Ejemplo n.º 8
0
    def create(self, **params):
        context = {}

        if cherrypy.request.method == "POST":
            context.update(record_from_request(None))
            if context.get('invalid_collections'):
                return missing_collection(params, context)

        if cherrypy.request.method == "GET":
            tmplid = params.get('tmplid', '')
            if tmplid == '':
                context['record'] = Record()
            else:
                context['record'] = get_or_404(Template, tmplid)

        return render("record-new.html", context)
Ejemplo n.º 9
0
    def edit(self, id, **params):
        context = {}

        ret = self._check_delete(id, 'tmpl-edit')
        if ret is not None:
            return ret

        if cherrypy.request.method == "POST":
            context.update(tmpl_from_request(id, params))
            if context.get('invalid_collections'):
                return missing_collection(params, context)
        elif cherrypy.request.method == "GET":
            context['tmpl'] = get_or_404(Template, id)
        else:
            assert False

        return render("tmpl-edit.html", context)
Ejemplo n.º 10
0
    def reparent(self, id, **params):
        if getparam('cancel') == '1':
            redirect(url("coll-view", id=id))
        coll = get_or_404(Collection, id)

        if cherrypy.request.method == "POST":
            # FIXME - check csrf token

            title = getparam('title')
            parents = coll_parents_from_request()

            if getparam('submit') == '1':
                # Save parents
                changed = coll.set_parents(filter(lambda x: x != '', parents))
                if coll.title != title:
                    coll.title = title
                Collection.objects.set(coll)
                Collection.objects.flush()
                Record.objects.flush()
                redirect(url("coll-view", id=id))
        else:
            title = coll.title
            parents = []
            for parent in coll.parents:
                parents.append(parent)
        if not parents:
            parents.append('')

        not_allowed = set(coll.ancestors) - set(coll.parents)
        not_allowed.add(id)

        allowable_parents = set(
            c.id for c in Collection.objects
            if c.id != id and id not in c.ancestors
        )

        context = dict(
            title = title,
            coll = coll,
            parents = tuple(enumerate(parents)),
            allowable_parents = sorted(allowable_parents),
            collections = Collection.objects,
        )

        return render("coll-reparent.html", context)
Ejemplo n.º 11
0
    def _get_export_records(self, stash, record_id, issearch, coll, incsubs):
        """Returns a search queryset, and a description of the export."""

        # record_id is set to export a single record.
        if record_id is not None:
            search = SearchCollection.doc_type('record').field.id == record_id
            return search, "record with id %s" % record_id, None

        # if issearch is set, export the search results.
        if issearch is not None:
            params = SearchParams(stash)
            search_obj = Search(params)
            if not search_obj.validate():
                redirect(url("search", **stash))
            desc = "%d records found by search.\n" % search_obj.match_count
            return search_obj.query, desc + search_obj.query_desc, None

        # FIXME - allow selected records here.

        # If we've got no collection set, but have a lock, set the collection
        # to the locked one.
        lockedto = get_lockto_coll()
        if lockedto is not None and coll is None:
            coll = lockedto.id

        # coll is set to export a whole collection.
        if coll is not None:
            coll_obj = get_or_404(Collection, coll)
            search = coll_obj.items_from_search(incsubs=incsubs)

            desc = "all %d records from collection %s" % (
                len(search),
                coll_obj.title
            )
            if incsubs:
                desc += " (including subcollections)"
            return search, desc, coll_obj

        # If none of the above are set, export everything.
        return SearchCollection.doc_type('record').all()[:], "all records", None
Ejemplo n.º 12
0
def record_from_request(id):
    params = cherrypy.request.params
    context = {}

    params, remcoll = handle_create_collection(params)
    validator = RecordValidator(params, remcoll)
    if validator.invalid_collections:
        return dict(invalid_collections=validator.invalid_collections)

    ok = True
    try:
        # FIXME - check csrf token
        record = validator.validate()
    except ValidationError, e:
        context['error'] = e.errormsg
        ok = False
        orig_record = get_or_404(Record, id)
        record = dict(inner_xml=params.get('inner_xml', None),
                      collections=validator.collections,
                      fieldnums=orig_record.fieldnums,
                      walkfields=orig_record.walkfields,
                      id=id,
                     )
Ejemplo n.º 13
0
    def view(self, id, **params):
        coll = get_or_404(Collection, id)

        def types_in_collection(collid):
            q = SearchCollection.field.coll.is_or_is_descendant(collid)
            q = q.calc_occur('!', '').check_at_least(-1)
            q = q.calc_facet_count('date', result_limit=2)
            q = q[:0]
            return dict(q.info[0]['counts']), q.info[1]

        def items_of_type(type, cons, order, incsubs, reverse, hpp):
            q = coll.items_from_search(type=type, incsubs=incsubs)

            if order != 'collection':
                q = q.order_by_multiple([])
                order_key = order
                if order.startswith('+'):
                    order_key, asc = order[1:], True
                elif order.startswith('-'):
                    order_key, asc = order[1:], False
                else:
                    order_key, asc = order, False
                q = q.order_by(order_key, asc)

            max_hpp = int(math.ceil(hpp * 1.25))
            items = q[startrank:startrank + max_hpp]
            if len(items) >= max_hpp:
                items = items[:hpp]
            return dict(coll_size=q.matches_estimated, items=items)

        stash = {}
        order = getparam('order', 'collection', stash, params)
        incsubs = int(getparam('incsubs', '1', stash, params))
        reverse = int(getparam('reverse', '0', stash, params))
        startrank = int(getparam('startrank', '0', stash, params))
        hpp = int(getparam('hpp', '100', stash, params))
        tab = getparam('tab', 'records', stash, params)

        tabs = []

        # FIXME - do a faceted search to check which type tabs to show.
        types_available, dates_available = types_in_collection(id)

        for page in coll.leading_tabs():
            tabs.append("x" + page.id)

        if coll.parents:
            tabs.append('parents')
        #if 'r' in types_available:
        tabs.append("records")
        if coll.children:
            tabs.append('children')
        if 'image' in types_available:
            tabs.append("images")
        if 'video' in types_available:
            tabs.append("video")
        if 'audio' in types_available:
            tabs.append("audio")
        if 'media' in types_available:
            tabs.append("media")
        if dates_available['values_seen'] >= 1:
            tabs.append("calendar")

        context = dict(
            stash = stash,
            tab = tab,
            tabs = tabs,
            startrank = startrank,
            hpp = hpp,
            coll = coll,
            order = order,
            incsubs = incsubs,
            reverse = reverse,
            showfull = int(getparam('showfull', '1', stash, params)),
        )

        if tab == 'records':
            context.update(items_of_type('record', lambda x: x, order, incsubs, reverse, hpp))
        if tab == 'images':
            context.update(items_of_type('image', lambda x: x, order, incsubs, reverse, hpp))
        if tab == 'video':
            context.update(items_of_type('video', lambda x: x, order, incsubs, reverse, hpp))
        if tab == 'audio':
            context.update(items_of_type('audio', lambda x: x, order, incsubs, reverse, hpp))
        if tab == 'media':
            context.update(items_of_type('media', lambda x: x, order, incsubs, reverse, hpp))
        if tab == 'calendar':
            calstyle = getparam('calstyle', 'year', stash, params)
            if calstyle not in ('year', 'month', 'day'):
                calstyle = 'year'
            datefield = getparam('datefield', 'date', stash, params)

            startdate = getparam('startdate', '', stash, params)
            year = getparam('year', None, None, params)
            month = getparam('month', None, None, params)
            day = getparam('day', None, None, params)

            incsubs = True # FIXME - no UI to set at present
            context.update(calendar_items(id, incsubs, calstyle, startdate,
                                          year, month, day,
                                          datefield))
            stash['startdate'] = [context['startdate']]

        if tab == 'children':
            context['direct_records'] = items_of_type('record', lambda x: x, 'collection', 0, False, 10)

        if tab.startswith('x'):
            context['currpage'] = coll.page(tab[1:])

        if params.get('lockto', None) == 'set':
            set_lockto_collid(id)

        elif params.get('lockto', None) == 'unset':
            unset_lockto_collid()

        return render("coll-view.html", context)
Ejemplo n.º 14
0
 def view(self, id, **params):
     tmpl = get_or_404(Template, id)
     context = dict(tmpl=tmpl)
     add_search_to_context(context)
     return render("tmpl-view.html", context)
Ejemplo n.º 15
0
 def unframed(self, id, tmplid=None):
     record = get_or_404(Record, id)
     context = dict(record=record, tmplid=tmplid)
     return render("record-view-unframed.html", context)
Ejemplo n.º 16
0
 def view(self, id, tmplid=None, **params):
     record = get_or_404(Record, id)
     context = dict(record=record, tmplid=tmplid)
     add_search_to_context(context)
     return render("record-view.html", context)