resolve conflict

This commit is contained in:
Cayo Puigdefabregas 2021-06-08 09:30:13 +02:00
commit 1e0d993cfe
3 changed files with 51 additions and 39 deletions

View File

@ -57,6 +57,9 @@ def upgrade():
sa.Column('user_from_id', postgresql.UUID(as_uuid=True), nullable=False),
sa.Column('user_to_id', postgresql.UUID(as_uuid=True), nullable=False),
sa.Column('document_id', citext.CIText(), nullable=True),
sa.Column('confirm', sa.Boolean(), nullable=True),
sa.Column('code', citext.CIText(), default='', nullable=True,
comment = "This code is used for traceability"),
sa.ForeignKeyConstraint(['id'], [f'{get_inv()}.action.id'], ),
sa.ForeignKeyConstraint(['user_from_id'], ['common.user.id'], ),
sa.ForeignKeyConstraint(['user_to_id'], ['common.user.id'], ),
@ -71,10 +74,10 @@ def upgrade():
op.create_table('confirm',
sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False),
sa.Column('user_id', postgresql.UUID(as_uuid=True), nullable=False),
sa.Column('trade_id', postgresql.UUID(as_uuid=True), nullable=False),
sa.Column('action_id', postgresql.UUID(as_uuid=True), nullable=False),
sa.ForeignKeyConstraint(['id'], [f'{get_inv()}.action.id'], ),
sa.ForeignKeyConstraint(['trade_id'], [f'{get_inv()}.trade.id'], ),
sa.ForeignKeyConstraint(['action_id'], [f'{get_inv()}.action.id'], ),
sa.ForeignKeyConstraint(['user_id'], ['common.user.id'], ),
sa.PrimaryKeyConstraint('id'),
schema=f'{get_inv()}'

View File

@ -714,26 +714,34 @@ class Trade(ActionWithMultipleDevices):
* without confirmation
"""
if not (data['user_from_email'] or data['user_to_email']):
txt = "you need one user from or user to for to do a offer"
raise ValidationError(txt)
if data['user_from_email']:
user_from = User.query.filter_by(email=data['user_from_email']).one()
data['user_from'] = user_from
else:
data['confirm'] = False
@validates_schema
def validate_email_users(self, data: dict):
"""We need at least one user"""
if not (data['user_from_email'] or data['user_to_email']):
txt = "you need one user from or user to for to do a trade"
raise ValidationError(txt)
if not g.user.email in [data['user_from_email'], data['user_to_email']]:
txt = "you need to be one of participate of the action"
raise ValidationError(txt)
@validates_schema
def validate_code(self, data: dict):
"""If the user not exist, you need a code to be able to do the traceability"""
if data['user_from_email'] and data['user_to_email']:
data['confirm'] = True
return
if not data.get('code'):
if not data['confirm'] and not data.get('code'):
txt = "you need a code to be able to do the traceability"
raise ValidationError(txt)
data['code'] = data['code'].replace('@', '_')
class InitTransfer(Trade):
__doc__ = m.InitTransfer.__doc__

View File

@ -29,14 +29,14 @@ class TradeView():
def __init__(self, data, resource_def, schema):
self.schema = schema
a = resource_def.schema.load(data)
a.pop('user_to_email', '')
a.pop('user_from_email', '')
self.trade = Trade(**a)
self.data = resource_def.schema.load(data)
self.data.pop('user_to_email', '')
self.data.pop('user_from_email', '')
self.create_phantom_account()
self.trade = Trade(**self.data)
db.session.add(self.trade)
self.create_automatic_trade()
self.create_confirmations()
self.create_automatic_trade()
def post(self):
db.session().final_flush()
@ -70,16 +70,13 @@ class TradeView():
txt = "You do not participate in this trading"
raise ValidationError(txt)
if self.trade.user_from == g.user or self.trade.user_from.phantom:
confirm_from = Confirm(user=self.trade.user_from,
action=self.trade,
devices=self.trade.devices)
db.session.add(confirm_from)
if self.trade.user_to == g.user or self.trade.user_to.phantom:
confirm_to = Confirm(user=self.trade.user_to,
action=self.trade,
devices=self.trade.devices)
db.session.add(confirm_from)
db.session.add(confirm_to)
def create_phantom_account(self) -> None:
@ -92,33 +89,40 @@ class TradeView():
The same if exist to but not from
"""
if self.trade.user_from and self.trade.user_to:
user_from = self.data.get('user_from')
user_to = self.data.get('user_to')
code = self.data.get('code')
if user_from and user_to:
return
if self.trade.user_from and not self.trade.user_to:
assert g.user == self.trade.user_from
email = "{}_{}@dhub.com".format(str(self.trade.user_from.id), self.trade.code)
if self.data['confirm']:
return
if user_from and not user_to:
assert g.user == user_from
email = "{}_{}@dhub.com".format(str(user_from.id), code)
users = User.query.filter_by(email=email)
if users.first():
user = users.first()
self.trade.user_to = user
self.data['user_to'] = user
return
user = User(email=email, password='', active=False, phantom=True)
db.session.add(user)
self.trade.user_to = user
self.data['user_to'] = user
if not self.trade.user_from and self.trade.user_to:
email = "{}_{}@dhub.com".format(str(self.trade.user_to.id), self.trade.code)
if not user_from and user_to:
email = "{}_{}@dhub.com".format(str(user_to.id), code)
users = User.query.filter_by(email=email)
if users.first():
user = users.first()
self.trade.user_from = user
self.data['user_from'] = user
return
user = User(email=email, password='', active=False, phantom=True)
db.session.add(user)
self.trade.user_from = user
self.data['user_from'] = user
def create_automatic_trade(self) -> None:
# not do nothing if it's neccesary confirmation explicity
@ -127,10 +131,7 @@ class TradeView():
# Change the owner for every devices
for dev in self.trade.devices:
dev.owner = self.trade.user_to
if hasattr(dev, 'components'):
for c in dev.components:
c.owner = self.trade.user_to
dev.change_owner(self.trade.user_to)
class ConfirmMixin():