@ -4,6 +4,7 @@ from typing import List
from secrets import token_urlsafe
import peewee as pw
from monero . numbers import to_atomic , from_atomic
from xmrbackers import config
from xmrbackers . helpers import EnumArrayField , EnumIntField
@ -28,12 +29,20 @@ class UserRole(IntEnum):
@unique
class Conten tType( IntEnum ) :
class Pos tType( IntEnum ) :
text = 0
gallery = 1
stream = 2
@unique
class ContentType ( IntEnum ) :
text = 0
image = 1
video = 2
stream = 3
class User ( pw . Model ) :
"""
User model is for base user management and reporting .
@ -78,6 +87,29 @@ class User(pw.Model):
return True
return False
def derive_subscription_fees ( self ) :
base_fee_atomic = to_atomic ( config . CREATOR_SUBSCRIPTION_FEE_XMR )
posts = Post . select ( ) . where ( Post . creator == self )
content = Content . select ( ) . join ( Post ) . where ( Post . creator == self )
content_base_fee_atomic = base_fee_atomic * config . CREATOR_CONTENT_FEE_PERCENT
content_fee_atomic = ( posts . count ( ) + content . count ( ) ) * content_base_fee_atomic
active_subs = Subscription . select ( ) . where (
Subscription . creator == self ,
Subscription . is_active == True
)
received_sub_xmr_atomic = 0
for sub in active_subs :
received_sub_xmr_atomic + = sub . meta . atomic_xmr
print ( ' User can expect to pay the following fees on their next reup: \n Base Fee: {} XMR \n Content Fees: {} ( {} posts, {} uploads) \n Subscriber Fees: {} ' . format (
config . CREATOR_SUBSCRIPTION_FEE_XMR ,
from_atomic ( content_fee_atomic ) ,
posts . count ( ) ,
content . count ( ) ,
from_atomic ( received_sub_xmr_atomic )
) )
class Meta :
database = db
@ -127,7 +159,6 @@ class CreatorSubscription(pw.Model):
atomic_xmr = pw . BigIntegerField ( )
term_hours = pw . IntegerField ( default = config . CREATOR_SUBSCRIPTION_TERM * 24 )
grace_hours = pw . IntegerField ( default = config . CREATOR_SUBSCRIPTION_GRACE * 24 )
delete_hours = pw . IntegerField ( default = 0 ) # delete me
@property
def grace_start_date ( self ) :
@ -188,19 +219,23 @@ class Subscription(pw.Model):
id = pw . AutoField ( )
subscribe_date = pw . DateTimeField ( default = datetime . utcnow )
tx = pw . ForeignKeyField ( Transaction )
active = pw . BooleanField ( default = True )
creator = pw . ForeignKeyField ( User )
backer = pw . ForeignKeyField ( User )
meta = pw . ForeignKeyField ( SubscriptionMeta )
@property
def is_active ( self ) :
end_date = self . subscribe_date + timedelta ( hours = self . meta . number_hours )
return end_date > datetime . utcnow ( )
class Meta :
database = db
class Content ( pw . Model ) :
class Pos t( pw . Model ) :
"""
Content model represents any uploaded content from a creator which is only
viewable by backers with an active subscription .
Post model represents a post from a creator consisting of Content objects
which is only viewable by backers with an active subscription .
"""
id = pw . AutoField ( )
post_date = pw . DateTimeField ( default = datetime . utcnow )
@ -210,6 +245,21 @@ class Content(pw.Model):
last_edit_date = pw . DateTimeField ( default = datetime . utcnow )
creator = pw . ForeignKeyField ( User )
type : List [ PostType ] = EnumIntField ( enum_class = PostType , default = PostType . text )
class Meta :
database = db
class Content ( pw . Model ) :
"""
Content model is any uploaded content from a creator .
"""
id = pw . AutoField ( )
location = pw . CharField ( )
upload_date = pw . DateTimeField ( default = datetime . utcnow )
post = pw . ForeignKeyField ( Post )
type : List [ ContentType ] = EnumIntField ( enum_class = ContentType , default = ContentType . text )
class Meta :