Mercurial > code > home > repos > href
annotate link.py @ 42:530650b3bc40 default tip
something changed in pom to break pyjwt. switched to jwskate
author | drewp@bigasterisk.com |
---|---|
date | Wed, 14 Dec 2022 22:07:19 -0800 |
parents | 293a694304b8 |
children |
rev | line source |
---|---|
41 | 1 import urllib.error |
30 | 2 import urllib.parse |
3 import urllib.request | |
41 | 4 |
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): |
41 | 13 |
5
f8c4c7ce5f4a
lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
14 def __init__(self, db): |
f8c4c7ce5f4a
lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
15 self.coll = db['links'] |
30 | 16 |
5
f8c4c7ce5f4a
lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
17 def insertOrUpdate(self, doc): |
10
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
18 if not doc['href']: |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
19 raise ValueError("no link") |
5
f8c4c7ce5f4a
lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
20 self.extract(doc) |
38 | 21 self.coll.update({'href': doc['href']}, doc, upsert=True) |
5
f8c4c7ce5f4a
lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
22 |
f8c4c7ce5f4a
lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
23 def extract(self, doc): |
f8c4c7ce5f4a
lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
24 forUsers = [] |
f8c4c7ce5f4a
lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
25 tags = [] |
f8c4c7ce5f4a
lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
26 for t in doc.get('tag', '').split(' '): |
f8c4c7ce5f4a
lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
27 if t.startswith('for:'): |
f8c4c7ce5f4a
lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
28 forUsers.append(t[4:]) |
f8c4c7ce5f4a
lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
29 else: |
f8c4c7ce5f4a
lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
30 tags.append(t) |
f8c4c7ce5f4a
lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
31 doc['extracted'] = dict(tags=tags, forUsers=forUsers) |
f8c4c7ce5f4a
lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
32 |
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
|
33 def find(self, uri, user): |
30 | 34 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
|
35 if len(docs) == 0: |
f8c4c7ce5f4a
lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
36 raise NotFound("not found") |
f8c4c7ce5f4a
lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
37 elif len(docs) > 1: |
f8c4c7ce5f4a
lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
38 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
|
39 else: |
f8c4c7ce5f4a
lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
40 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
|
41 |
8008ec2fd763
fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents:
16
diff
changeset
|
42 def filter(self, user, startTime): |
30 | 43 return self.coll.find({'user': user, 't': {'$gte': startTime}}) |
44 | |
10
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
45 def forDisplay(self, doc): |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
46 """return a mustache-ready dict for this db doc""" |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
47 out = doc.copy() |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
48 del out['_id'] |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
49 out['t'] = out['t'].astimezone(tzlocal()).isoformat() |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
50 if not out['description'].strip(): |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
51 out['displayDescription'] = out['href'] |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
52 else: |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
53 out['displayDescription'] = out['description'] |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
54 |
30 | 55 out['tagWords'] = [{'word': w} for w in out['tag'].split(None)] |
28 | 56 out['domain'] = urllib.parse.urlparse(out['href']).netloc |
41 | 57 out['editLink'] = 'addLink?' + urllib.parse.urlencode([('url', out['href'])]) |
30 | 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 ) |