Created
August 29, 2013 16:02
-
-
Save dfeeney/6380023 to your computer and use it in GitHub Desktop.
Script to import exhibits from viewshare.org. Just change username, and slug to local target values, and source_url to the exhibit to import
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import os | |
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "viewshare.settings") | |
from freemix.exhibit import models | |
import json, urllib2, uuid | |
from django.contrib.auth.models import User | |
from freemix.exhibit.serializers import ExhibitPropertyListSerializer | |
username="dfeeney" | |
slug="something" | |
source_url="http://viewshare.org/data/dfeeney/list-example/" | |
def get_json(url): | |
opener = urllib2.build_opener() | |
r = urllib2.Request(url) | |
data = json.load(opener.open(r)) | |
return data | |
canvas = models.Canvas.objects.all()[0] | |
owner = User.objects.get(username=username) | |
models.PublishedExhibit.objects.filter(slug=slug).delete() | |
exhibit = models.PublishedExhibit.objects.create(slug=slug, | |
title=slug, | |
canvas=canvas, | |
owner=owner, | |
is_draft=False) | |
exhibit.save() | |
properties = get_json(source_url + "properties.json")["properties"] | |
out = ExhibitPropertyListSerializer(exhibit, data=properties) | |
out.save() | |
properties = {} | |
data = get_json(source_url + "data.json") | |
for record in data["items"]: | |
ex_id = record["id"] | |
label = record["label"] | |
for key in record.keys(): | |
if key == "id" or key == "label": | |
continue | |
arr = properties.get(key, []) | |
arr.append({"id": ex_id, "label": label, key: record[key]}) | |
properties[key] = arr | |
for key in properties.keys(): | |
try: | |
prop = exhibit.properties.get(name=key) | |
models.PropertyData(exhibit_property=prop, json=json.dumps(properties[key])).save() | |
except models.ExhibitProperty.DoesNotExist: | |
pass | |
profile = { | |
"facets": {}, | |
"views": { | |
"views": [{ | |
"id": str(uuid.uuid4()), | |
"type": "list", | |
"name": "List", | |
"lens": { | |
"type": "list", | |
"properties": [p.name for p in exhibit.properties.all()] | |
} | |
}], | |
}, | |
"default_lens":{ | |
"type": "list", | |
"properties": [p.name for p in exhibit.properties.all()] | |
}, | |
} | |
exhibit.profile=profile | |
exhibit.save() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment