Esempio n. 1
0
def upload_opml_file(request):
    if request.method == 'POST':
        form = UploadOpmlFileForm(request.POST, request.FILES)
        if form.is_valid():
        	#opml_file = request.FILES['file'].read()
        	#tree = ET.parse(opml_file)
        	#root = tree.getroot()
        	#for outline in root.iter('outline'):
			#	source = Source(user=request.user, xml_url=outline.get('xmlUrl'))
			#	source.save()

			Source.objects.all().delete()
			Group.objects.all().delete()

			group = None
			for event, elem in ET.iterparse(request.FILES['file']):
				#import pdb; pdb.set_trace()
				if elem.tag == 'body':
					outlines = list(elem)

					for outline in outlines:
						if 'xmlUrl' not in outline.attrib:
							group = Group(user=request.user, name=outline.attrib['title']) 
							group.save()

							children = list(outline)
							for child in children:
								source = Source()
								source.text = child.attrib['text']
								source.title = child.attrib['title']
								source.feed_type = child.attrib['type'] 
								source.xml_url = child.attrib['xmlUrl']
								source.html_url = child.attrib['htmlUrl']
								source.save()

								user_source = UserSource(user=request.user, source=source, group=group)
								user_source.save()		
						elif 'xmlUrl' in outline.attrib:
							print outline.attrib
							source = Source()
							source.text = outline.attrib['text']
							source.title = outline.attrib['title']
							source.feed_type = outline.attrib['type'] 
							source.xml_url = outline.attrib['xmlUrl']
							source.html_url = outline.attrib['htmlUrl']
							source.save()	

							user_source = UserSource(user=request.user, source=source)
							user_source.save()		

			return HttpResponseRedirect( reverse('entries') )
    else:
        form = UploadOpmlFileForm()

    return render_to_response('feeds/upload_opml.html', {'form': form}, context_instance=RequestContext(request))