def import_items(request): raw = request.POST.getlist('item') items = [] o={} o['imported_docs'] = [] o['existing_docs'] = [] for item in raw: items.append(json.loads(item,strict=False)) for doc_import in items: if not ZoteroLink.objects.filter(zotero_url=doc_import['url']): d = Document(creator=request.user, last_updater=request.user, description=doc_import['csl']) d.save() o['imported_docs'].append(d) link = ZoteroLink(zotero_data=json.dumps(doc_import['json']), zotero_url=doc_import['url'], doc_id=d.id) try: doc_import['date']['year'] link.date_information = json.dumps(doc_import['date']) except KeyError: pass link.save() else: existing_link = ZoteroLink.objects.filter(zotero_url=doc_import['url'])[0] o['existing_docs'].append(existing_link.doc) if doc_import['related_object'] == 'topic': related_topic = Topic.objects.get(id=int(doc_import['related_id'])) if TopicAssignment.objects.filter(document=d, topic=related_topic): pass else: new_assignment = TopicAssignment.objects.create(content_object=d, topic=related_topic, creator=request.user) new_assignment.save() return render_to_response( 'success.html', o, context_instance=RequestContext(request))
def import_items(request): if request.session.get('import_complete', False): return HttpResponse('Please only submit items once') item_data = request.POST.getlist('data') item_citations = request.POST.getlist('changed-citation') user = request.user o={} o['created_items'] = [] item_counter = 0 for item_data_string, updated_citation in zip(item_data, item_citations): item_counter += 1 action = request.POST.get('import-action-%s' % item_counter) if action not in ['create', 'update']: continue item_data = json.loads(item_data_string, object_pairs_hook=OrderedDict) if updated_citation: citation = updated_citation else: citation = item_data['citation'] if action == "create": d = Document(creator=user, last_updater=user, description=citation) d.save() elif action == "update": update_id = request.POST.get('item-update-%s' % item_counter) d = Document.objects.get(id=update_id) d.last_updated = datetime.datetime.now() d.last_updater = user d.save() if d.zotero_link(): d.zotero_link().delete() link = ZoteroLink(zotero_data=item_data['json'], zotero_url=item_data['url'], doc_id=d.id) try: item_data['date']['year'] link.date_information = json.dumps(item_data['date']) except KeyError: pass link.save() reltopic = request.GET.get('reltopic', False) if reltopic: related_topic = Topic.objects.get(id=int(reltopic)) new_assignment = TopicAssignment.objects.create( content_object=d, topic=related_topic, creator=user ) new_assignment.save() o['created_items'].append(d) request.session['import_complete'] = True redirect_url = request.GET.get('return_to', '/') return HttpResponseRedirect(redirect_url)
def import_from_jigsaw_root(root, user, project): order = 1 for node in root: id = node.findtext("docID") text = node.findtext("docText") #text.tag = 'div' docs = list(Document.objects.filter(import_id=id)) if len(docs)==0: d = Document(creator=user, last_updater=user, import_id=id, description=id, ordering=order, project=project) d.save() t = Transcript(creator=user, last_updater=user, document=d, content=text) t.save() else: d = docs[0] d.last_updater = user d.description = id d.order = order d.language = gl.guessLanguageName(text) order += 1 d.save() date = node.find("docDate") if date is not None and date.text: try: # pdb.set_trace() dt = datetime.strptime(date.text, "%m/%d/%Y") res = list(Topic.objects.filter(date=dt)) cnt = len(res) if cnt==0: normalized = "Date: %d/%d/%d" % (dt.year, dt.month, dt.day) t=get_or_create_topic(user, normalized, 'EVT', project, dt) t.date = dt #t=Topic(creator=user, last_updater=user, preferred_name=normalized, date=dt, type='EVT',project=project) t.save() else: t=res[0] d.related_topics.create(creator=user, topic=t) except ValueError as e: pass for p in node.findall("concept"): name=p.text t=get_or_create_topic(user, name, 'TAG',project) if t: d.related_topics.create(creator=user, topic=t) else: print "Cannot create topic(%s,type=%s)" % (t,'TAG') for p in node.findall("person"): name=p.text t=get_or_create_topic(user, name, 'PER',project) if t: d.related_topics.create(creator=user, topic=t) else: print "Cannot create topic(%s,type=%s)" % (t,'PER') for p in node.findall("location"): name=p.text t=get_or_create_topic(user, name, 'PLA',project) if t: d.related_topics.create(creator=user, topic=t) else: print "Cannot create topic(%s,type=%s)" % (t,'PLA')