MCPcopy
hub / github.com/pallets/werkzeug / URL

Class URL

examples/couchy/models.py:12–50  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

10
11
12class URL(Document):
13 target = TextField()
14 public = BooleanField()
15 added = DateTimeField(default=datetime.utcnow())
16 shorty_id = TextField(default=None)
17 db = None
18
19 @classmethod
20 def load(cls, id):
21 return super().load(URL.db, id)
22
23 @classmethod
24 def query(cls, code):
25 return URL.db.query(code)
26
27 def store(self):
28 if getattr(self._data, "id", None) is None:
29 new_id = self.shorty_id if self.shorty_id else None
30 while 1:
31 id = new_id if new_id else get_random_uid()
32 try:
33 docid = URL.db.resource.put(content=self._data, path=f"/{id}/")[
34 "id"
35 ]
36 except Exception:
37 continue
38 if docid:
39 break
40 self._data = URL.db.get(docid)
41 else:
42 super().store(URL.db)
43 return self
44
45 @property
46 def short_url(self):
47 return url_for("link", uid=self.id, _external=True)
48
49 def __repr__(self):
50 return f"<URL {self.id!r}>"

Callers 1

newFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected