Skip to content

Commit

Permalink
filtering the dasboard to the corresponding groups (#1108)
Browse files Browse the repository at this point in the history
* filtering the dasboard to the corresponding groups

* wip #1103 separating donation from subscriptions and one pay of in the subscribers page

* adding show donors filtering button in subscribers page
  • Loading branch information
joeltejeda authored Apr 12, 2023
1 parent d50b602 commit 9afcd49
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 9 deletions.
20 changes: 16 additions & 4 deletions subscribie/blueprints/admin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
get_number_of_subscribers,
get_number_of_signups,
get_number_of_one_off_purchases,
get_number_of_transactions_with_donations,
)

import stripe
Expand Down Expand Up @@ -461,7 +462,7 @@ def dashboard():
num_subscribers = get_number_of_subscribers()
num_signups = get_number_of_signups()
num_one_off_purchases = get_number_of_one_off_purchases()

num_donations = get_number_of_transactions_with_donations()
shop_default_country_code = get_shop_default_country_code()
saas_url = current_app.config.get("SAAS_URL")
if Setting.query.first().donations_enabled is True:
Expand All @@ -481,6 +482,7 @@ def dashboard():
num_active_subscribers=num_active_subscribers,
num_subscribers=num_subscribers,
num_signups=num_signups,
num_donations=num_donations,
num_one_off_purchases=num_one_off_purchases,
shop_default_country_code=shop_default_country_code,
saas_url=saas_url,
Expand Down Expand Up @@ -1233,17 +1235,27 @@ def subscribers():
query = query.where(Person.email.like(f"%{subscriber_email}%"))
if subscriber_name:
query = query.where(Person.full_name.like(f"%{subscriber_name}%"))

if show_active:
if action == "show_active":
query = query.filter(Person.subscriptions)
query = query.where(
(Subscription.stripe_status == "active")
| (Subscription.stripe_status == "trialing")
)
elif action == "show_donors":
query = query.filter(Person.transactions)
query = query.where(Transaction.is_donation == True)

elif action == "show_one_off_payments":
query = query.filter(Person.subscriptions)
query = query.where(Subscription.stripe_subscription_id == None)

people = query.order_by(desc(Person.created_at))

return render_template(
"admin/subscribers.html", people=people.all(), show_active=show_active
"admin/subscribers.html",
people=people.all(),
show_active=show_active,
action=action,
)


Expand Down
12 changes: 11 additions & 1 deletion subscribie/blueprints/admin/stats.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from subscribie.database import database
from subscribie.models import Person, Subscription, Plan, PlanRequirements
from subscribie.models import Person, Subscription, Plan, PlanRequirements, Transaction
from sqlalchemy.sql import func
import logging

Expand Down Expand Up @@ -80,3 +80,13 @@ def get_number_of_one_off_purchases():
.count()
)
return count


def get_number_of_transactions_with_donations():
count = (
database.session.query(Person)
.join(Transaction)
.filter(Transaction.is_donation == 1)
.count()
)
return count
9 changes: 6 additions & 3 deletions subscribie/blueprints/admin/templates/admin/dashboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,13 @@ <h3 class="card-title justify-content-between d-flex">Stats
<span class="badge badge-primary badge-pill" > Donations:{{ total_donations }} </span>
{% endif %}
</h3>
<p>You have: {{ num_active_subscribers }} <a href="{{ url_for('admin.subscribers') }}">subscribers</a> with <em>active</em> subscriptions.</p>
<p>You have: {{ num_active_subscribers }} <a href="{{ url_for('admin.subscribers', action='show_active')}}">subscribers</a> with <em>active</em> subscriptions.</p>
<p>You've had: {{ num_subscribers }} <a href="{{ url_for('admin.subscribers') }}">subscribers</a> since starting your shop.</p>
<p>You've had: {{ num_signups }} <a href="{{ url_for('admin.subscribers') }}">people</a> either buy one-off or start a subscription since starting your shop.</p>
<p>You've had: {{ num_one_off_purchases }} <a href="{{ url_for('admin.subscribers') }}">people</a> buy a one-off item from your shop.</p>
<p>You've had: {{ num_one_off_purchases }} <a href="{{ url_for('admin.subscribers', action='show_one_off_payments')}}">people</a> buy a one-off item from your shop.</p>
{% if settings.donations_enabled %}
<p>You've had: {{ num_donations }} <a href="{{ url_for('admin.subscribers', action='show_donors')}}">Donors</a> Donations to your shop.</p>
{% endif %}
<p>You've had: {{ num_signups }} <a href="{{ url_for('admin.subscribers') }}">people</a> either buy one-off or start a subscription{% if settings.donations_enabled %} or donations {% endif %} since starting your shop.</p>
<p><a href="/" class="btn btn-success mb-2 mt-2" style="color: white" target="_blank">View my Shop</a></p>
</div>

Expand Down
42 changes: 41 additions & 1 deletion subscribie/blueprints/admin/templates/admin/subscribers.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ <h2 class="text-center text-dark mb-3">My Subscribers</h2>
{% else %}
<a href="{{url_for('admin.subscribers', action='show_active')}}" id="show-active-subscribers" class="btn btn-primary ">Show Active</a>
{%endif %}
<a href="{{url_for('admin.archived_subscribers')}}" class="btn btn-primary">Show Archived</a>
{% if settings.donations_enabled %}
{% if action == "show_donors" %}
<a href="{{url_for('admin.subscribers')}}" id="show-active-subscribers" class="btn btn-primary active">Show all</a>
{% else %}
<a href="{{url_for('admin.subscribers', action='show_donors')}}" id="show-donors" class="btn btn-primary ">Show Donors</a>
{% endif %}
{% endif %}
<button id="refresh_subscriptions" title="Get latest subscription statuses (active/paused/inactive)" class="btn btn-primary disable-on-click">Refresh Subscriptions</button>
</div>
<form action="#" method="GET">
Expand Down Expand Up @@ -201,6 +207,40 @@ <h4>Search...</h4>
</li>
{% endfor %}
</ul>
{% else %}
{# Donations are not in the subscription table since they are not plans nor subscriptions #}
{# therefore we need to look at the transction table to find the donations #}
{% for transaction in person.transactions %}
{% if transaction.is_donation %}
<div class="card">
<ul class="list-unstyled px-2">
<li><strong>Title: </strong>
<span class="donation-title">Donation</span>
</li>
<li><strong>Transaction ID: </strong>{{ transaction.uuid }}</li>
<li><strong>Date: </strong>{{ transaction.created_at.strftime('%Y-%m-%d') }}</li>
<li>
<strong>Price: </strong>
<span class="donation_amount">{{ transaction.showSellPrice() }}</span>
</li>
</span>
<li><strong>Status:</strong>
<span class="transaction-status">{{ transaction.payment_status }}</span>
</li>
<li>
{% if transaction.comment %}
<details open>
<summary><strong>Donation Note</strong></summary>
{{ transaction.comment }}
{% else %}
No note was given.
</details>
{% endif %}
</li>
</ul>
</div>
{% endif %}
{% endfor %}
{% endif %}
</td>
</tr>
Expand Down
6 changes: 6 additions & 0 deletions subscribie/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,12 @@ class Transaction(database.Model):
fulfillment_state = database.Column(database.String())
is_donation = database.Column(database.Boolean(), default=0)

def showSellPrice(self) -> str:
currency_symbol = get_geo_currency_symbol()
amount = self.amount / 100
result = f"{currency_symbol}{amount:.2f}"
return result


class SeoPageTitle(database.Model):
__tablename__ = "module_seo_page_title"
Expand Down

0 comments on commit 9afcd49

Please sign in to comment.