Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tag type creation and crud functions #7928

Merged
merged 10 commits into from
Jul 3, 2023
38 changes: 38 additions & 0 deletions openlibrary/core/infobase_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,42 @@ create table work_str (
create index work_str_idx ON work_str(key_id, value);
create index work_str_thing_id_idx ON work_str(thing_id);

create table tag_boolean (
thing_id int references thing,
key_id int references property,
value boolean,
ordering int default NULL
);
create index tag_boolean_idx ON tag_boolean(key_id, value);
create index tag_boolean_thing_id_idx ON tag_boolean(thing_id);

create table tag_int (
thing_id int references thing,
key_id int references property,
value int,
ordering int default NULL
);
create index tag_int_idx ON tag_int(key_id, value);
create index tag_int_thing_id_idx ON tag_int(thing_id);

create table tag_ref (
thing_id int references thing,
key_id int references property,
value int references thing,
ordering int default NULL
);
create index tag_ref_idx ON tag_ref(key_id, value);
create index tag_ref_thing_id_idx ON tag_ref(thing_id);

create table tag_str (
thing_id int references thing,
key_id int references property,
value varchar(2048),
ordering int default NULL
);
create index tag_str_idx ON tag_str(key_id, value);
create index tag_str_thing_id_idx ON tag_str(thing_id);

-- sequences --
CREATE SEQUENCE type_edition_seq;

Expand All @@ -410,6 +446,8 @@ CREATE SEQUENCE type_work_seq;

CREATE SEQUENCE type_publisher_seq;

CREATE SEQUENCE type_tag_seq;

create table store (
id serial primary key,
key text unique,
Expand Down
51 changes: 51 additions & 0 deletions openlibrary/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from datetime import datetime, timedelta
import logging
import web
import json
import requests
from typing import Any
from collections import defaultdict
Expand Down Expand Up @@ -1116,6 +1117,54 @@ def get_default_cover(self):
return Image(web.ctx.site, "b", cover_id)


class Tag(Thing):
"""Class to represent /type/tag objects in OL."""

@classmethod
JaydenTeoh marked this conversation as resolved.
Show resolved Hide resolved
def find(cls, tag_name, tag_type):
"""Returns a Tag object for a given tag name and tag type."""
q = {'type': '/type/tag', 'name': tag_name, 'tag_type': tag_type}
match = list(web.ctx.site.things(q))
return match[0] if match else None

@classmethod
def create(
cls,
tag_name,
tag_description,
tag_type,
tag_plugins,
ip='127.0.0.1',
comment='New Tag',
):
"""Creates a new Tag object."""
current_user = web.ctx.site.get_user()
patron = current_user and current_user.username or 'ImportBot'
key = web.ctx.site.new_key('/type/tag')
from openlibrary.accounts import RunAs

with RunAs(patron):
web.ctx.ip = web.ctx.ip or ip
web.ctx.site.save(
{
'key': key,
'name': tag_name,
'tag_description': tag_description,
'tag_type': tag_type,
'tag_plugins': json.loads(tag_plugins or "[]"),
'type': dict(key='/type/tag'),
},
comment=comment,
)
return key

def url(self, suffix="", **params):
return self.get_url(suffix, **params)

def get_url_suffix(self):
return self.name or "unnamed"


@dataclass
class LoggedBooksData:
"""
Expand Down Expand Up @@ -1164,6 +1213,7 @@ def register_models():
client.register_thing_class('/type/user', User)
client.register_thing_class('/type/list', List)
client.register_thing_class('/type/usergroup', UserGroup)
client.register_thing_class('/type/tag', Tag)


def register_types():
Expand All @@ -1174,6 +1224,7 @@ def register_types():
types.register_type('^/books/[^/]*$', '/type/edition')
types.register_type('^/works/[^/]*$', '/type/work')
types.register_type('^/languages/[^/]*$', '/type/language')
types.register_type('^/tags/[^/]*$', '/type/tag')

types.register_type('^/usergroup/[^/]*$', '/type/usergroup')
types.register_type('^/permission/[^/]*$', '/type/permission')
Expand Down
2 changes: 2 additions & 0 deletions openlibrary/core/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ def get_schema():
schema.add_table_group('work', '/type/work', datatypes)
schema.add_table_group('publisher', '/type/publisher', datatypes)
schema.add_table_group('subject', '/type/subject', datatypes)
# schema.add_table_group('tag', '/type/tag', datatypes)

schema.add_seq('/type/edition', '/books/OL%dM')
schema.add_seq('/type/author', '/authors/OL%dA')

schema.add_seq('/type/work', '/works/OL%dW')
schema.add_seq('/type/publisher', '/publishers/OL%dP')
schema.add_seq('/type/tag', '/tags/OL%dT')

_sql = schema.sql

Expand Down
61 changes: 61 additions & 0 deletions openlibrary/plugins/openlibrary/types/tag.type
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"name": "Tag",
"key": "/type/tag",
"id": 121411342,
"kind": "regular",
"created": {
"type": "/type/datetime",
"value": "2023-04-05T22:27:36.162339"
},
"last_modified": {
"type": "/type/datetime",
"value": "2023-04-05T22:37:06.504291"
},
"latest_revision": 3,
"type": {
"key": "/type/type"
},
"properties": [
{
"expected_type": {
"key": "/type/string",
},
"name": "name",
"type": {
"key": "/type/property"
},
"unique": true
},
{
"expected_type": {
"key": "/type/string",
},
"name": "tag_description",
"type": {
"key": "/type/property"
},
"unique": true
},
{
"expected_type": {
"key": "/type/string",
},
"name": "tag_plugins",
"type": {
"key": "/type/property"
},
"unique": true
},
{
"expected_type": {
"key": "/type/string",
},
"name": "tag_type",
"type": {
"key": "/type/property"
},
"unique": true
},
],
"revision": 3
}