Mercurial > code > home > repos > href
comparison link.py @ 5:f8c4c7ce5f4a
lots of href additions: add/edit, nav fixes
Ignore-this: 863335c4680ac9bcc6a7fc5867638d61
author | Drew Perttula <drewp@bigasterisk.com> |
---|---|
date | Thu, 21 Feb 2013 01:39:01 -0800 |
parents | |
children | e054949143e9 |
comparison
equal
deleted
inserted
replaced
4:409da49c148d | 5:f8c4c7ce5f4a |
---|---|
1 | |
2 class NotFound(ValueError): | |
3 pass | |
4 | |
5 class Links(object): | |
6 def __init__(self, db): | |
7 self.coll = db['links'] | |
8 | |
9 def insertOrUpdate(self, doc): | |
10 self.extract(doc) | |
11 self.coll.update({'href':doc['href']}, doc, upsert=True, safe=True) | |
12 | |
13 def extract(self, doc): | |
14 forUsers = [] | |
15 tags = [] | |
16 for t in doc.get('tag', '').split(' '): | |
17 if t.startswith('for:'): | |
18 forUsers.append(t[4:]) | |
19 else: | |
20 tags.append(t) | |
21 doc['extracted'] = dict(tags=tags, forUsers=forUsers) | |
22 | |
23 def find(self, uri): | |
24 docs = list(self.coll.find({'href': uri})) | |
25 if len(docs) == 0: | |
26 raise NotFound("not found") | |
27 elif len(docs) > 1: | |
28 raise ValueError("%s docs found for href %s" % (len(docs), uri)) | |
29 else: | |
30 return docs[0] | |
31 |