Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added is_cachable to requirements #608

Merged
merged 3 commits into from
Aug 26, 2024
Merged

Conversation

alimaktabi
Copy link
Collaborator

@alimaktabi alimaktabi commented Aug 26, 2024

Summary by Sourcery

Add a new 'is_cachable' attribute to constraints to determine if their verification results should be cached, and update the caching logic in validators to respect this attribute.

Enhancements:

  • Introduce the 'is_cachable' attribute to the ConstraintVerification class to control caching behavior.

Copy link
Contributor

sourcery-ai bot commented Aug 26, 2024

Reviewer's Guide by Sourcery

This pull request introduces a new is_cachable attribute to constraint verifications, allowing for more granular control over caching behavior. The changes primarily affect the caching logic in user constraint and permission checks.

File-Level Changes

Change Details Files
Added an is_cachable attribute to the ConstraintVerification class
  • Introduced is_cachable = True as a default attribute in the ConstraintVerification class
  • Set is_cachable = False for the HasVerifiedCloudflareCaptcha constraint
core/constraints/abstract.py
core/constraints/captcha.py
Modified caching logic in user constraint and permission checks
  • Added a condition to check constraint.is_cachable before caching verification results
  • Wrapped the existing caching logic inside the new condition
  • Kept the error handling and result assignment logic outside the caching condition
prizetap/validators.py
tokenTap/validators.py

Tips
  • Trigger a new Sourcery review by commenting @sourcery-ai review on the pull request.
  • Continue your discussion with Sourcery by replying directly to review comments.
  • You can change your review settings at any time by accessing your dashboard:
    • Enable or disable the Sourcery-generated pull request summary or reviewer's guide;
    • Change the review language;
  • You can always contact us if you have any questions or feedback.

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @alimaktabi - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment to tell me if it was helpful.

@@ -55,6 +55,7 @@ class ConstraintVerification(ABC):
_param_keys = []
app_name = ConstraintApp.GENERAL.value
__response_text = ""
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: Consider adding a comment explaining the purpose and usage of the is_cachable attribute

This attribute controls whether the constraint verification result should be cached. It might be helpful to provide examples of when it should be set to False, such as for time-sensitive or security-critical verifications.

cache_data,
caching_time,
)
if constraint.is_cachable:
Copy link
Contributor

Choose a reason for hiding this comment

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

issue (complexity): Consider refactoring the caching logic into a separate function.

The new code introduces additional complexity due to the conditional caching logic based on constraint.is_cachable. This adds cognitive load and can make the code harder to read and maintain. Consider refactoring the caching logic into a separate function to encapsulate the behavior and improve readability. This would reduce duplication and make the main function more straightforward. Here's a suggestion:

def cache_constraint_result(cache_key, is_verified, info):
    caching_time = 60 * 60 if is_verified else 60
    expiration_time = time.time() + caching_time
    cache_data = {
        "is_verified": is_verified,
        "info": info,
        "expiration_time": expiration_time,
    }
    cache.set(cache_key, cache_data, caching_time)
    return cache_data

# In the main function
if constraint.is_cachable:
    cache_data = cache_constraint_result(cache_key, is_verified, info)
else:
    cache_data = {"is_verified": is_verified, "info": info}

if not cache_data.get("is_verified"):
    error_messages[c.title] = constraint.response
result[c.pk] = cache_data

This refactoring should help in making the code cleaner and easier to maintain.

@alimaktabi alimaktabi merged commit dd28a56 into develop Aug 26, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant