Skip to content
Snippets Groups Projects
members.py 4.19 KiB
Newer Older
'''
Created on 5 févr. 2014

@author: inso
'''

inso's avatar
inso committed
from ucoinpy.api import bma
from ..core.person import Person
from PyQt5.QtCore import QAbstractTableModel, QSortFilterProxyModel, Qt, \
                        QDateTime, QModelIndex
inso's avatar
inso committed
from PyQt5.QtGui import QColor
import logging
inso's avatar
inso committed
class MembersFilterProxyModel(QSortFilterProxyModel):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.community = None

    def setSourceModel(self, sourceModel):
        self.community = sourceModel.community
        super().setSourceModel(sourceModel)

    def lessThan(self, left, right):
        """
        Sort table by given column number.
        """
        left_data = self.sourceModel().data(left, Qt.DisplayRole)
        right_data = self.sourceModel().data(right, Qt.DisplayRole)
        return (left_data < right_data)

    def data(self, index, role):
        source_index = self.mapToSource(index)
        source_data = self.sourceModel().data(source_index, role)
inso's avatar
inso committed
        expiration_col = self.sourceModel().columns_ids.index('expiration')
inso's avatar
inso committed
        expiration_index = self.sourceModel().index(source_index.row(), expiration_col)
        expiration_data = self.sourceModel().data(expiration_index, Qt.DisplayRole)
        current_time = QDateTime().currentDateTime().toMSecsSinceEpoch()
        sig_validity = self.community.parameters['sigValidity']
        warning_expiration_time = int(sig_validity / 3)
inso's avatar
inso committed
        #logging.debug("{0} > {1}".format(current_time, expiration_data))
        will_expire_soon = (current_time > expiration_data*1000 - warning_expiration_time*1000)
inso's avatar
inso committed
        if role == Qt.DisplayRole:
            if source_index.column() == self.sourceModel().columns_ids.index('renewed'):
inso's avatar
inso committed
                date = QDateTime.fromTime_t(source_data)
                return date.date()
inso's avatar
inso committed
            if source_index.column() == self.sourceModel().columns_ids.index('expiration'):
inso's avatar
inso committed
                date = QDateTime.fromTime_t(source_data)
                return date.date()
inso's avatar
inso committed
            if source_index.column() == self.sourceModel().columns_ids.index('pubkey'):
inso's avatar
inso committed
                return "pub:{0}".format(source_data[:5])

        if role == Qt.ForegroundRole:
            if will_expire_soon:
                return QColor(Qt.red)
        return source_data


class MembersTableModel(QAbstractTableModel):
    '''
    A Qt abstract item model to display communities in a tree
    '''
    def __init__(self, community, parent=None):
        '''
        Constructor
        '''
inso's avatar
inso committed
        super().__init__(parent)
        self.community = community
inso's avatar
inso committed
        self.columns_titles = {
                               'uid': 'UID',
                               'pubkey': 'Pubkey',
                               'renewed': 'Renewed',
inso's avatar
inso committed
                               'expiration': 'Expiration'}
        self.columns_ids = ('uid', 'pubkey', 'renewed', 'expiration')
inso's avatar
inso committed

    @property
    def pubkeys(self):
        return self.community.members_pubkeys()
    def rowCount(self, parent):
inso's avatar
inso committed
        return len(self.pubkeys)

    def columnCount(self, parent):
inso's avatar
inso committed
        return len(self.columns_ids)
inso's avatar
inso committed

    def headerData(self, section, orientation, role):
        if role == Qt.DisplayRole:
inso's avatar
inso committed
            id = self.columns_ids[section]
            return self.columns_titles[id]
inso's avatar
inso committed

    def member_data(self, pubkey):
        person = Person.lookup(pubkey, self.community)
inso's avatar
inso committed
        join_block = person.membership(self.community)['blockNumber']
        join_date = self.community.get_block(join_block).mediantime
        parameters = self.community.parameters
inso's avatar
inso committed
        expiration_date = join_date + parameters['sigValidity']
        return (person.uid, pubkey, join_date, expiration_date)
    def data(self, index, role):
        if role == Qt.DisplayRole:
            row = index.row()
inso's avatar
inso committed
            col = index.column()
            return self.member_data(self.pubkeys[row])[col]
    def person_index(self, pubkey):
        try:
            row = self.pubkeys.index(pubkey)
            index_start = self.index(row, 0)
            index_end = self.index(row, len(self.columns_ids))
            return (index_start, index_end)
        except ValueError:
            return (QModelIndex(), QModelIndex())

    def flags(self, index):
        return Qt.ItemIsSelectable | Qt.ItemIsEnabled