Esempio n. 1
0
def import_file(dbsession, text):
    lines = text.split("\n")

    tags_label = "TAGS: "
    separator = 60*"-"

    from time import time
    timestamp = time()

    idx = 0
    while idx < len(lines):
        assert lines[idx].startswith(tags_label)
        tags = lines[idx][len(tags_label):]
        idx += 1

        body = []

        while idx < len(lines) and not lines[idx].startswith(separator):
            body.append(lines[idx])
            idx += 1

        idx += 1  # skip separator

        store_itemversion(dbsession,
                contents="\n".join(body), tags=tags, timestamp=timestamp)

    dbsession.commit()
Esempio n. 2
0
    def http_store_item(self, request):
        data = json.loads(request.POST["json"])

        current_query = data.pop("current_query", None)
        deleting = data["contents"] is None

        # if view ordering is present for current query,
        # make sure this entry shows up last
        if not deleting:
            # if we're not deleting
            from synoptic.datamodel import ViewOrderingHandler
            from synoptic.query import parse_query
            voh = ViewOrderingHandler(
                    request.dbsession, request.datamodel,
                    parse_query(current_query))
            if voh.has_ordering():
                voh.load()
                if data["id"] in voh:
                    # we already have a spot in the ordering, don't bother
                    voh = None
            else:
                voh = None
        else:
            voh = None

        if not deleting:
            set_with_parsed_datetime(data, "start_date",
                    use_utc=data["all_day"])
            set_with_parsed_datetime(data, "end_date", "start_date",
                    use_utc=data["all_day"])
            set_with_parsed_datetime(data, "hide_until", "start_date")
            set_with_parsed_datetime(data, "highlight_at", "start_date")

        if not deleting:
            from synoptic.datamodel import is_valid_tag
            for tag in data["tags"]:
                if not is_valid_tag(tag):
                    raise RuntimeError("invalid tag")

        itemversion = store_itemversion(request.dbsession,
                **data)

        request.dbsession.commit()  # fills in the item_id

        if voh is not None:
            voh.insert(len(voh), itemversion.item_id)
            voh.save()

        # send response
        return request.respond(
                json.dumps(self.item_to_json(itemversion)),
                mimetype="text/plain")