Exemplo n.º 1
0
class YeFengWordTable(BaseModel):
    id = IntegerField(primary_key=True)
    word = CharField()
    py = CharField()
    priority = IntegerField()
    updatedt = DateTimeField("%Y-%m-%d %H:%M:%S")
Exemplo n.º 2
0
class Shelf(BaseModel):
    READ = 'read'
    CURENTLY_READ = 'currently read'
    WANT_TO_READ = 'want to read'
    name = CharField(max_length=32)
    user = ForeignKeyField(User, backref='shelves')
Exemplo n.º 3
0
class Superblock(BaseModel, GovernanceClass):
    governance_object = ForeignKeyField(GovernanceObject,
                                        related_name='superblocks',
                                        on_delete='CASCADE',
                                        on_update='CASCADE')
    event_block_height = IntegerField()
    payment_addresses = TextField()
    payment_amounts = TextField()
    proposal_hashes = TextField(default='')
    sb_hash = CharField()
    object_hash = CharField(max_length=64)

    govobj_type = DASHD_GOVOBJ_TYPES['superblock']
    only_masternode_can_submit = True

    class Meta:
        db_table = 'superblocks'

    def is_valid(self):
        import dashlib
        import decimal

        printdbg("In Superblock#is_valid, for SB: %s" % self.__dict__)

        # it's a string from the DB...
        addresses = self.payment_addresses.split('|')
        for addr in addresses:
            if not dashlib.is_valid_address(addr, config.network):
                printdbg("\tInvalid address [%s], returning False" % addr)
                return False

        amounts = self.payment_amounts.split('|')
        for amt in amounts:
            if not misc.is_numeric(amt):
                printdbg("\tAmount [%s] is not numeric, returning False" % amt)
                return False

            # no negative or zero amounts allowed
            damt = decimal.Decimal(amt)
            if not damt > 0:
                printdbg("\tAmount [%s] is zero or negative, returning False" %
                         damt)
                return False

        # verify proposal hashes correctly formatted...
        if len(self.proposal_hashes) > 0:
            hashes = self.proposal_hashes.split('|')
            for object_hash in hashes:
                if not misc.is_hash(object_hash):
                    printdbg("\tInvalid proposal hash [%s], returning False" %
                             object_hash)
                    return False

        # ensure number of payment addresses matches number of payments
        if len(addresses) != len(amounts):
            printdbg(
                "\tNumber of payment addresses [%s] != number of payment amounts [%s], returning False"
                % (len(addresses), len(amounts)))
            return False

        printdbg("Leaving Superblock#is_valid, Valid = True")
        return True

    def is_deletable(self):
        # end_date < (current_date - 30 days)
        # TBD (item moved to external storage/DashDrive, etc.)
        pass

    def hash(self):
        import dashlib
        return dashlib.hashit(self.serialise())

    def hex_hash(self):
        return "%x" % self.hash()

    # workaround for now, b/c we must uniquely ID a superblock with the hash,
    # in case of differing superblocks
    #
    # this prevents sb_hash from being added to the serialised fields
    @classmethod
    def serialisable_fields(self):
        return [
            'event_block_height', 'payment_addresses', 'payment_amounts',
            'proposal_hashes'
        ]

    # has this masternode voted to fund *any* superblocks at the given
    # event_block_height?
    @classmethod
    def is_voted_funding(self, ebh):
        count = (
            self.select().where(self.event_block_height == ebh).join(
                GovernanceObject).join(Vote).join(Signal).switch(
                    Vote)  # switch join query context back to Vote
            .join(Outcome).where(Vote.signal == VoteSignals.funding).where(
                Vote.outcome == VoteOutcomes.yes).count())
        return count

    @classmethod
    def latest(self):
        try:
            obj = self.select().order_by(
                self.event_block_height).desc().limit(1)[0]
        except IndexError as e:
            obj = None
        return obj

    @classmethod
    def at_height(self, ebh):
        query = (self.select().where(self.event_block_height == ebh))
        return query

    @classmethod
    def find_highest_deterministic(self, sb_hash):
        # highest block hash wins
        query = (self.select().where(self.sb_hash == sb_hash).order_by(
            self.object_hash.desc()))
        try:
            obj = query.limit(1)[0]
        except IndexError as e:
            obj = None
        return obj
Exemplo n.º 4
0
class User(BasicModel):
    """
    Пользователь
    """
    login = CharField(unique=True)
    email = CharField(null=True, unique=True)
    phone = CharField(null=True)
    password_hash = CharField(null=True)
    first_name = CharField(default="")
    last_name = CharField(null=True)
    role = CharField(null=True, default="user")
    picture = JSONField(null=True)
    status = CharField(default="unconfirmed")
    code = CharField(null=True)
    rating = DoubleField(default=0)
    reg_method = CharField(null=True)
    registration_date = DateTimeField(null=True)
    money = IntegerField(default=0)
    lang = CharField(default="en")
    timezone = CharField(default="UTC")
    utm_source = CharField(max_length=1000, null=True)
    utm_medium = CharField(max_length=1000, null=True)
    utm_campaign = CharField(max_length=1000, null=True)

    def __str__(self):
        return "{id}: {first_name} {last_name} ({pair})".format(**self.dict())
Exemplo n.º 5
0
class Book(BaseModel):
    isbn = CharField(max_length=32)
    name = CharField(max_length=255)
Exemplo n.º 6
0
class Result(BaseModel):
    id = CharField(primary_key=True)
    raceid = CharField(null=True)
    racetype = CharField(null=True)
    racetypeid = CharField(null=True)
    ballotorder = IntegerField(null=True)
    candidateid = CharField(null=True)
    description = CharField(null=True)
    delegatecount = IntegerField(null=True)
    electiondate = DateField(null=True)
    electtotal = IntegerField(null=True)
    electwon = IntegerField(null=True)
    fipscode = CharField(max_length=5, null=True)
    first = CharField(null=True)
    incumbent = BooleanField(null=True)
    initialization_data = BooleanField(null=True)
    is_ballot_measure = BooleanField(null=True)
    last = CharField(null=True)
    lastupdated = DateTimeField(null=True)
    level = CharField(null=True)
    national = BooleanField(null=True)
    officeid = CharField(null=True)
    officename = CharField(null=True)
    party = CharField(null=True)
    polid = CharField(null=True)
    polnum = CharField(null=True)
    precinctsreporting = IntegerField(null=True)
    precinctsreportingpct = DecimalField(null=True)
    precinctstotal = IntegerField(null=True)
    reportingunitid = CharField(null=True)
    reportingunitname = CharField(null=True)
    runoff = BooleanField(null=True)
    seatname = CharField(null=True)
    seatnum = CharField(null=True)
    statename = CharField(null=True)
    statepostal = CharField(max_length=2)
    test = BooleanField(null=True)
    uncontested = BooleanField(null=True)
    votecount = IntegerField(null=True)
    votepct = DecimalField(null=True)
    winner = BooleanField(null=True)

    def is_npr_winner(self):
        if self.level == 'district':
            if (self.electwon > 0 and
                    self.call[0].accept_ap) or self.call[0].override_winner:
                return True
            else:
                return False

        if (self.winner
                and self.call[0].accept_ap) or self.call[0].override_winner:
            return True
        else:
            return False

    def is_pickup(self):
        if self.is_npr_winner() and self.party != self.meta[0].current_party:
            return True
        else:
            return False

    def is_expected(self):
        if self.is_npr_winner() and self.party == self.meta[0].expected:
            return True
        else:
            return False

    def is_not_expected(self):
        if self.is_npr_winner():
            if self.meta[0].expected == 'Dem' and self.party != 'Dem':
                return True
            if self.meta[0].expected == 'GOP' and self.party != 'GOP':
                return True
            else:
                return False
        else:
            return False
Exemplo n.º 7
0
 class Role(RoleMixin, db.Model):
     name = CharField(unique=True, max_length=80)
     description = TextField(null=True)
     permissions = TextField(null=True)
Exemplo n.º 8
0
class Player(DatabaseModel):
    table_name = 'player'

    defaults = {
        'username':
        ''.join(random.choice(string.ascii_letters) for _ in range(8)),
        'password':
        ''.join(
            random.choice(string.ascii_letters + string.digits)
            for _ in range(10)),
        'join_time':
        int(time.time()),
        'avatar_src':
        '/static/img/cookie.svg',
        'clicks':
        0
    }

    username = CharField(primary_key=True)
    password = CharField()
    join_time = BigIntegerField()
    avatar_src = CharField()
    clicks = BigIntegerField()

    def __repr__(self):
        return ', '.join((f'{name}: {self.__getattribute__(name)}'
                          for name in self.get_attributes()))

    def __str__(self):
        return self.__repr__()

    def get_attributes(self):
        return self._meta.sorted_field_names

    @staticmethod
    def get_player(username):
        player = list(Player.select().where(Player.username == username))
        if len(player) == 1:
            return player[0]

    @staticmethod
    def get_player_if_auth(username, password):
        player = Player.get_player(username)
        if player and player.username == username and player.password == password:
            return player

    @staticmethod
    def create_player(**player):
        p = {**Player.defaults}
        for key, value in player.items():
            p[key] = value
        status = False
        try:
            return Player.create(**p)
        except peewee.IntegrityError:
            status = 'already exists'
        finally:
            print(
                f'Player with username {player.get("username")} {status if status else "created"}'
            )

    @staticmethod
    def list():
        return list(Player.select())

    @staticmethod
    def set_player(player=None, **attrs):
        """
        Set a set of attributes from kwargs to player
        :param player: A username which is looked up in the db or a Player
        :param attrs: The attributes
        """
        if isinstance(player, str):
            player = Player.get_player(player)
        for attr in player.get_attributes():
            if attrs.get(attr, False):
                player.__setattr__(attr, attrs[attr])
        player.save()
Exemplo n.º 9
0
class Location_Type(BaseModel):
    id = PrimaryKeyField()
    uuid = UUIDField(constraints=[SQL('DEFAULT uuid_generate_v4()')],
                     unique=True)
    type = CharField()
Exemplo n.º 10
0
class QueueItemModel(BaseQueueModel):
    scan_path = CharField(max_length=256, unique=True, null=False)
    scan_for = CharField(max_length=64, null=False)
    scan_section = IntegerField(null=False)
    scan_type = CharField(max_length=64, null=False)
Exemplo n.º 11
0
class User(BaseModel):
    username = CharField(unique=True, primary_key=True)
Exemplo n.º 12
0
 class Plugin(BaseModel):
     id = AutoField()
     name = CharField(unique=True)
     version = CharField()
Exemplo n.º 13
0
class TableVersion(BaseModel):
    """table to keep track of table versions"""

    name = CharField(max_length=60, index=True)
    version = IntegerField(default=0)
import yaml
import os
from peewee import (
    PostgresqlDatabase,
    Model,
    CharField,
)
from playhouse.migrate import (
    PostgresqlMigrator,
    migrate,
)

with open("config.yaml") as cfg_file:
    cfg = yaml.load(cfg_file, Loader=yaml.Loader).get(os.environ['ENV'])
    db_cfg = cfg.get('postgres')

pg_db = PostgresqlDatabase(db_cfg.get('name'),
                           user=db_cfg.get('user'),
                           password=db_cfg.get('password'),
                           host=db_cfg.get('host'),
                           port=db_cfg.get('port'))

migrator = PostgresqlMigrator(pg_db)

migrate(migrator.add_column('students', 'state', CharField(default='TEST')))
Exemplo n.º 15
0
class UserProfile(BaseModel):
    id = IntegerField(primary_key=True)
    ap_id = CharField(unique=True)
    name = CharField(null=True)  # Display name
    disabled = BooleanField(
        default=False)  # True if the user is disabled in the server
    is_remote = BooleanField(default=False)  # The user is a remote user
    private_key = TextField(null=True)  # Private key used to sign AP actions
    public_key = TextField(null=True)  # Public key
    description = TextField(default="")  # Description of the profile
    avatar_file = CharField(null=True)
    following_count = IntegerField(default=0)
    followers_count = IntegerField(default=0)
    statuses_count = IntegerField(default=0)
    user = ForeignKeyField(User, backref='profile', on_delete='CASCADE')
    public_inbox = CharField(max_length=255, null=True)

    @property
    def username(self):
        return self.user.username

    @property
    def is_private(self):
        return self.user.is_private

    def save(self, *args, **kwargs):
        if not self.is_remote:
            self.ap_id = uri("user", {"username": self.username})

        if not self.id:
            self.id = self.user.id

        if not self.avatar_file:
            pixel_avatar = PixelAvatar(rows=10, columns=10)
            image_byte_array = pixel_avatar.get_image(size=400,
                                                      string=self.ap_id,
                                                      filetype="jpeg")

            self.avatar_file = self._crate_avatar_file(image_byte_array)

        return super(UserProfile, self).save(*args, **kwargs)

    @property
    def uris(self):
        if self.is_remote:
            return URIs(
                id=self.ap_id,
                inbox=f'{self.ap_id}/inbox',
                outbox=f'{self.ap_id}/inbox',
                following=f'{self.ap_id}/following',
                followers=f'{self.ap_id}/followers',
            )

        return URIs(id=uri("user", {"username": self.username}),
                    following=uri("following", {"username": self.username}),
                    followers=uri("followers", {"username": self.username}),
                    outbox=uri("outbox", {"username": self.username}),
                    inbox=uri("inbox", {"username": self.username}),
                    atom=uri("atom", {"id": self.id}),
                    featured=uri("featured", {"username": self.username}),
                    avatar=uri('profile_image', {"name": self.avatar_file}),
                    client=uri('user_client', {'username': self.username}))

    def to_json(self):
        json = {
            'id': self.id,
            'username': self.username,
            'name': self.name,
            'display_name': self.name,
            'locked': self.is_private,
            'created_at': self.user.created_at,
            'followers_count': self.followers_count,
            'following_count': self.following_count,
            'statuses_count': self.statuses_count,
            'note': self.description,
            'url': None,
            'avatar': self.avatar,
            'moved': None,
            'fields': [],
            'bot': self.user.is_bot,
        }

        if self.is_remote:
            json.update({'acct': self.ap_id})

        else:
            json.update({'acct': self.username})

        return json

    def to_model(self):
        return self.to_json()

    def to_activitystream(self):
        json = {
            "@context": [
                "https://www.w3.org/ns/activitystreams",
                "https://w3id.org/security/v1",
            ],
            "type":
            "Person",
            "id":
            self.uris.id,
            "name":
            self.name,
            "preferredUsername":
            self.username,
        }

        if not self.is_remote:
            json.update({
                "following": self.uris.following,
                "followers": self.uris.followers,
                "outbox": self.uris.outbox,
                "inbox": self.uris.inbox,
                "publicKey": {
                    'publicKeyPem': import_keys()["actorKeys"]["publicKey"],
                    'id': f'{BASE_URL}/users/{self.username}#main-key',
                    'owner': f'{BASE_URL}/users/{self.username}'
                },
                "summary": self.description,
                "manuallyApprovesFollowers": self.is_private,
                "featured": self.uris.featured,
                "endpoints": {
                    "sharedInbox": uri('sharedInbox')
                }
            })

        return json

    def _create_avatar_id(self):
        hashid = Hashids(salt=salt_code, min_length=6)

        possible_id = self.id + int(
            (datetime.datetime.now() -
             datetime.datetime(1970, 1, 1)).total_seconds())
        return hashid.encode(possible_id)

    def _crate_avatar_file(self, image):
        """
        image - A byte array with the image
        """

        filename = self._create_avatar_id()
        image = io.BytesIO(image)
        im = Image.open(image)
        im = im.convert('RGB')
        im.thumbnail((400, 400), Image.ANTIALIAS)
        file_path = os.path.join(MEDIA_FOLDER, 'avatars', filename + '.jpeg')
        im.save(file_path, 'jpeg')

        return f'{filename}.jpeg'

    def update_avatar(self, image):
        return self._crate_avatar_file(image)

    @property
    def avatar(self):
        return uri("profile_image", {"name": self.avatar_file})

    def followers(self):
        from models.followers import FollowerRelation

        return (UserProfile.select().join(
            FollowerRelation, on=FollowerRelation.user).where(
                FollowerRelation.follows == self).order_by(
                    FollowerRelation.created_at.desc()))

    def timeline(self):
        from models.status import Status
        return self.statuses.order_by(Status.id.desc())

    def following(self):
        from models.followers import FollowerRelation

        return (UserProfile.select().join(
            FollowerRelation, on=FollowerRelation.follows).where(
                FollowerRelation.user == self).order_by(
                    FollowerRelation.created_at.desc()))

    def is_following(self, user):
        from models.followers import FollowerRelation

        return (FollowerRelation.select().where(
            (FollowerRelation.user == self)
            & (FollowerRelation.follows == user)).exists())

    def liked(self):
        return self.liked_posts

    def follow(self, target, valid=False):
        """
        The current user follows the target account. 
        
        target: An instance of User
        valid: Boolean to force a valid Follow. This means that the user
                doesn't have to accept the follow
        """

        from models.followers import FollowerRelation
        if not FollowerRelation.get_or_none(user=self, follows=target):
            FollowerRelation.create(user=self, follows=target, valid=valid)
            followers_increment = UserProfile.update({
                UserProfile.followers_count:
                UserProfile.followers_count + 1
            }).where(UserProfile.id == target.id)
            following_increment = UserProfile.update({
                UserProfile.following_count:
                UserProfile.following_count + 1
            }).where(UserProfile.id == self.id)

            following_increment.execute()
            followers_increment.execute()

    def unfollow(self, target, valid=False):
        """
        The current user follows the target account. 
        
        target: An instance of User
        valid: Boolean to force a valid Follow. This means that the user
                doesn't have to accept the follow
        """

        from models.followers import FollowerRelation
        t = FollowerRelation.get_or_none(user=self, follows=target)
        if t:
            t.delete_instance()
            followers_increment = UserProfile.update({
                UserProfile.followers_count:
                UserProfile.followers_count - 1
            }).where(UserProfile.id == target.id)
            following_increment = UserProfile.update({
                UserProfile.following_count:
                UserProfile.following_count - 1
            }).where(UserProfile.id == self.id)

            following_increment.execute()
            followers_increment.execute()
Exemplo n.º 16
0
class Question(Model):
    question_text = CharField()
    pub_date = DateField()

    class Meta:
        database = db
Exemplo n.º 17
0
class User(Model):
    id: int = PrimaryKeyField()
    state: str = CharField(max_length=25, default='')
    lang_translate: Language = ForeignKeyField(Language)
    lang_wiki: Language = ForeignKeyField(Language)
    wolfram_free: int = IntegerField(default=WOLFRAM_LIM)
    translate_free: int = IntegerField(default=TRANSLATE_LIM)

    def reduce(self, attr: str, quantity: int):
        setattr(self, attr+'_free', getattr(self, attr+'_free') - quantity)
        self.save()

    def reset_state(self):
        self.state = ''
        self.save()

    def set_state(self, state):
        self.state = state
        self.save()

    def set_lang(self, item: str, lang: Language):
        setattr(self, f'lang_{item}', lang)
        self.save()

    def reduce_wolfram(self):
        self.wolfram_free -= 1
        self.save()

    def reduce_translate(self, count: int):
        self.translate_free -= count
        self.save()

    @property
    def lang_wiki_markup(self):
        markup = InlineKeyboardMarkup()
        markup.row(InlineKeyboardButton(text=self.get_lang_text('ru'),
                                        callback_data='ru'),
                   InlineKeyboardButton(text=self.get_lang_text('en'),
                                        callback_data='en'))
        markup.row(InlineKeyboardButton(text=self.get_lang_text('de'),
                                        callback_data='de'),
                   InlineKeyboardButton(text=self.get_lang_text('fr'),
                                        callback_data='fr'))
        markup.row(InlineKeyboardButton(text=buttons.back,
                                        callback_data='profile'))
        return markup

    def get_lang_text(self, ui):
        return f'{own_langs[ui]} ✅' if self.lang_wiki.ui == ui else own_langs[ui]

    @property
    def info(self):
        return texts.profile_0.format(
            user=self, wolfram_lim=WOLFRAM_LIM, tranlator_lim=TRANSLATE_LIM)

    def reset_limits(self):
        limits = ['wolfram', 'translate']
        for lim in limits:
            setattr(self, lim+'_free', getattr(config, lim.upper()+'_LIM'))
        self.save()

    class Meta:
        database = db
        db_table = 'users'
Exemplo n.º 18
0
class Person(BaseModel):
    crud_config = config

    name = CharField()
    email = CharField()
    create_datetime = DateTimeField(default=datetime.datetime.now, null=True)
Exemplo n.º 19
0
class CfgNotify(BaseModel):
    check_order = IntegerField()  # Sort
    notify_type = CharField()  # Notification Type:MAIL/SMS
    notify_name = CharField()  # Notifier Name
    notify_number = CharField()  # Notification Number
    status = BooleanField(default=True)  # Validation Mark
Exemplo n.º 20
0
    class Journal(Model):
        timestamp = DateTimeField(default=datetime.now)
        level = SmallIntegerField(index=True)
        text = CharField(max_length=255, index=True)

        col_float1 = FloatField(default=2.2)
        col_smallint1 = SmallIntegerField(default=2)
        col_int1 = IntegerField(default=2000000)
        col_bigint1 = BigIntegerField(default=99999999)
        col_char1 = CharField(max_length=255, default="value1")
        col_text1 = TextField(
            default="Moo,Foo,Baa,Waa,Moo,Foo,Baa,Waa,Moo,Foo,Baa,Waa")
        col_decimal1 = DecimalField(12, 8, default=Decimal("2.2"))
        col_json1 = JSONField(default={
            "a": 1,
            "b": "b",
            "c": [2],
            "d": {
                "e": 3
            },
            "f": True
        })

        col_float2 = FloatField(null=True)
        col_smallint2 = SmallIntegerField(null=True)
        col_int2 = IntegerField(null=True)
        col_bigint2 = BigIntegerField(null=True)
        col_char2 = CharField(max_length=255, null=True)
        col_text2 = TextField(null=True)
        col_decimal2 = DecimalField(12, 8, null=True)
        col_json2 = JSONField(null=True)

        col_float3 = FloatField(default=2.2)
        col_smallint3 = SmallIntegerField(default=2)
        col_int3 = IntegerField(default=2000000)
        col_bigint3 = BigIntegerField(default=99999999)
        col_char3 = CharField(max_length=255, default="value1")
        col_text3 = TextField(
            default="Moo,Foo,Baa,Waa,Moo,Foo,Baa,Waa,Moo,Foo,Baa,Waa")
        col_decimal3 = DecimalField(12, 8, default=Decimal("2.2"))
        col_json3 = JSONField(default={
            "a": 1,
            "b": "b",
            "c": [2],
            "d": {
                "e": 3
            },
            "f": True
        })

        col_float4 = FloatField(null=True)
        col_smallint4 = SmallIntegerField(null=True)
        col_int4 = IntegerField(null=True)
        col_bigint4 = BigIntegerField(null=True)
        col_char4 = CharField(max_length=255, null=True)
        col_text4 = TextField(null=True)
        col_decimal4 = DecimalField(12, 8, null=True)
        col_json4 = JSONField(null=True)

        class Meta:
            database = db
Exemplo n.º 21
0
class Migrations(BasicModel):
    name = CharField()
    time = DateTimeField(default=SQL("NOW()"))
Exemplo n.º 22
0
class Group(BaseModel):
    group_name = CharField(unique=True)
Exemplo n.º 23
0
class User(Model):
    name = CharField(max_length=255, unique=True)
    password = CharField(max_length=255)

    class Meta:
        database = db
Exemplo n.º 24
0
class User(BaseModel):
    telegram_id = CharField(unique=True)
    first_name = CharField()
    last_name = CharField()
    status = CharField()
Exemplo n.º 25
0
class Author(BaseModel):
    name = CharField(max_length=32)
Exemplo n.º 26
0
class Movement(BaseModel):
    equipment = ForeignKeyField(Equipment, backref="movements")
    campus = CharField()
    room = CharField()
Exemplo n.º 27
0
class Proposal(GovernanceClass, BaseModel):
    governance_object = ForeignKeyField(GovernanceObject,
                                        related_name='proposals',
                                        on_delete='CASCADE',
                                        on_update='CASCADE')
    name = CharField(default='', max_length=40)
    url = CharField(default='')
    start_epoch = IntegerField()
    end_epoch = IntegerField()
    payment_address = CharField(max_length=36)
    payment_amount = DecimalField(max_digits=16, decimal_places=8)
    object_hash = CharField(max_length=64)

    govobj_type = DASHD_GOVOBJ_TYPES['proposal']

    class Meta:
        db_table = 'proposals'

    def is_valid(self):
        import dashlib

        printdbg("In Proposal#is_valid, for Proposal: %s" % self.__dict__)

        try:
            # proposal name exists and is not null/whitespace
            if (len(self.name.strip()) == 0):
                printdbg("\tInvalid Proposal name [%s], returning False" %
                         self.name)
                return False

            # proposal name is normalized (something like "[a-zA-Z0-9-_]+")
            if not re.match(r'^[-_a-zA-Z0-9]+$', self.name):
                printdbg(
                    "\tInvalid Proposal name [%s] (does not match regex), returning False"
                    % self.name)
                return False

            # end date < start date
            if (self.end_epoch <= self.start_epoch):
                printdbg(
                    "\tProposal end_epoch [%s] <= start_epoch [%s] , returning False"
                    % (self.end_epoch, self.start_epoch))
                return False

            # amount must be numeric
            if misc.is_numeric(self.payment_amount) is False:
                printdbg(
                    "\tProposal amount [%s] is not valid, returning False" %
                    self.payment_amount)
                return False

            # amount can't be negative or 0
            if (float(self.payment_amount) <= 0):
                printdbg(
                    "\tProposal amount [%s] is negative or zero, returning False"
                    % self.payment_amount)
                return False

            # payment address is valid base58 dash addr, non-multisig
            if not dashlib.is_valid_address(self.payment_address,
                                            config.network):
                printdbg(
                    "\tPayment address [%s] not a valid Dash address for network [%s], returning False"
                    % (self.payment_address, config.network))
                return False

            # URL
            if (len(self.url.strip()) < 4):
                printdbg("\tProposal URL [%s] too short, returning False" %
                         self.url)
                return False

            try:
                parsed = urlparse.urlparse(self.url)
            except Exception as e:
                printdbg(
                    "\tUnable to parse Proposal URL, marking invalid: %s" % e)
                return False

        except Exception as e:
            printdbg(
                "Unable to validate in Proposal#is_valid, marking invalid: %s"
                % e.message)
            return False

        printdbg("Leaving Proposal#is_valid, Valid = True")
        return True

    def is_expired(self, superblockcycle=None):
        from constants import SUPERBLOCK_FUDGE_WINDOW
        import dashlib

        if not superblockcycle:
            raise Exception("Required field superblockcycle missing.")

        printdbg("In Proposal#is_expired, for Proposal: %s" % self.__dict__)
        now = misc.now()
        printdbg("\tnow = %s" % now)

        # half the SB cycle, converted to seconds
        # add the fudge_window in seconds, defined elsewhere in Sentinel
        expiration_window_seconds = int(
            (dashlib.blocks_to_seconds(superblockcycle) / 2) +
            SUPERBLOCK_FUDGE_WINDOW)
        printdbg("\texpiration_window_seconds = %s" %
                 expiration_window_seconds)

        # "fully expires" adds the expiration window to end time to ensure a
        # valid proposal isn't excluded from SB by cutting it too close
        fully_expires_at = self.end_epoch + expiration_window_seconds
        printdbg("\tfully_expires_at = %s" % fully_expires_at)

        if (fully_expires_at < now):
            printdbg("\tProposal end_epoch [%s] < now [%s] , returning True" %
                     (self.end_epoch, now))
            return True

        printdbg("Leaving Proposal#is_expired, Expired = False")
        return False

    def is_deletable(self):
        # end_date < (current_date - 30 days)
        thirty_days = (86400 * 30)
        if (self.end_epoch < (misc.now() - thirty_days)):
            return True

        # TBD (item moved to external storage/DashDrive, etc.)
        return False

    @classmethod
    def approved_and_ranked(self, proposal_quorum, next_superblock_max_budget):
        # return all approved proposals, in order of descending vote count
        #
        # we need a secondary 'order by' in case of a tie on vote count, since
        # superblocks must be deterministic
        query = (
            self.select(
                self,
                GovernanceObject)  # Note that we are selecting both models.
            .join(GovernanceObject).where(
                GovernanceObject.absolute_yes_count > proposal_quorum).
            order_by(GovernanceObject.absolute_yes_count.desc(),
                     GovernanceObject.object_hash.desc()))

        ranked = []
        for proposal in query:
            proposal.max_budget = next_superblock_max_budget
            if proposal.is_valid():
                ranked.append(proposal)

        return ranked

    @classmethod
    def expired(self, superblockcycle=None):
        if not superblockcycle:
            raise Exception("Required field superblockcycle missing.")

        expired = []

        for proposal in self.select():
            if proposal.is_expired(superblockcycle):
                expired.append(proposal)

        return expired

    @property
    def rank(self):
        rank = 0
        if self.governance_object:
            rank = self.governance_object.absolute_yes_count
            return rank

    def get_prepare_command(self):
        import dashlib
        obj_data = dashlib.SHIM_serialise_for_dashd(self.serialise())

        # new superblocks won't have parent_hash, revision, etc...
        cmd = ['gobject', 'prepare', '0', '1', str(int(time.time())), obj_data]

        return cmd

    def prepare(self, dashd):
        try:
            object_hash = dashd.rpc_command(*self.get_prepare_command())
            printdbg("Submitted: [%s]" % object_hash)
            self.go.object_fee_tx = object_hash
            self.go.save()

            manual_submit = ' '.join(self.get_submit_command())
            print(manual_submit)

        except JSONRPCException as e:
            print("Unable to prepare: %s" % e.message)
Exemplo n.º 28
0
class User(Model):
    telegramId = CharField(unique=True)

    class Meta:
        database = db  # this model uses the "amazon.db" database
Exemplo n.º 29
0
class GovernanceObject(BaseModel):
    parent_id = IntegerField(default=0)
    object_creation_time = IntegerField(default=int(time.time()))
    object_hash = CharField(max_length=64)
    object_parent_hash = CharField(default='0')
    object_type = IntegerField(default=0)
    object_revision = IntegerField(default=1)
    object_fee_tx = CharField(default='')
    yes_count = IntegerField(default=0)
    no_count = IntegerField(default=0)
    abstain_count = IntegerField(default=0)
    absolute_yes_count = IntegerField(default=0)

    class Meta:
        db_table = 'governance_objects'

    # sync dashd gobject list with our local relational DB backend
    @classmethod
    def sync(self, dashd):
        golist = dashd.rpc_command('gobject', 'list')

        # objects which are removed from the network should be removed from the DB
        try:
            for purged in self.purged_network_objects(list(golist.keys())):
                # SOMEDAY: possible archive step here
                purged.delete_instance(recursive=True, delete_nullable=True)

            for item in golist.values():
                (go, subobj) = self.import_gobject_from_dashd(dashd, item)
        except Exception as e:
            printdbg("Got an error upon import: %s" % e)

    @classmethod
    def purged_network_objects(self, network_object_hashes):
        query = self.select()
        if network_object_hashes:
            query = query.where(~(self.object_hash << network_object_hashes))
        return query

    @classmethod
    def import_gobject_from_dashd(self, dashd, rec):
        import decimal
        import dashlib
        import inflection

        object_hex = rec['DataHex']
        object_hash = rec['Hash']

        gobj_dict = {
            'object_hash': object_hash,
            'object_fee_tx': rec['CollateralHash'],
            'absolute_yes_count': rec['AbsoluteYesCount'],
            'abstain_count': rec['AbstainCount'],
            'yes_count': rec['YesCount'],
            'no_count': rec['NoCount'],
        }

        # shim/dashd conversion
        object_hex = dashlib.SHIM_deserialise_from_dashd(object_hex)
        objects = dashlib.deserialise(object_hex)
        subobj = None

        obj_type, dikt = objects[0:2:1]
        obj_type = inflection.pluralize(obj_type)
        subclass = self._meta.reverse_rel[obj_type].model_class

        # set object_type in govobj table
        gobj_dict['object_type'] = subclass.govobj_type

        # exclude any invalid model data from dashd...
        valid_keys = subclass.serialisable_fields()
        subdikt = {k: dikt[k] for k in valid_keys if k in dikt}

        # get/create, then sync vote counts from dashd, with every run
        govobj, created = self.get_or_create(object_hash=object_hash,
                                             defaults=gobj_dict)
        if created:
            printdbg("govobj created = %s" % created)
        count = govobj.update(**gobj_dict).where(
            self.id == govobj.id).execute()
        if count:
            printdbg("govobj updated = %d" % count)
        subdikt['governance_object'] = govobj

        # get/create, then sync payment amounts, etc. from dashd - Dashd is the master
        try:
            newdikt = subdikt.copy()
            newdikt['object_hash'] = object_hash

            sub, params = subclass(**newdikt), []
            if isinstance(sub, Watchdog):
                params = [dashd]

            if sub.is_valid(*params) is False:
                govobj.vote_delete(dashd)
                return (govobj, None)

            subobj, created = subclass.get_or_create(object_hash=object_hash,
                                                     defaults=subdikt)

        except Exception as e:
            # in this case, vote as delete, and log the vote in the DB
            printdbg("Got invalid object from dashd! %s" % e)
            govobj.vote_delete(dashd)
            return (govobj, None)

        if created:
            printdbg("subobj created = %s" % created)
        count = subobj.update(**subdikt).where(
            subclass.id == subobj.id).execute()
        if count:
            printdbg("subobj updated = %d" % count)

        # ATM, returns a tuple w/gov attributes and the govobj
        return (govobj, subobj)

    def vote_delete(self, dashd):
        if not self.voted_on(signal=VoteSignals.delete,
                             outcome=VoteOutcomes.yes):
            self.vote(dashd, VoteSignals.delete, VoteOutcomes.yes)
        return

    def get_vote_command(self, signal, outcome):
        cmd = [
            'gobject', 'vote-conf', self.object_hash, signal.name, outcome.name
        ]
        return cmd

    def vote(self, dashd, signal, outcome):
        import dashlib

        # At this point, will probably never reach here. But doesn't hurt to
        # have an extra check just in case objects get out of sync (people will
        # muck with the DB).
        if (self.object_hash == '0' or not misc.is_hash(self.object_hash)):
            printdbg("No governance object hash, nothing to vote on.")
            return

        # have I already voted on this gobject with this particular signal and outcome?
        if self.voted_on(signal=signal):
            printdbg("Found a vote for this gobject/signal...")
            vote = self.votes.where(Vote.signal == signal)[0]

            # if the outcome is the same, move on, nothing more to do
            if vote.outcome == outcome:
                # move on.
                printdbg(
                    "Already voted for this same gobject/signal/outcome, no need to re-vote."
                )
                return
            else:
                printdbg(
                    "Found a STALE vote for this gobject/signal, deleting so that we can re-vote."
                )
                vote.delete_instance()

        else:
            printdbg("Haven't voted on this gobject/signal yet...")

        # now ... vote!

        vote_command = self.get_vote_command(signal, outcome)
        printdbg(' '.join(vote_command))
        output = dashd.rpc_command(*vote_command)

        # extract vote output parsing to external lib
        voted = dashlib.did_we_vote(output)

        if voted:
            printdbg('VOTE success, saving Vote object to database')
            Vote(governance_object=self,
                 signal=signal,
                 outcome=outcome,
                 object_hash=self.object_hash).save()
        else:
            printdbg('VOTE failed, trying to sync with network vote')
            self.sync_network_vote(dashd, signal)

    def sync_network_vote(self, dashd, signal):
        printdbg('\tsyncing network vote for object %s with signal %s' %
                 (self.object_hash, signal.name))
        vote_info = dashd.get_my_gobject_votes(self.object_hash)
        for vdikt in vote_info:
            if vdikt['signal'] != signal.name:
                continue

            # ensure valid outcome
            outcome = VoteOutcomes.get(vdikt['outcome'])
            if not outcome:
                continue

            printdbg(
                '\tFound a matching valid vote on the network, outcome = %s' %
                vdikt['outcome'])
            Vote(governance_object=self,
                 signal=signal,
                 outcome=outcome,
                 object_hash=self.object_hash).save()

    def voted_on(self, **kwargs):
        signal = kwargs.get('signal', None)
        outcome = kwargs.get('outcome', None)

        query = self.votes

        if signal:
            query = query.where(Vote.signal == signal)

        if outcome:
            query = query.where(Vote.outcome == outcome)

        count = query.count()
        return count
Exemplo n.º 30
0
class CharFreqTable(BaseModel):
    id = IntegerField(primary_key=True)
    char = CharField()
    freq = IntegerField()
    updatedt = DateTimeField("%Y-%m-%d %H:%M:%S")