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

Implement voucher per event and for all events of an organizer | Create Voucher #473

Merged
merged 8 commits into from
Dec 18, 2024

Conversation

odkhang
Copy link
Collaborator

@odkhang odkhang commented Dec 12, 2024

This PR partly resolves - #382 Implement voucher per event and for all events of an organizer
This PR implement:

  1. New page in admin area called "Voucher" /control/admin/vouchers/

image

  1. Allow Admin to create voucher which will be using by Organizer to deduce billing invoice price.

image

Summary by Sourcery

Implement a voucher management system in the admin area, enabling the creation, update, and deletion of vouchers that can be used by organizers to adjust billing invoices. This includes a new admin page for managing vouchers and the ability to limit voucher usage to specific events or organizers.

New Features:

  • Introduce a new 'Voucher' page in the admin area, allowing administrators to manage vouchers that organizers can use to reduce billing invoice prices.

Enhancements:

  • Add functionality for creating, updating, and deleting vouchers, with options to limit their usage to specific events or organizers.

Summary by Sourcery

Implement a new voucher management system in the admin area, allowing administrators to create, update, and delete vouchers for organizers to use in reducing billing invoice prices. This includes a new 'Voucher' page and associated views and templates for managing voucher details and effects.

New Features:

  • Introduce a new 'Voucher' page in the admin area to manage vouchers for billing invoice deductions.

Enhancements:

  • Add functionality for administrators to create, update, and delete vouchers that can be used by organizers to reduce billing invoice prices.

Copy link

sourcery-ai bot commented Dec 12, 2024

Reviewer's Guide by Sourcery

This PR implements a new voucher management system in the admin area that allows administrators to create, update, and delete vouchers. The vouchers can be used by organizers to reduce billing invoice prices, with options to limit their usage to specific events or organizers. The implementation includes new model classes, views, forms, and templates to support the voucher management functionality.

Class diagram for the new InvoiceVoucher model

classDiagram
    class InvoiceVoucher {
        +String code
        +PositiveInteger max_usages
        +PositiveInteger redeemed
        +Decimal budget
        +DateTime valid_until
        +String price_mode
        +Decimal value
        +ManyToManyField limit_events
        +ManyToManyField limit_organizer
        +DateTime created_at
        +String created_by
        +DateTime updated_at
        +String updated_by
        +is_active() bool
    }
    class Event
    class Organizer
    InvoiceVoucher --> Event : limit_events
    InvoiceVoucher --> Organizer : limit_organizer
    note for InvoiceVoucher "Represents a voucher that can be used to reduce billing invoice prices."
Loading

File-Level Changes

Change Details Files
Implemented new InvoiceVoucher model with comprehensive voucher configuration options
  • Added fields for voucher code, usage limits, redemption tracking
  • Implemented price modification modes (set price, subtract amount, percentage discount)
  • Added support for budget limits and validity periods
  • Created relationships to limit vouchers to specific events or organizers
src/pretix/base/models/vouchers.py
src/pretix/base/migrations/0006_create_invoice_voucher.py
Created admin interface views for voucher management
  • Implemented list view for viewing all vouchers
  • Added create view for new vouchers
  • Created update view for modifying existing vouchers
  • Implemented delete view with validation for used vouchers
src/pretix/control/views/admin.py
Added form handling for voucher creation and modification
  • Created form class with fields for all voucher properties
  • Implemented validation logic for voucher settings
  • Added support for selecting multiple events and organizers
src/pretix/control/forms/admin/vouchers.py
Created templates for voucher management interface
  • Implemented list view template with sorting and pagination
  • Created detail template for voucher creation/editing
  • Added delete confirmation template
  • Created custom styling for voucher-related elements
src/pretix/control/templates/pretixcontrol/admin/vouchers/index.html
src/pretix/control/templates/pretixcontrol/admin/vouchers/detail.html
src/pretix/control/templates/pretixcontrol/admin/vouchers/delete.html
src/pretix/static/eventyay-common/scss/_voucher.scss
Updated navigation and URL routing for voucher management
  • Added voucher section to admin navigation menu
  • Created URL patterns for all voucher-related views
src/pretix/control/navigation.py
src/pretix/control/urls.py

Assessment against linked issues

Issue Objective Addressed Explanation
#382 Implement voucher functionality for a single event
#382 Implement voucher functionality for all events of an organizer

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@odkhang odkhang marked this pull request as ready for review December 17, 2024 10:48
Copy link

@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 @odkhang - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider removing redundant method overrides that only call super() (e.g. get(), post(), get_form_kwargs()) to reduce code duplication and improve maintainability.
Here's what I looked at during the review
  • 🟡 General issues: 2 issues 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 and I'll use the feedback to improve your reviews.

ctx = super().get_context_data(**kwargs)
return ctx

def get(self, request, *args, **kwargs):
Copy link

Choose a reason for hiding this comment

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

suggestion: Remove unnecessary method overrides that just call super()

Several methods in these views (get(), get_context_data(), get_form_kwargs(), form_valid() in VoucherCreate) just call super() without adding functionality. These can be removed to reduce code noise.

Suggested implementation:

If there are other methods in the file that just call super() without adding functionality (like get(), get_form_kwargs(), form_valid()), they should also be removed in the same way. However, I can't see them in the provided code snippet to make those specific changes.

form_class = InvoiceVoucherForm
return form_class

def get_object(self, queryset=None) -> InvoiceVoucherForm:
Copy link

Choose a reason for hiding this comment

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

issue: Fix incorrect return type annotation

The return type should be InvoiceVoucher, not InvoiceVoucherForm, as this method returns an InvoiceVoucher instance.

@@ -127,3 +132,113 @@
)

return HttpResponseRedirect(reverse('control:admin.task_management'))


class VoucherList(PaginationMixin, AdministratorPermissionRequiredMixin, ListView):
Copy link

Choose a reason for hiding this comment

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

issue (complexity): Consider creating a base class for voucher views to reduce code duplication and simplify the create/update views.

The code can be simplified while maintaining all functionality:

  1. Create a base class for shared voucher view logic:
class BaseVoucherView(AdministratorPermissionRequiredMixin):
    model = InvoiceVoucher
    template_name = 'pretixcontrol/admin/vouchers/detail.html'
    context_object_name = 'voucher'
    form_class = InvoiceVoucherForm

    def get_context_data(self, **kwargs):
        ctx = super().get_context_data(**kwargs)
        ctx['currency'] = settings.DEFAULT_CURRENCY
        return ctx

    def get_success_url(self) -> str:
        return reverse('control:admin.vouchers')
  1. Simplify the create/update views to only include unique behavior:
class VoucherCreate(BaseVoucherView, CreateView):
    pass

class VoucherUpdate(BaseVoucherView, UpdateView):
    def get_object(self, queryset=None) -> InvoiceVoucher:
        try:
            return InvoiceVoucher.objects.get(id=self.kwargs['voucher'])
        except InvoiceVoucher.DoesNotExist:
            raise Http404(_("The requested voucher does not exist."))

    def form_valid(self, form):
        messages.success(self.request, _('Your changes have been saved.'))
        return super().form_valid(form)

This removes empty method overrides and duplicated code while preserving all functionality. The base class makes the relationship between these views explicit and easier to maintain.

Copy link
Member

@hongquan hongquan left a comment

Choose a reason for hiding this comment

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

This PR has many do-nothing overrides. But they are minor.

self.fields['event_effect'].queryset = Event.objects.all()
self.fields['organizer_effect'].queryset = Organizer.objects.all()

def clean(self):
Copy link
Member

Choose a reason for hiding this comment

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

This override is unnecessary.

@mariobehling mariobehling merged commit 87dc76f into fossasia:development Dec 18, 2024
4 of 5 checks 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.

4 participants