Repository: fossasia/open-event-server

Issue:
feat: Max/Min price fields for donation tickets
**Describe the feature**
There is a requirement of min. price and max.price for donation tickets. Schemas need to be added as well.
Frontend PR: https://github.com/fossasia/open-event-frontend/pull/3069


**Additional info**
Working on it



Discussion:

Resolving patch:
diff --git a/app/api/schema/tickets.py b/app/api/schema/tickets.py
--- a/app/api/schema/tickets.py
+++ b/app/api/schema/tickets.py
@@ -57,6 +57,11 @@ def validate_quantity(self, data):
                 raise UnprocessableEntity({'pointer': '/data/attributes/quantity'},
                                           "quantity should be greater than or equal to min-order")
 
+        if 'min_price' in data and 'max_price' in data and data['type'] == 'donation':
+                if data['min_price'] > data['max_price']:
+                    raise UnprocessableEntity({'pointer': '/data/attributes/min-price'},
+                                              "minimum price should be lesser than or equal to maximum price")
+
         if 'quantity' in data and 'max_order' in data:
             if data['quantity'] < data['max_order']:
                 raise UnprocessableEntity({'pointer': '/data/attributes/quantity'},
@@ -78,6 +83,8 @@ def validate_discount_code(self, data, original_data):
     description = fields.Str(allow_none=True)
     type = fields.Str(required=True)
     price = fields.Float(validate=lambda n: n >= 0, allow_none=True)
+    min_price = fields.Float(validate=lambda n: n >= 0)
+    max_price = fields.Float(validate=lambda n: n >= 0, allow_none=True)
     quantity = fields.Integer(validate=lambda n: n >= 0, allow_none=True)
     is_description_visible = fields.Boolean(default=False)
     position = fields.Integer(allow_none=True)
diff --git a/app/models/ticket.py b/app/models/ticket.py
--- a/app/models/ticket.py
+++ b/app/models/ticket.py
@@ -32,6 +32,8 @@ class Ticket(SoftDeletionModel):
     quantity = db.Column(db.Integer)
     position = db.Column(db.Integer)
     price = db.Column(db.Float)
+    min_price = db.Column(db.Float, default=0, nullable=False)
+    max_price = db.Column(db.Float)
     is_fee_absorbed = db.Column(db.Boolean)
     sales_starts_at = db.Column(db.DateTime(timezone=True), nullable=False)
     sales_ends_at = db.Column(db.DateTime(timezone=True), nullable=False)
@@ -67,6 +69,8 @@ def __init__(self,
                  price=0,
                  min_order=1,
                  max_order=10,
+                 min_price=0,
+                 max_price=0,
                  is_fee_absorbed=False,
                  tags=[],
                  access_codes=[],
@@ -82,6 +86,8 @@ def __init__(self,
         self.is_checkin_restricted = is_checkin_restricted
         self.auto_checkin_enabled = auto_checkin_enabled
         self.price = price
+        self.min_price = min_price
+        self.max_price = max_price
         self.sales_starts_at = sales_starts_at
         self.sales_ends_at = sales_ends_at
         self.is_hidden = is_hidden
diff --git a/migrations/versions/43e8c59337ae_.py b/migrations/versions/43e8c59337ae_.py
new file mode 100644
--- /dev/null
+++ b/migrations/versions/43e8c59337ae_.py
@@ -0,0 +1,30 @@
+"""empty message
+
+Revision ID: 43e8c59337ae
+Revises: 232b13d4e86a
+Create Date: 2019-06-18 10:13:37.254265
+
+"""
+
+from alembic import op
+import sqlalchemy as sa
+import sqlalchemy_utils
+
+
+# revision identifiers, used by Alembic.
+revision = '43e8c59337ae'
+down_revision = '232b13d4e86a'
+
+
+def upgrade():
+    # ### commands auto generated by Alembic - please adjust! ###
+    op.add_column('tickets', sa.Column('max_price', sa.Float(), nullable=True))
+    op.add_column('tickets', sa.Column('min_price', sa.Float(), server_default=0, nullable=False))
+    # ### end Alembic commands ###
+
+
+def downgrade():
+    # ### commands auto generated by Alembic - please adjust! ###
+    op.drop_column('tickets', 'min_price')
+    op.drop_column('tickets', 'max_price')
+    # ### end Alembic commands ###


Test patch: