Commits

Dries Desmet committed c9f7bc5

Working state of node-approach

Comments (0)

Files changed (7)

example/settings.py

     'django.contrib.messages',
     'django.contrib.staticfiles',
     'ibpcatalog',
+    'treebeard', #dependancy of ibpcatalog
 )

ibpcatalog/management/commands/doparse.py

 
     def handle(self, *args, **options):
 
-
-        for feed in Feed.objects.all():
-            
-            parse_and_save(feed)
-            
-
+    	print args
+    	for feedname in args:
+    		feed = Feed.objects.get(name=feedname)
+    		parse_and_save(feed)

ibpcatalog/models.py

 from django.db import models
 from ibpcatalog.utils import IbpParser
+from treebeard.al_tree import AL_Node
 
-class Entry(models.Model):
-    """docstring for Entry"""
+class Node(AL_Node):
+    """docstring for Node"""
     
-    feed = models.ForeignKey('Feed', related_name='entries')
-    doc_id = models.CharField(max_length=255)
-    title = models.CharField(max_length=255)
-    productname = models.CharField(max_length=255)
-    companyname = models.CharField(max_length=255)
-    category = models.CharField(max_length=255)
-    calculatedurl = models.URLField()
+    feed = models.ForeignKey('Feed', related_name='nodes')
+    parent = models.ForeignKey('self',
+                           related_name='children_set',
+                           null=True,
+                           db_index=True)
+    name = models.CharField(max_length=255)
+    calculatedurl = models.URLField(null=True)
+    node_order_by = ['name', ]
+
+    def __unicode__(self):
+        return self.name
 
 def parse_and_save(feed):
     """
 
     print "Number of entries: ",len(ibpparser.results)
     for result in ibpparser.results:
-    
-        try:
-            database_entry = Entry.objects.get(feed_id=feed.id, doc_id=result['doc_id'], title=result['title'])
-            print "Entry already in the database."
-                
-        except Entry.DoesNotExist:
-            e = Entry(
-                feed = feed,
-                doc_id = result['doc_id'],
-                title = result['title'],
-                productname = result['productname'],
-                companyname = result['companyname'],
-                category = result['category'],
-                calculatedurl = result['calculatedurl']
-            )
-            e.save()
-            print e,"saved..."
+        
+        companynode, created = Node.objects.get_or_create(name=result['companyname'], feed=feed)
+        languagenode, created = Node.objects.get_or_create(name=result['language'], parent=companynode, feed=feed)
+        productnamenode, created = Node.objects.get_or_create(name=result['productname'], parent=languagenode, feed=feed)
+        titlenode, created = Node.objects.get_or_create(name=result['title'], calculatedurl=result['calculatedurl'], parent=productnamenode, feed=feed)
+        
+        if created:
+            print titlenode, "saved..."
+        else:
+            print titlenode, "already in the database..."
 
 class Feed(models.Model):
     """
     def save(self, *args, **kwargs):
         
         super(Feed, self).save(*args, **kwargs) # Call the "real" save() method.
-        parse_and_save(self)
+        #parse_and_save(self)
         
 
 

ibpcatalog/templates/ibpcatalog/feed_detail.html

 {% extends "base.html" %}
 
 {% block content %}
-{{ source }}
 
-<ul>
-{{ target|unordered_list }}
-</ul>
+{% for item, info in node.get_annotated_list %}
+    {% if info.open %}
+        <ul><li>
+    {% else %}
+        </li><li>
+    {% endif %}
 
-<p>{{ test }}</p>
+    {% if item.calculatedurl %}
+    <a href="{{ item.calculatedurl }}"> {{ item }}</a>
+    {% else %}
+    {{ item }}
+    {% endif %}
 
-<ul>
-	{% for entry in entries %}
-	<li>
-		{{ entry.companyname }}
-	</li>
-	{% endfor %}
-</ul>
+    {% for close in info.close %}
+        </li></ul>
+    {% endfor %}
+{% endfor %}
 
 {% endblock %}

ibpcatalog/utils.py

 
         for entry in self.soup.find_all('entry'):
             self.results.append({
-                'doc_id': entry.id.string.encode('utf-8'),
-                'title': entry.title.string.encode('utf-8'),
-                'productname': entry.find('d:productname').string.encode('utf-8'),
-                'companyname': entry.find('d:companyname').string.encode('utf-8'),
-                'category': entry.find('d:categorydescription').string.encode('utf-8'),
-                'calculatedurl': entry.find('d:calculatedurl').string.encode('utf-8'),
+                'companyname': unicode(entry.find('d:companyname').string),
+                'language': unicode(entry.find('d:language').string),
+                'productname': unicode(entry.find('d:productname').string),
+                'title': unicode(entry.title.string),
+                'calculatedurl': unicode(entry.find('d:calculatedurl').string),
 
             })
 

ibpcatalog/views.py

 from django.views.generic import ListView, DetailView
-from models import Feed, Entry
+from models import Feed
 
 
 
     """
     model = Feed
     context_object_name = 'feed'
-    
-    source=[
-        {
-            'companyname': 'Allianz',
-            'productname': 'product1',
-            'title': 'document1'
-        },
-        {
-            'companyname': 'Allianz',
-            'productname': 'product1',
-            'title': 'document2'
-        }
-    ]
-
-    for entry in source:
-        pass
 
 
     target = [
             'product5',
             ['document9', 'document10'],
         ]
-]    
+    ]
+
+    
         
     def get_context_data(self, **kwargs):
         # Call the base implementation first to get a context
         context = super(FeedDetail, self).get_context_data(**kwargs)
         # Add in extra context
-        # import ipdb;ipdb.set_trace()
-        context['source'] = self.source
-        context['target'] = self.target
+        context['node'] = self.object.nodes.all()[0]
+        
         
         return context
 Django
 beautifulsoup4
 lxml
+django-treebeard==1.61