from flask import current_app

class Article(dict):
    """
    An article in papertalk.
    Title (str)
    Authors (list of str)
    Year (dt)
    Num citations (num)
    Full Citation (text)
    url (str)
    """


    def __init__(self):
        attrs = {'_id': None,
                 'title':        None,
                'authors':       [],
                'source_urls':   [],
                'reactions':     [],
                'direct_url':    None,
                'num_citations': 0,
                'url_citations': None,
                'url_versions':  None,
                'num_versions':  0,
                'outlet':        None,
                'year':          None,
                'url':           None,
                'doi':           None}
        self.update(attrs)


    def save(self):
        db = current_app.mongo.db
        article = db.articles.find_one({"title" : self["title"], "year"  : self["year"]})
        if article:
            db.articles.save(article)
        else:
            del self["_id"]
            self["_id"] = db.articles.insert(self)

        return self["_id"]

    def as_txt(self):
        # Get items sorted in specified order:
        return '\n'.join(["%s: %s" % (item[0], item[1]) for item in self.iteritems()])

    @classmethod
    def lookup(cls, query):
        """
        lookup an article in our db
        """
        return current_app.mongo.db.articles.find_one_or_404(query)

    def disambiguate(self, other):
        """
        returns a new article with the two articles merged
        """
        pass
@@ -802,24 +802,25 @@
 ef save(self
+,
 ):%0A        d
@@ -856,39 +856,16 @@
     
-article = db.articles.find_one(
+query = 
 %7B%22ti
@@ -892,18 +892,16 @@
 , %22year%22
-  
 : self%5B%22
@@ -907,17 +907,16 @@
 %22year%22%5D%7D
-)
 %0A       
@@ -920,56 +920,59 @@
     
-if 
 article
-:%0A            db.articles.save(article
+ = db.articles.find_one(query, as_class=dict
 )%0A  
@@ -981,11 +981,17 @@
     
-els
+if articl
 e:%0A 
@@ -1005,259 +1005,151 @@
     
-del self%5B%22_id%22%5D%0A            self%5B%22_id%22%5D = db.articles.insert(self)%0A%0A        return self%5B%22_id%22%5D%0A%0A    def as_txt(self):%0A        # Get items sorted in specified order:%0A        return '%5Cn'.join(%5B%22%25s: %25s%22 %25 (item%5B0%5D, item%5B1%5D) for item in self.iteritems()%5D)
+self%5B%22_id%22%5D = db.articles.save(article)%0A        else:%0A            self%5B%22_id%22%5D = db.articles.insert(self, safe=True)%0A%0A        return self%5B%22_id%22%5D
 %0A%0A  
@@ -1188,16 +1188,28 @@
 s, query
+, mult=False
 ):%0A     
@@ -1271,22 +1271,20 @@
         
-return
+db =
  current
@@ -1296,16 +1296,112 @@
 mongo.db
+%0A        if mult:%0A            return db.articles.find(query)%0A        else:%0A            return db
 .article

fix up article model to have lookup multiple option