diff --git a/ereuse_devicehub/inventory/forms.py b/ereuse_devicehub/inventory/forms.py index 26c3a700..78c9ebfe 100644 --- a/ereuse_devicehub/inventory/forms.py +++ b/ereuse_devicehub/inventory/forms.py @@ -70,7 +70,7 @@ from ereuse_devicehub.resources.device.models import ( from ereuse_devicehub.resources.documents.models import DataWipeDocument from ereuse_devicehub.resources.enums import Severity from ereuse_devicehub.resources.hash_reports import insert_hash -from ereuse_devicehub.resources.lot.models import Lot +from ereuse_devicehub.resources.lot.models import Lot, ShareLot from ereuse_devicehub.resources.tag.model import Tag from ereuse_devicehub.resources.tradedocument.models import TradeDocument from ereuse_devicehub.resources.user.models import User @@ -160,11 +160,14 @@ class FilterForm(FlaskForm): '', choices=DEVICES, default="All Computers", render_kw={'class': "form-select"} ) - def __init__(self, lots, lot_id, *args, **kwargs): + def __init__(self, lots, lot, lot_id, *args, **kwargs): self.all_devices = kwargs.pop('all_devices', False) super().__init__(*args, **kwargs) self.lots = lots + self.lot = lot self.lot_id = lot_id + if self.lot_id and not self.lot: + self.lot = self.lots.filter(Lot.id == self.lot_id).one() self._get_types() def _get_types(self): @@ -175,8 +178,7 @@ class FilterForm(FlaskForm): self.filter.data = self.device_type def filter_from_lots(self): - if self.lot_id: - self.lot = self.lots.filter(Lot.id == self.lot_id).one() + if self.lot: device_ids = (d.id for d in self.lot.devices) self.devices = Device.query.filter(Device.id.in_(device_ids)).filter( Device.binding == None # noqa: E711 @@ -256,7 +258,8 @@ class LotForm(FlaskForm): return self.id def remove(self): - if self.instance and not self.instance.trade: + shared = ShareLot.query.filter_by(lot=self.instance).first() + if self.instance and not self.instance.trade and not shared: self.instance.delete() db.session.commit() return self.instance diff --git a/ereuse_devicehub/inventory/views.py b/ereuse_devicehub/inventory/views.py index bb1ac9a9..4ddf5a23 100644 --- a/ereuse_devicehub/inventory/views.py +++ b/ereuse_devicehub/inventory/views.py @@ -14,6 +14,7 @@ from flask import current_app as app from flask import g, make_response, request, url_for from flask.views import View from flask_login import current_user, login_required +from sqlalchemy import or_ from werkzeug.exceptions import NotFound from ereuse_devicehub import messages @@ -51,7 +52,7 @@ from ereuse_devicehub.resources.device.models import ( from ereuse_devicehub.resources.documents.device_row import ActionRow, DeviceRow from ereuse_devicehub.resources.enums import SnapshotSoftware from ereuse_devicehub.resources.hash_reports import insert_hash -from ereuse_devicehub.resources.lot.models import Lot +from ereuse_devicehub.resources.lot.models import Lot, ShareLot from ereuse_devicehub.resources.tag.model import Tag from ereuse_devicehub.views import GenericMixin @@ -73,19 +74,25 @@ class DeviceListMixin(GenericMixin): per_page = int(request.args.get('per_page', PER_PAGE)) filter = request.args.get('filter', "All+Computers") + lot = None + + share_lots = self.context['share_lots'] + share_lot = share_lots.filter_by(lot_id=lot_id).first() + if share_lot: + lot = share_lot.lot + lots = self.context['lots'] - form_filter = FilterForm(lots, lot_id, all_devices=all_devices) + form_filter = FilterForm(lots, lot, lot_id, all_devices=all_devices) devices = form_filter.search().paginate(page=page, per_page=per_page) devices.first = per_page * devices.page - per_page + 1 devices.last = len(devices.items) + devices.first - 1 - lot = None form_transfer = '' form_delivery = '' form_receiver = '' form_customer_details = '' - if lot_id: + if lot_id and not lot: lot = lots.filter(Lot.id == lot_id).one() if not lot.is_temporary and lot.transfer: form_transfer = EditTransferForm(lot_id=lot.id) @@ -111,6 +118,7 @@ class DeviceListMixin(GenericMixin): 'list_devices': self.get_selected_devices(form_new_action), 'all_devices': all_devices, 'filter': filter, + 'share_lots': share_lots, } ) @@ -537,8 +545,9 @@ class LotDeleteView(View): def dispatch_request(self, id): form = LotForm(id=id) - if form.instance.trade: - msg = "Sorry, the lot cannot be deleted because have a trade action " + shared = ShareLot.query.filter_by(lot=form.instance).first() + if form.instance.trade or shared: + msg = "Sorry, the lot cannot be deleted because this lot is share" messages.error(msg) next_url = url_for('inventory.lotdevicelist', lot_id=id) return flask.redirect(next_url) @@ -1005,9 +1014,21 @@ class ExportsView(View): return export_ids[export_id]() def find_devices(self): + # import pdb; pdb.set_trace() + sql = """ + select lot_device.device_id as id from {schema}.share_lot as share + inner join {schema}.lot_device as lot_device + on share.lot_id=lot_device.lot_id + where share.user_to_id='{user_id}' + """.format( + schema=app.config.get('SCHEMA'), user_id=g.user.id + ) + + shared = (x[0] for x in db.session.execute(sql)) + args = request.args.get('ids') ids = args.split(',') if args else [] - query = Device.query.filter(Device.owner == g.user) + query = Device.query.filter(or_(Device.owner == g.user, Device.id.in_(shared))) return query.filter(Device.devicehub_id.in_(ids)) def response_csv(self, data, name): diff --git a/ereuse_devicehub/labels/views.py b/ereuse_devicehub/labels/views.py index 13766bfe..89697271 100644 --- a/ereuse_devicehub/labels/views.py +++ b/ereuse_devicehub/labels/views.py @@ -8,7 +8,7 @@ from requests.exceptions import ConnectionError from ereuse_devicehub import __version__, messages from ereuse_devicehub.labels.forms import PrintLabelsForm, TagForm, TagUnnamedForm -from ereuse_devicehub.resources.lot.models import Lot +from ereuse_devicehub.resources.lot.models import Lot, ShareLot from ereuse_devicehub.resources.tag.model import Tag labels = Blueprint('labels', __name__, url_prefix='/labels') @@ -23,6 +23,7 @@ class TagListView(View): def dispatch_request(self): lots = Lot.query.filter(Lot.owner_id == current_user.id) + share_lots = ShareLot.query.filter_by(user_to_id=current_user.id) tags = Tag.query.filter(Tag.owner_id == current_user.id).order_by( Tag.created.desc() ) @@ -31,6 +32,7 @@ class TagListView(View): 'tags': tags, 'page_title': 'Unique Identifiers Management', 'version': __version__, + 'share_lots': share_lots, } return flask.render_template(self.template_name, **context) @@ -42,7 +44,13 @@ class TagAddView(View): def dispatch_request(self): lots = Lot.query.filter(Lot.owner_id == current_user.id) - context = {'page_title': 'New Tag', 'lots': lots, 'version': __version__} + share_lots = ShareLot.query.filter_by(user_to_id=current_user.id) + context = { + 'page_title': 'New Tag', + 'lots': lots, + 'version': __version__, + 'share_lots': share_lots, + } form = TagForm() if form.validate_on_submit(): form.save() @@ -59,10 +67,12 @@ class TagAddUnnamedView(View): def dispatch_request(self): lots = Lot.query.filter(Lot.owner_id == current_user.id) + share_lots = ShareLot.query.filter_by(user_to_id=current_user.id) context = { 'page_title': 'New Unnamed Tag', 'lots': lots, 'version': __version__, + 'share_lots': share_lots, } form = TagUnnamedForm() if form.validate_on_submit(): @@ -94,11 +104,13 @@ class PrintLabelsView(View): def dispatch_request(self): lots = Lot.query.filter(Lot.owner_id == current_user.id) + share_lots = ShareLot.query.filter_by(user_to_id=current_user.id) context = { 'lots': lots, 'page_title': self.title, 'version': __version__, 'referrer': request.referrer, + 'share_lots': share_lots, } form = PrintLabelsForm() @@ -123,6 +135,7 @@ class LabelDetailView(View): def dispatch_request(self, id): lots = Lot.query.filter(Lot.owner_id == current_user.id) + share_lots = ShareLot.query.filter_by(user_to_id=current_user.id) tag = ( Tag.query.filter(Tag.owner_id == current_user.id).filter(Tag.id == id).one() ) @@ -131,6 +144,7 @@ class LabelDetailView(View): 'page_title': self.title, 'version': __version__, 'referrer': request.referrer, + 'share_lots': share_lots, } devices = [] diff --git a/ereuse_devicehub/migrations/versions/2f2ef041483a_share_lot.py b/ereuse_devicehub/migrations/versions/2f2ef041483a_share_lot.py new file mode 100644 index 00000000..9284475e --- /dev/null +++ b/ereuse_devicehub/migrations/versions/2f2ef041483a_share_lot.py @@ -0,0 +1,52 @@ +"""share lot + +Revision ID: 2f2ef041483a +Revises: ac476b60d952 +Create Date: 2023-04-26 16:04:21.560888 + +""" +import sqlalchemy as sa +from alembic import context, op +from sqlalchemy.dialects import postgresql + +# revision identifiers, used by Alembic. +revision = '2f2ef041483a' +down_revision = 'ac476b60d952' +branch_labels = None +depends_on = None + + +def get_inv(): + INV = context.get_x_argument(as_dictionary=True).get('inventory') + if not INV: + raise ValueError("Inventory value is not specified") + return INV + + +def upgrade(): + op.create_table( + 'share_lot', + sa.Column( + 'created', + sa.TIMESTAMP(timezone=True), + server_default=sa.text('CURRENT_TIMESTAMP'), + nullable=False, + ), + sa.Column( + 'updated', + sa.TIMESTAMP(timezone=True), + server_default=sa.text('CURRENT_TIMESTAMP'), + nullable=False, + ), + sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False), + sa.Column('user_to_id', postgresql.UUID(as_uuid=True), nullable=True), + sa.Column('lot_id', postgresql.UUID(as_uuid=True), nullable=False), + sa.ForeignKeyConstraint(['user_to_id'], ['common.user.id']), + sa.ForeignKeyConstraint(['lot_id'], [f'{get_inv()}.lot.id']), + sa.PrimaryKeyConstraint('id'), + schema=f'{get_inv()}', + ) + + +def downgrade(): + op.drop_table('share_lot', schema=f'{get_inv()}') diff --git a/ereuse_devicehub/resources/lot/models.py b/ereuse_devicehub/resources/lot/models.py index 23a7ef0c..39ee9f73 100644 --- a/ereuse_devicehub/resources/lot/models.py +++ b/ereuse_devicehub/resources/lot/models.py @@ -124,7 +124,10 @@ class Lot(Thing): @property def is_temporary(self): - return not bool(self.trade) and not bool(self.transfer) + trade = bool(self.trade) + transfer = bool(self.transfer) + owner = self.owner == g.user + return not trade and not transfer and owner @property def is_incoming(self): @@ -144,6 +147,19 @@ class Lot(Thing): return False + @property + def is_shared(self): + try: + self.shared + except Exception: + self.shared = ShareLot.query.filter_by( + lot_id=self.id, user_to=g.user + ).first() + + if self.shared: + return True + return False + @classmethod def descendantsq(cls, id): _id = UUIDLtree.convert(id) @@ -396,3 +412,15 @@ class LotParent(db.Model): .select_from(Path) .where(i > 0), ) + + +class ShareLot(Thing): + id = db.Column(UUID(as_uuid=True), primary_key=True) + lot_id = db.Column(db.UUID(as_uuid=True), db.ForeignKey(Lot.id), nullable=False) + lot = db.relationship(Lot, primaryjoin=lot_id == Lot.id) + user_to_id = db.Column( + UUID(as_uuid=True), + db.ForeignKey(User.id), + nullable=True, + ) + user_to = db.relationship(User, primaryjoin=user_to_id == User.id) diff --git a/ereuse_devicehub/templates/ereuse_devicehub/base_site.html b/ereuse_devicehub/templates/ereuse_devicehub/base_site.html index de544d75..179dcfd4 100644 --- a/ereuse_devicehub/templates/ereuse_devicehub/base_site.html +++ b/ereuse_devicehub/templates/ereuse_devicehub/base_site.html @@ -276,7 +276,28 @@ {% endif %} {% endfor %} + + {% if share_lots.all() %} + + {% elif lot.is_shared %} + + {% endif %} @@ -39,7 +42,9 @@

- {{ lot.name }} + + {{ lot.name }} {% if lot.is_shared %} {{ lot.owner.email }}{% endif %} +

@@ -54,10 +59,12 @@ Create Incoming Lot {% endif %} + {% if not lot.is_shared %} Delete Lot + {% endif %} {% endif %}
@@ -72,7 +79,7 @@ - {% if lot and not lot.is_temporary %} + {% if lot and not lot.is_temporary and not lot.is_shared %} @@ -106,6 +113,7 @@
+ {% if not lot or not lot.is_shared %} + {% endif %} + {% if lot and lot.is_shared %} + + {% endif %}
- {% if lot and not lot.is_temporary %} + {% if lot and not lot.is_temporary and not lot.is_shared %}