Skip to content

Conversation

@uds5501
Copy link
Contributor

@uds5501 uds5501 commented Mar 2, 2019

Fixes fossasia/open-event-frontend#2169

Checklist

  • I have read the Contribution & Best practices Guide and my PR follows them.
  • My branch is up-to-date with the Upstream development branch.
  • The unit tests pass locally with my changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)
  • All the functions created/modified in this PR contain relevant docstrings.

Short description of what this resolves:

this makes sure no more than 1 object of a particular Currency-Country combination is being formed

Changes proposed in this pull request:

  • implement conflict checks for currency-country combinations

pass
else:
raise ConflictException({'pointer': ''},
"Currency Country combination ({}-{}) already exists"\

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the backslash is redundant between brackets

@uds5501
Copy link
Contributor Author

uds5501 commented Mar 2, 2019

ezgif com-video-to-gif 2

Only unique objects are present now:

unqieucombos

@uds5501
Copy link
Contributor Author

uds5501 commented Mar 2, 2019

@iamareebjamal @CosmicCoder96 Please review :) 🍻

@fossasia fossasia deleted a comment Mar 2, 2019
@fossasia fossasia deleted a comment Mar 2, 2019
@fossasia fossasia deleted a comment Mar 3, 2019
@fossasia fossasia deleted a comment Mar 3, 2019
- implement conflict checks for currency-country combinations
- change TicketFee Factory model
- add Unprocessable Entity exception for API
@uds5501
Copy link
Contributor Author

uds5501 commented Mar 3, 2019

@CosmicCoder96 @iamareebjamal According to my API conditions, this particular test case should've failed. So, how do I fix that Travis build?

@fossasia fossasia deleted a comment Mar 3, 2019
@fossasia fossasia deleted a comment Mar 3, 2019
else:
raise UnprocessableEntity({'source': ''}, "Country or Currency cannot be NULL")
else:
raise UnprocessableEntity({'source': ''}, "Country or Currency Attribute is missing")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use marshmallow for validation

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iamareebjamal could you please elaborate?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood, shall commit soon

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@uds5501 I think here @iamareebjamal simply means that use allow_none=False in the schema itself. It will raise error if country or currency is missing and you can use nullable=False in model to disallow use of NULL values.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shreyanshdwivedi Ooooh! I understand now. I will update it soon

@uds5501
Copy link
Contributor Author

uds5501 commented Mar 7, 2019

@iamareebjamal I was using @validate_schema decorator to apply the condition checks on schema level, but it can't differenciate between a POST and a PATCH request.
My objective is to allow PATCH requests but deny any POST request which aims to create a new object of same currency and country which is already existing.
What shall I do in this case??

@iamareebjamal
Copy link
Member

The same thing is applied to all types of schema like event, speaker, etc.

Check those.

You can use before hooks but don't validate the schema there.

@uds5501
Copy link
Contributor Author

uds5501 commented Mar 7, 2019

@iamareebjamal Thanks! Fixed it now, committing

@fossasia fossasia deleted a comment Mar 7, 2019
Copy link
Member

@iamareebjamal iamareebjamal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are still validating the schema. There should be no check for country or currency. It should be handled by Marshmallow

@shreyanshdwivedi
Copy link
Member

@uds5501 can you please update this ? It seems quite an important bug to me as it deals with the fees charged.

@uds5501
Copy link
Contributor Author

uds5501 commented May 17, 2019

@shreyanshdwivedi I will be needing some help regarding the marshmallow schema validation. As to how to determine if the new combination is present or not

@shreyanshdwivedi
Copy link
Member

@uds5501 also don't validate schema where you are doing it currently. Instead use the before hooks which means functions like - before_update_object or before_post_object to raise errors which you are currenlty doing via validating schema.
Reference examples -

def before_post(self, args, kwargs, data=None):

See how validate_event function is called before posting. If there is any discrepancies error is raised. Similarly you can find checks before updating the object. Implement something like this.
Hope you understood 😅

@uds5501 uds5501 changed the title feat : add conflict check for TicketFee feat: add conflict check for TicketFee May 21, 2019
@auto-label auto-label bot added the feature label May 21, 2019
@uds5501
Copy link
Contributor Author

uds5501 commented May 21, 2019

@mrsaicharan1 @shreyanshdwivedi @iamareebjamal @CosmicCoder96
I want the model to not take any NULL values, but for that, I would need the migration file to ensure that old entries of ticket_fees country and currency fields are filled up with some server_default values.
can I set them to be 'United States' and 'USD' respectively?

@iamareebjamal
Copy link
Member

You can just define those in marshmallow schema for now

@uds5501
Copy link
Contributor Author

uds5501 commented May 22, 2019

@iamareebjamal understood, shall do that

@uds5501
Copy link
Contributor Author

uds5501 commented May 22, 2019

@iamareebjamal @shreyanshdwivedi Please review

@fossasia fossasia deleted a comment May 22, 2019
id = fields.Integer(dump_only=True)
currency = fields.Str(validate=validate.OneOf(choices=PAYMENT_CURRENCY_CHOICES), allow_none=True)
country = fields.String(allow_none=True)
currency = fields.Str(validate=validate.OneOf(choices=PAYMENT_CURRENCY_CHOICES), allow_none=False, default='USD')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't default to USD. We need the user to provide the values. There is no UI showing the user that it will default to USD if no value is provided

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iamareebjamal So I should use no default value?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

@uds5501
Copy link
Contributor Author

uds5501 commented May 30, 2019

@iamareebjamal I have removed the default values from schema, does anything else needs to be done now?

if (data['country'] and data['currency']):
try:
already_existing_combination = TicketFees.query.filter_by(country=data['country'],
currency=data['currency']).one()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just add a unique key on country and currency. No need for this check

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iamareebjamal A unique key constraint would ensure that I don't get to save similar values of the same column.

I want to be able to save :USA - USD and USA-INR combination simultaneously. I don't think adding unique constraints would allow that

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are simulating unique key constraint on TicketFee, if unique key won't work, this shouldn't work too

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are ensuring that country and currency pair is unique and not repeated. That's the work on unique key

@shreyanshdwivedi
Copy link
Member

@uds5501 similar to my PR, this is failing due to multiple heads. Please fix it

@fossasia fossasia deleted a comment Jun 3, 2019
@fossasia fossasia deleted a comment Jun 3, 2019
@uds5501
Copy link
Contributor Author

uds5501 commented Jun 3, 2019

@shreyanshdwivedi do i need to change the revises in migration file here?


# revision identifiers, used by Alembic.
revision = 'cb4ccda76a2b'
down_revision = '0e80c49a6e28'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@uds5501 change down_revision and revises to Revision ID of migration in my latest merged PR

@uds5501
Copy link
Contributor Author

uds5501 commented Jun 4, 2019

@shreyanshdwivedi this PR won't work until the dredd hook request is fixed. I am opening an issue to fix this request :

{
  "data": {
    "type": "ticket-fee",
    "attributes": {
      "currency": "USD",
      "service-fee": "3.14",
      "maximum-fee": "5.14"
    }
  }
}

Copy link
Member

@iamareebjamal iamareebjamal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have to add composite unique key on country and currency if I understand correctly. Shouldn't I be able to have ('UK', 'GBP') and ('UK', 'EUR')?

@uds5501
Copy link
Contributor Author

uds5501 commented Jun 4, 2019

@iamareebjamal you are right, I will look for corresponding resources and update this soon

@kushthedude
Copy link
Member

Closed in favour of #6017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

More than one fee settings can be saved for a payment country - payment currency combination

5 participants