# Path: demetsiiify/extensions.py



from datetime import datetime
from flask import current_app, url_for
from sqlalchemy.dialects import postgresql as pg
from sqlalchemy.orm import load_only
from .extensions import db
import shortuuid
        if iiif_id:
            self.iiif_id = iiif_id

    @classmethod
    def get(cls, id_):
        return cls.query.get(id_)

    @classmethod
    def by_url(cls, url):
        return cls.query.filter_by(url=url).first()

    @classmethod
    def save(cls, *images):
        if not images:
            return
        base_query = pg.insert(cls).returning(Image.id)
        return db.session.execute(
            base_query.on_conflict_do_update(
                index_elements=[Image.url],
                set_=dict(width=base_query.excluded.width,
                          height=base_query.excluded.height,
                          format=base_query.excluded.format,
                          iiif_id=base_query.excluded.iiif_id)),
            [dict(url=i.url, width=i.width, height=i.height,
                  format=i.format, iiif_id=i.iiif_id) for i in images])


class Annotation(db.Model):
    surrogate_id = db.Column(db.Integer, primary_key=True)
    id = db.Column(db.String, unique=True, nullable=False)

# Path: demetsiiify/extensions.py



# Path: demetsiiify/models.py
from datetime import datetime
from flask import current_app, url_for
from sqlalchemy.dialects import postgresql as pg
from sqlalchemy.orm import load_only
from .extensions import db
import shortuuid

        if iiif_id:
            self.iiif_id = iiif_id

    @classmethod
    def get(cls, id_):
        return cls.query.get(id_)

    @classmethod
    def by_url(cls, url):
        return cls.query.filter_by(url=url).first()

    @classmethod
    def save(cls, *images):
        if not images:
            return
        base_query = pg.insert(cls).returning(Image.id)
        return db.session.execute(
            base_query.on_conflict_do_update(
                index_elements=[Image.url],
                set_=dict(width=base_query.excluded.width,
                          height=base_query.excluded.height,
                          format=base_query.excluded.format,
                          iiif_id=base_query.excluded.iiif_id)),
            [dict(url=i.url, width=i.width, height=i.height,
                  format=i.format, iiif_id=i.iiif_id) for i in images])


class Annotation(db.Model):
    surrogate_id = db.Column(db.Integer, primary_key=True)
    id = db.Column(db.String, unique=True, nullable=False)

    target = db.Column(db.String, nullable=False, index=True)