Mercurial > code > home > repos > href
annotate link.py @ 30:e86642cf7393
style and requirements.txt cleanup
author | drewp@bigasterisk.com |
---|---|
date | Sun, 12 Jul 2020 13:33:54 -0700 |
parents | 7c82ffbca5d0 |
children | f3a15a724483 |
rev | line source |
---|---|
30 | 1 import urllib.parse |
2 import urllib.request | |
3 import urllib.parse | |
4 import urllib.error | |
10
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
5 from dateutil.tz import tzlocal |
5
f8c4c7ce5f4a
lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
6 |
30 | 7 |
5
f8c4c7ce5f4a
lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
8 class NotFound(ValueError): |
f8c4c7ce5f4a
lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
9 pass |
f8c4c7ce5f4a
lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
10 |
30 | 11 |
5
f8c4c7ce5f4a
lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
12 class Links(object): |
f8c4c7ce5f4a
lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
13 def __init__(self, db): |
f8c4c7ce5f4a
lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
14 self.coll = db['links'] |
30 | 15 |
5
f8c4c7ce5f4a
lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
16 def insertOrUpdate(self, doc): |
10
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
17 if not doc['href']: |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
18 raise ValueError("no link") |
5
f8c4c7ce5f4a
lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
19 self.extract(doc) |
30 | 20 self.coll.update({'href': doc['href']}, doc, upsert=True, safe=True) |
5
f8c4c7ce5f4a
lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
21 |
f8c4c7ce5f4a
lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
22 def extract(self, doc): |
f8c4c7ce5f4a
lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
23 forUsers = [] |
f8c4c7ce5f4a
lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
24 tags = [] |
f8c4c7ce5f4a
lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
25 for t in doc.get('tag', '').split(' '): |
f8c4c7ce5f4a
lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
26 if t.startswith('for:'): |
f8c4c7ce5f4a
lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
27 forUsers.append(t[4:]) |
f8c4c7ce5f4a
lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
28 else: |
f8c4c7ce5f4a
lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
29 tags.append(t) |
f8c4c7ce5f4a
lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
30 doc['extracted'] = dict(tags=tags, forUsers=forUsers) |
f8c4c7ce5f4a
lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
31 |
16
d44d3e4d415b
don't show other user's tags as previously-saved tags to the current user
Drew Perttula <drewp@bigasterisk.com>
parents:
10
diff
changeset
|
32 def find(self, uri, user): |
30 | 33 docs = list(self.coll.find({'href': uri, 'user': user})) |
5
f8c4c7ce5f4a
lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
34 if len(docs) == 0: |
f8c4c7ce5f4a
lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
35 raise NotFound("not found") |
f8c4c7ce5f4a
lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
36 elif len(docs) > 1: |
f8c4c7ce5f4a
lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
37 raise ValueError("%s docs found for href %s" % (len(docs), uri)) |
f8c4c7ce5f4a
lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
38 else: |
f8c4c7ce5f4a
lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
39 return docs[0] |
21
8008ec2fd763
fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents:
16
diff
changeset
|
40 |
8008ec2fd763
fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents:
16
diff
changeset
|
41 def filter(self, user, startTime): |
30 | 42 return self.coll.find({'user': user, 't': {'$gte': startTime}}) |
43 | |
10
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
44 def forDisplay(self, doc): |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
45 """return a mustache-ready dict for this db doc""" |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
46 out = doc.copy() |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
47 del out['_id'] |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
48 out['t'] = out['t'].astimezone(tzlocal()).isoformat() |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
49 if not out['description'].strip(): |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
50 out['displayDescription'] = out['href'] |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
51 else: |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
52 out['displayDescription'] = out['description'] |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
53 |
30 | 54 out['tagWords'] = [{'word': w} for w in out['tag'].split(None)] |
28 | 55 out['domain'] = urllib.parse.urlparse(out['href']).netloc |
30 | 56 out['editLink'] = 'addLink?' + urllib.parse.urlencode( |
57 [('url', out['href'])]) | |
58 out['shareWith'] = [{'label': uri} for uri in doc.get('shareWith', [])] | |
10
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
59 return out |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
60 |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
61 def fromPostdata(self, data, user, t): |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
62 if not user or not data.href: |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
63 raise ValueError("incomplete") |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
64 return dict( |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
65 user=user, |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
66 description=data.description, |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
67 extended=data.extended, |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
68 href=data.href, |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
69 private=data.private, |
28 | 70 shareWith=[_f for _f in data.shareWith.split(',') if _f], |
10
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
71 tag=data.tag, |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
72 t=t, |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
73 ) |
23 | 74 |
75 def asDeliciousAddParams(self): | |
30 | 76 return dict( |
77 url=self['href'], | |
78 description=self['description'], | |
79 extended=self['extended'], | |
80 tags=','.join(self['tag'].split(' ')), | |
81 dt=self['t'], | |
82 replace='yes', | |
23 | 83 ) |