Skip to content
Snippets Groups Projects
Commit aac84aea authored by inso's avatar inso
Browse files

Working on certificaton dialog

parent 9cbbbc5e
No related branches found
No related tags found
No related merge requests found
...@@ -44,6 +44,21 @@ class CertificationsProcessor: ...@@ -44,6 +44,21 @@ class CertificationsProcessor:
""" """
return self._certifications_repo.get_all(currency=currency, certified=pubkey) return self._certifications_repo.get_all(currency=currency, certified=pubkey)
def cert_issuance_delay(self, currency, pubkey, parameters, blockchain_time):
"""
Get the remaining time before being able to issue new certification.
:param str currency: the currency of the certifications
:param str pubkey: the pubkey of the certifications
:param sakia.data.entities.BlockchainParameters parameters: the parameters of the blockchain
:param int blockchain_time: the current time of the blockchain
:return: the remaining time
:rtype: int
"""
certified = self._certifications_repo.get_latest_sent(currency=currency, certifier=pubkey)
if certified and blockchain_time - certified.timestamp < parameters.sig_period:
return parameters.sig_period - (blockchain_time - certified.timestamp)
return 0
def create_certification(self, currency, cert, blockstamp): def create_certification(self, currency, cert, blockstamp):
""" """
Creates a certification and insert it in the db Creates a certification and insert it in the db
......
...@@ -4,7 +4,6 @@ from ..entities import Source ...@@ -4,7 +4,6 @@ from ..entities import Source
from .nodes import NodesProcessor from .nodes import NodesProcessor
from ..connectors import BmaConnector from ..connectors import BmaConnector
from duniterpy.api import bma, errors from duniterpy.api import bma, errors
from duniterpy.documents import Block, BMAEndpoint
import asyncio import asyncio
......
...@@ -78,6 +78,23 @@ class CertificationsRepo: ...@@ -78,6 +78,23 @@ class CertificationsRepo:
return [Certification(*data) for data in datas] return [Certification(*data) for data in datas]
return [] return []
def get_latest_sent(self, currency, pubkey):
"""
Get latest sent certification
:param str currency:
:param str pubkey:
:return:
:rtype: sakia.data.entities.Certification
"""
request = """SELECT * FROM certifications
WHERE currency=? AND certifier=?
ORDER BY timestamp DESC
LIMIT 1"""
c = self._conn.execute(request, (currency, pubkey))
data = c.fetchone()
if data:
return Certification(*data)
def drop(self, certification): def drop(self, certification):
""" """
Drop an existing certification from the database Drop an existing certification from the database
......
...@@ -6,8 +6,8 @@ ...@@ -6,8 +6,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>715</width> <width>517</width>
<height>477</height> <height>316</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
...@@ -59,55 +59,6 @@ ...@@ -59,55 +59,6 @@
<property name="topMargin"> <property name="topMargin">
<number>6</number> <number>6</number>
</property> </property>
<item>
<layout class="QHBoxLayout" name="layout_mode_contact">
<item>
<widget class="QRadioButton" name="radio_contact">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Con&amp;tact</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Maximum</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QComboBox" name="combo_contact">
<property name="enabled">
<bool>true</bool>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
</layout>
</item>
<item> <item>
<layout class="QHBoxLayout" name="layout_mode_pubkey"> <layout class="QHBoxLayout" name="layout_mode_pubkey">
<item> <item>
......
...@@ -42,13 +42,11 @@ class CertificationController(ComponentController): ...@@ -42,13 +42,11 @@ class CertificationController(ComponentController):
:rtype: CertificationController :rtype: CertificationController
""" """
view = CertificationView(parent.view, None, None, communities_names, contacts_names) view = CertificationView(parent.view, None, None)
model = CertificationModel(None, app, account, community) model = CertificationModel(None, app)
certification = cls(parent, view, model, None, None) certification = cls(parent, view, model, None, None)
search_user = SearchUserController.create(certification, app, search_user = SearchUserController.create(certification, app)
account=model.account,
community=model.community)
certification.set_search_user(search_user) certification.set_search_user(search_user)
user_information = UserInformationController.create(certification, app, user_information = UserInformationController.create(certification, app,
...@@ -156,25 +154,20 @@ class CertificationController(ComponentController): ...@@ -156,25 +154,20 @@ class CertificationController(ComponentController):
""" """
pubkey = None pubkey = None
if self.view.recipient_mode() == CertificationView.RecipientMode.CONTACT: if self.view.recipient_mode() == CertificationView.RecipientMode.SEARCH:
contact_name = self.view.selected_contact()
pubkey = self.model.contact_name_pubkey(contact_name)
elif self.view.recipient_mode() == CertificationView.RecipientMode.SEARCH:
if self.search_user.current_identity(): if self.search_user.current_identity():
pubkey = self.search_user.current_identity().pubkey pubkey = self.search_user.current_identity().pubkey
else: else:
pubkey = self.view.pubkey_value() pubkey = self.view.pubkey_value()
return pubkey return pubkey
@once_at_a_time def refresh(self):
@asyncify
async def refresh(self):
stock = self.model.get_cert_stock() stock = self.model.get_cert_stock()
written, pending = await self.model.nb_certifications() written, pending = self.model.nb_certifications()
days, hours, minutes, seconds = await self.model.remaining_time() days, hours, minutes, seconds = self.model.remaining_time()
self.view.display_cert_stock(written, pending, stock, days, hours, minutes) self.view.display_cert_stock(written, pending, stock, days, hours, minutes)
if await self.model.could_certify(): if self.model.could_certify():
if written < stock or stock == 0: if written < stock or stock == 0:
if days+hours+minutes > 0: if days+hours+minutes > 0:
if days > 0: if days > 0:
...@@ -197,10 +190,10 @@ class CertificationController(ComponentController): ...@@ -197,10 +190,10 @@ class CertificationController(ComponentController):
pubkey = self.selected_pubkey() pubkey = self.selected_pubkey()
self.user_information.search_identity(pubkey) self.user_information.search_identity(pubkey)
def change_current_community(self, index): def change_current_connection(self, index):
self.model.change_community(index) self.model.change_connection(index)
self.search_user.set_community(self.community) self.search_user.set_connection(self.model.connection)
self.user_information.change_community(self.community) self.user_information.change_connection(self.model.connection)
self.refresh() self.refresh()
def async_exec(self): def async_exec(self):
......
from sakia.gui.component.model import ComponentModel from sakia.gui.component.model import ComponentModel
from duniterpy.api import errors from sakia.data.processors import IdentitiesProcessor, CertificationsProcessor, BlockchainProcessor
from sakia.errors import NoPeerAvailable
import logging
class CertificationModel(ComponentModel): class CertificationModel(ComponentModel):
...@@ -10,42 +7,26 @@ class CertificationModel(ComponentModel): ...@@ -10,42 +7,26 @@ class CertificationModel(ComponentModel):
The model of Certification component The model of Certification component
""" """
def __init__(self, parent, app, identity, currency, def __init__(self, parent, app, connection):
connections_repo, identities_processor, blockchain_processor):
""" """
The data model of the certification dialog The data model of the certification dialog
:param parent: :param parent:
:param sakia.app.Application app: :param sakia.app.Application app:
:param sakia.data.entities.Identity identity: the identity of the certifier :param sakia.data.entities.Connection connection: the connection used to certify
:param str currency:
:param sakia.data.repositories.ConnectionsRepository connections_repo:
:param sakia.data.processors.IdentitiesProcessor identities_processor:
:param sakia.data.processors.BlockchainProcessor blockchain_processor:
""" """
super().__init__(parent) super().__init__(parent)
self.app = app self.app = app
self.currency = currency self.connection = connection
self.connections_repo = connections_repo self._certifications_processor = CertificationsProcessor.instanciate(app)
self.identities_processor = identities_processor self._identities_processor = IdentitiesProcessor.instanciate(app)
self.blockchain_processor = blockchain_processor self._blockchain_processor = BlockchainProcessor.instanciate(app)
def contact_name_pubkey(self, name): def change_connection(self, index):
"""
Get the pubkey of a contact from its name
:param str name:
:return:
:rtype: str
"""
for contact in self.account.contacts:
if contact['name'] == name:
return contact['pubkey']
def change_currency(self, index):
""" """
Change current currency Change current currency
:param int index: index of the community in the account list :param int index: index of the community in the account list
""" """
self.currency = self.connections_repo.get_currencies()[index] self.connection = self.connections_repo.get_currencies()[index]
def get_cert_stock(self): def get_cert_stock(self):
""" """
...@@ -55,45 +36,40 @@ class CertificationModel(ComponentModel): ...@@ -55,45 +36,40 @@ class CertificationModel(ComponentModel):
""" """
return self.blockchain_processor.parameters(self.currency).sig_stock return self.blockchain_processor.parameters(self.currency).sig_stock
async def remaining_time(self): def remaining_time(self):
""" """
Get remaining time as a tuple to display Get remaining time as a tuple to display
:return: a tuple containing (days, hours, minutes, seconds) :return: a tuple containing (days, hours, minutes, seconds)
:rtype: tuple[int] :rtype: tuple[int]
""" """
remaining_time = await account_identity.cert_issuance_delay(self.app.identities_registry, self.currency) remaining_time = self._certifications_processor.cert_issuance_delay(self.connection.currency,
self.connection.pubkey)
days, remainder = divmod(remaining_time, 3600 * 24) days, remainder = divmod(remaining_time, 3600 * 24)
hours, remainder = divmod(remainder, 3600) hours, remainder = divmod(remainder, 3600)
minutes, seconds = divmod(remainder, 60) minutes, seconds = divmod(remainder, 60)
return days, hours, minutes, seconds return days, hours, minutes, seconds
async def nb_certifications(self): def nb_certifications(self):
""" """
Get Get
:return: a tuple containing (written valid certifications, pending certifications) :return: a tuple containing (written valid certifications, pending certifications)
:rtype: tuple[int] :rtype: tuple[int]
""" """
account_identity = await self.account.identity(self.currency) certifications = self._certifications_processor.certifications_sent(self.connection.currency,
certifications = await account_identity.unique_valid_certified_by(self.app.identities_registry, self.currency) self.connection.pubkey)
nb_certifications = len([c for c in certifications if c['block_number']]) nb_certifications = len([c for c in certifications if c.written_on])
nb_cert_pending = len([c for c in certifications if not c['block_number']]) nb_cert_pending = len([c for c in certifications if not c.written_on])
return nb_certifications, nb_cert_pending return nb_certifications, nb_cert_pending
async def could_certify(self): def could_certify(self):
""" """
Check if the user could theorically certify Check if the user could theorically certify
:return: true if the user can certifiy :return: true if the user can certifiy
:rtype: bool :rtype: bool
""" """
account_identity = await self.account.identity(self.community) is_member = self._identities_processor.get_identity(self.connection.currency,
is_member = await account_identity.is_member(self.community) self.connection.pubkey,
try: self.connection.pubkey)
block_0 = await self.community.get_block(0)
except errors.DuniterError as e: return is_member and self._blockchain_processor.current_buid(self.connection.currency)
if e.ucode == errors.BLOCK_NOT_FOUND:
block_0 = None
except NoPeerAvailable as e:
logging.debug(str(e))
block_0 = None
return is_member or not block_0
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment