diff --git a/testing.md b/testing.md new file mode 100644 index 0000000..6dad9b6 --- /dev/null +++ b/testing.md @@ -0,0 +1,16 @@ +# Platform Sub + +### Good one +tx_id: c50b4c2c2ca26691ac6205c67d72052a882aa0d71d71b18bdfe0dd887d8b75aa +tx_key: aaf3d3b81e57b2c874f0428a50428cf38ba5369c2ad06df7d4d7e7e7bb43e308 + +### Bad one +tx_id: 7fd5111343a0cc9c44705a03cddb4115027a2671855806f0a0dffddc7cc889ab +tx_key: c7f1c8266a6109ea547c7c5d04d226f68faa5f5a4cf1eb7ad6077cdb2bbb830e + + +# Content Sub + +tx_id: 1127feb964a2f35ea819454fc8dd17f23f374f829364704a7a51cbec676e09a5 +tx_key: 12ca27135e6dfc1bb5f2b6dc6334a84e3e9c09003db4204061a553a49452f505 +address: 75gXGUPpyqb1Hhxv6wSZ9218x1QBjBNo3dvYX6FnVMRPCWXoAX52DZnCkAe9LwjRjaGB1A6CGUr5c5hUTqdqywxN9jsutNP diff --git a/xmrbackers/filters.py b/xmrbackers/filters.py index 8f8744b..8e4c541 100644 --- a/xmrbackers/filters.py +++ b/xmrbackers/filters.py @@ -22,4 +22,4 @@ def xmr_block_explorer(v): @bp.app_template_filter('from_atomic') def from_atomic(amt): - return numbers.as_monero(numbers.from_atomic(amt)) + return numbers.from_atomic(amt) diff --git a/xmrbackers/forms.py b/xmrbackers/forms.py index f1600ea..679c4ba 100644 --- a/xmrbackers/forms.py +++ b/xmrbackers/forms.py @@ -25,16 +25,20 @@ class UserRegistration(FlaskForm): class UserLogin(FlaskForm): - handle = StringField('Handle:', validators=[DataRequired()], render_kw={"placeholder": "online handle", "class": "form-control", "type": "text"}) + handle = StringField('Handle:', validators=[DataRequired()], render_kw={'placeholder': 'online handle', 'class': 'form-control', 'type': 'text'}) class UserChallenge(FlaskForm): - signature = StringField('Signature:', validators=[DataRequired()], render_kw={"placeholder": "signed data", "class": "form-control", "type": "text"}) + signature = StringField('Signature:', validators=[DataRequired()], render_kw={'placeholder': 'signed data', 'class': 'form-control', 'type': 'text'}) + +class ConfirmPlatformSubscription(FlaskForm): + tx_id = StringField('TX ID:', validators=[DataRequired()], render_kw={'placeholder': 'TX ID', 'class': 'form-control', 'type': 'text'}) + tx_key = StringField('TX Key:', validators=[DataRequired()], render_kw={'placeholder': 'TX Key', 'class': 'form-control', 'type': 'text'}) -class ConfirmSubscription(FlaskForm): - tx_id = StringField('TX ID:', validators=[DataRequired()], render_kw={"placeholder": "TX ID", "class": "form-control", "type": "text"}) - tx_key = StringField('TX Key:', validators=[DataRequired()], render_kw={"placeholder": "TX Key", "class": "form-control", "type": "text"}) + +class ConfirmCreatorSubscription(ConfirmPlatformSubscription): + wallet_address = StringField('Wallet Address:', validators=[DataRequired(), is_valid_xmr_address], render_kw={'placeholder': 'monero wallet address', 'class': 'form-control', 'type': 'text'}) class CreateSubscription(FlaskForm): diff --git a/xmrbackers/models.py b/xmrbackers/models.py index 4d065db..2b2010b 100644 --- a/xmrbackers/models.py +++ b/xmrbackers/models.py @@ -16,6 +16,9 @@ db = pw.PostgresqlDatabase( host=config.DB_HOST, ) +def gen_challenge(): + return token_urlsafe().replace('-', '').replace('_', '') + @unique class UserRole(IntEnum): @@ -41,7 +44,7 @@ class User(pw.Model): handle = pw.CharField(unique=True) wallet_address = pw.CharField(unique=True) email = pw.CharField(unique=True, null=True) - challenge = pw.CharField(default=token_urlsafe) + challenge = pw.CharField(default=gen_challenge) roles: List[UserRole] = EnumArrayField(enum_class=UserRole, field_class=pw.IntegerField, default=[UserRole.backer]) @property @@ -63,10 +66,18 @@ class User(pw.Model): def get_id(self): return self.id - def generate_challenge(self): - self.challenge = token_urlsafe(24) + def regenerate_challenge(self): + self.challenge = gen_challenge() self.save() + def is_subscribed(self, subscription): + s = Subscription.select().where( + Subscription.backer == self + ).first() + if s: + return True + return False + class Meta: database = db diff --git a/xmrbackers/routes/auth.py b/xmrbackers/routes/auth.py index 58a71a0..b8c55cf 100644 --- a/xmrbackers/routes/auth.py +++ b/xmrbackers/routes/auth.py @@ -76,7 +76,7 @@ async def challenge(handle): try: res = make_wallet_rpc('verify', data) if res['good']: - user.generate_challenge() + user.regenerate_challenge() login_user(user) await flash('Successful login!') return redirect(url_for('main.index')) diff --git a/xmrbackers/routes/creator.py b/xmrbackers/routes/creator.py index 0d7c14a..ca00c4c 100644 --- a/xmrbackers/routes/creator.py +++ b/xmrbackers/routes/creator.py @@ -23,7 +23,7 @@ async def all(): @bp.route('/creators/join', methods=['GET', 'POST']) @login_required async def join(): - form = forms.ConfirmSubscription() + form = forms.ConfirmPlatformSubscription() valid_address = False try: @@ -102,23 +102,35 @@ async def join(): ) -@bp.route('/creator/') +@bp.route('/creator/', methods=['GET', 'POST']) async def show(handle): - creator = User.select().where( + form = forms.ConfirmCreatorSubscription() + creator = User.select().join(Profile, pw.JOIN.LEFT_OUTER).where( User.handle == handle, User.roles.contains_any(UserRole.creator) - ) + ).first() + if not creator: - await flash('That creator does not exist.') + await flash('That creator does not exist.', 'warning') return redirect(url_for('main.index')) + + if form.validate_on_submit(): + await flash('valid form submission', 'success') + posts = Content.select().where( Content.creator == creator, Content.hidden == False ).order_by(Content.post_date.desc()) + subscription = SubscriptionMeta.select().where( + SubscriptionMeta.user == creator + ).order_by(SubscriptionMeta.create_date.desc()).first() + return await render_template( 'creator/show.html', creator=creator, - posts=posts + subscription=subscription, + posts=posts, + form=form ) @@ -158,32 +170,11 @@ async def manage_subscriptions(): form=form ) - -# @bp.route('/creator//subscription') -# async def subscription(username): -# user = User.select().where(User.username == username) -# creator = CreatorProfile.select().where( -# CreatorProfile.user == user -# ) -# if creator: -# subscription_meta = SubscriptionMeta.select().where( -# SubscriptionMeta.creator == creator -# ).order_by(SubscriptionMeta.create_date.desc()).first() -# form = ConfirmSubscription() -# return await render_template( -# 'creator/subscription.html', -# subscription_meta=subscription_meta, -# form=form -# ) -# else: -# await flash('That creator does not exist.') -# return redirect(url_for('meta.index')) - # @bp.route('/subscription//confirm', methods=['POST']) # async def confirm_subscription(subscription_id): # # do checks here for SubscriptionMeta assumption # sm = SubscriptionMeta.get_or_none(subscription_id) -# form = ConfirmSubscription() +# form = ConfirmPlatformSubscription() # if form.validate_on_submit(): # w = Wallet( # port=8000, diff --git a/xmrbackers/templates/auth/challenge.html b/xmrbackers/templates/auth/challenge.html index 4b6c692..aa3468b 100644 --- a/xmrbackers/templates/auth/challenge.html +++ b/xmrbackers/templates/auth/challenge.html @@ -1,48 +1,11 @@ - - +{% extends 'includes/base.html' %} - {% include 'includes/head.html' %} +{% block content %} - -
+

Challenge

+

Handle: {{ user.handle }}

+

Challenge: {{ user.challenge }}

+

Wallet Address: {{ user.wallet_address }}

+{% include 'includes/form.html' %} - {% include 'includes/header.html' %} - - - - {% include 'includes/footer.html' %} - -
- - {% include 'includes/scripts.html' %} - - - +{% endblock %} diff --git a/xmrbackers/templates/auth/login.html b/xmrbackers/templates/auth/login.html index 4b7e4b3..a962ae5 100644 --- a/xmrbackers/templates/auth/login.html +++ b/xmrbackers/templates/auth/login.html @@ -1,45 +1,8 @@ - - +{% extends 'includes/base.html' %} - {% include 'includes/head.html' %} +{% block content %} - -
+

Login

+{% include 'includes/form.html' %} - {% include 'includes/header.html' %} - - - - {% include 'includes/footer.html' %} - -
- - {% include 'includes/scripts.html' %} - - - +{% endblock %} diff --git a/xmrbackers/templates/auth/register.html b/xmrbackers/templates/auth/register.html index 8bcccb7..f68216c 100644 --- a/xmrbackers/templates/auth/register.html +++ b/xmrbackers/templates/auth/register.html @@ -1,45 +1,8 @@ - - +{% extends 'includes/base.html' %} - {% include 'includes/head.html' %} +{% block content %} - -
+

Register

+{% include 'includes/form.html' %} - {% include 'includes/header.html' %} - - - - {% include 'includes/footer.html' %} - -
- - {% include 'includes/scripts.html' %} - - - +{% endblock %} diff --git a/xmrbackers/templates/creator/manage_subscriptions.html b/xmrbackers/templates/creator/manage_subscriptions.html index b5ad478..5507613 100644 --- a/xmrbackers/templates/creator/manage_subscriptions.html +++ b/xmrbackers/templates/creator/manage_subscriptions.html @@ -22,7 +22,6 @@

Content Subscriptions

{% if not content_subs %}

No subscriptions to your content yet.

- {% include 'includes/form.html' %} {% else %}
    {% for s in content_subs %} @@ -36,6 +35,7 @@ {% endfor %}
{% endif %} +{% include 'includes/form.html' %}

Subscribers

{% if not subscribers %} diff --git a/xmrbackers/templates/creator/show.html b/xmrbackers/templates/creator/show.html index 99d4256..f5d1db0 100644 --- a/xmrbackers/templates/creator/show.html +++ b/xmrbackers/templates/creator/show.html @@ -2,14 +2,37 @@ {% block content %} -

{{ creator.handle }}

-

Bio: {{ creator.bio }}

- - {% if posts %} -

Posts

- {% for post in posts %} -

{{ post.id }} - {{ post.title }} - {{ post.post_date | humanize }}

- {% endfor %} +

{{ creator.handle }}

+{% if creator.bio %}

Bio: {{ creator.bio }}

{% endif %} + +

Subscribe

+{% if subscription %} +

+ Send {{ subscription.atomic_xmr | from_atomic | round(2) }} XMR to + {{ subscription.wallet_address }} + and provide your TX ID and TX KEY to subscribe to this user + and view their content for the next {{ subscription.number_hours / 24 }} days + ({{ subscription.number_hours }} hours). +

+{% else %} +

This creator has not setup a subscription plan yet.

+{% endif %} + +{% if current_user.is_authenticated %} + {% if not current_user.is_subscribed(subscription) %} + {% include 'includes/form.html' %} + {% else %} + subscribed {% endif %} +{% else %} +

Login to subscribe.

+{% endif %} + +

Content

+{% if not posts %}

This creator has not posted any content yet.

{% endif %} +{% for post in posts %} +

{{ post.id }} - {{ post.title }} - {{ post.post_date | humanize }}

+{% endfor %} + {% endblock %} diff --git a/xmrbackers/templates/includes/base.html b/xmrbackers/templates/includes/base.html index 9a642d8..bee58c1 100644 --- a/xmrbackers/templates/includes/base.html +++ b/xmrbackers/templates/includes/base.html @@ -1,10 +1,19 @@ {% include 'includes/head.html' %} - + {% include 'includes/header.html' %} {% block content %}{% endblock %} {% include 'includes/debug.html' %} {% include 'includes/scripts.html' %} + diff --git a/xmrbackers/templates/includes/debug.html b/xmrbackers/templates/includes/debug.html index 98bc6b7..812cf15 100644 --- a/xmrbackers/templates/includes/debug.html +++ b/xmrbackers/templates/includes/debug.html @@ -1,11 +1,9 @@ -
- {% if config.DEBUG and current_user.is_authenticated %} +

Debug

- Authenticated: {{ current_user.is_authenticated }}
Username: {{ current_user.handle }}
- Email: {{ current_user.email }}
+ {% if current_user.email %}Email: {{ current_user.email }}
{% endif %} Wallet Address: {{ current_user.wallet_address }}

{% endif %}