Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature(forms): connecte le formulaires de soumission de news #163

Merged
merged 5 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 61 additions & 11 deletions qtribu/gui/form_rdp_news.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@
# standard
from functools import partial
from pathlib import Path
from typing import Union
from urllib.parse import urlparse

# PyQGIS
from qgis.core import QgsApplication
from qgis.PyQt import uic
from qgis.PyQt.QtCore import Qt
from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtWidgets import QDialog
from qgis.PyQt.QtWidgets import QDialog, QDialogButtonBox

# plugin
from qtribu.__about__ import DIR_PLUGIN_ROOT
from qtribu.__about__ import DIR_PLUGIN_ROOT, __title__, __version__
from qtribu.constants import GEORDP_NEWS_CATEGORIES, GEORDP_NEWS_ICONS, GeotribuImage
from qtribu.toolbelt import NetworkRequestsManager, PlgLogger, PlgOptionsManager
from qtribu.toolbelt.commons import open_url_in_browser
Expand All @@ -29,6 +30,9 @@ class RdpNewsForm(QDialog):
"""QDialog form to submit a news to a next GeoRDP."""

LOCAL_CDN_PATH: Path = Path().home() / ".geotribu/cdn/"
ISSUE_FORM_BASE_URL: str = (
"https://github.com/geotribu/website/issues/new?template=RDP_NEWS.yml"
)

def __init__(self, parent=None):
"""Constructor.
Expand Down Expand Up @@ -75,13 +79,16 @@ def __init__(self, parent=None):
)
)

# connect help button
# connect standard buttons
self.btn_box.helpRequested.connect(
partial(
open_url_in_browser,
"https://contribuer.geotribu.fr/rdp/add_news/",
)
)
self.btn_box.button(QDialogButtonBox.Ok).clicked.connect(self.on_btn_submit)
self.btn_box.button(QDialogButtonBox.Ok).setDefault(True)
self.btn_box.button(QDialogButtonBox.Ok).setText(self.tr("Submit"))

def cbb_icon_populate(self) -> None:
"""Populate combobox of news icons."""
Expand Down Expand Up @@ -164,13 +171,7 @@ def generate_preview(self) -> None:
self.txt_preview.clear()
self.txt_preview.setMarkdown(md_txt)

def accept(self) -> bool:
"""Auto-connected to the OK button (within the button box), i.e. the `accepted`
signal. Check if required form fields are correctly filled.

:return: False if some check fails. True and emit accepted() signal if everything is ok.
:rtype: bool
"""
def check_required_fields(self) -> bool:
invalid_fields = []
error_message = ""

Expand Down Expand Up @@ -237,6 +238,55 @@ def accept(self) -> bool:
for wdg in invalid_fields:
wdg.setStyleSheet("border: 1px solid red;")
return False
else:

return True

def on_btn_submit(self) -> Union[bool, str, None]:
"""Check if required form fields are correctly filled and submit to Github issue
form.

:return: False if some check fails. True and emit accepted() signal if
everything is ok.
:rtype: bool
"""
if not self.check_required_fields():
return False

completed_url = (
f"{self.ISSUE_FORM_BASE_URL}"
f"&in_author_name={self.wdg_author.lne_firstname.text()} "
f"{self.wdg_author.lne_lastname.text()}"
f"&in_author_mail={self.wdg_author.lne_email.text()}"
f"&in_author_linkedin={self.wdg_author.lne_linkedin_account.text()}"
f"&in_author_mastodon={self.wdg_author.lne_mastodon_account.text()}"
f"&in_author_twitter={self.wdg_author.lne_twitter_account.text()}"
f"&in_author_license=true"
f"&cb_author_content_relationship={self.chb_transparency.isChecked()}"
f"&dr_news_category={self.cbb_category.currentText()}"
f"&in_news_title={self.lne_title.text()}"
gounux marked this conversation as resolved.
Show resolved Hide resolved
f"&in_news_icon={self.cbb_icon.currentText()}"
f"&tx_news_content={self.txt_body.toPlainText()}"
f"&tx_misc_comment={self.txt_comment.toPlainText()}"
f"&title=[GeoRDP] {self.lne_title.text()} - {__title__} {__version__}"
)
self.log(message=f"Opening issue form: {completed_url}", log_level=4)
url_opened = open_url_in_browser(url=completed_url)
if url_opened:
self.log(
message=self.tr("Issue form URL opened in default system web browser."),
log_level=4,
)
super().accept()
return True
else:
Guts marked this conversation as resolved.
Show resolved Hide resolved
self.log(
parent_location=self,
message=self.tr(
"Opening issue form URL in default system web browser failed. "
"Check if there is any special characters in form fields and try again."
),
push=True,
duration=10,
log_level=2,
)
return False
30 changes: 7 additions & 23 deletions qtribu/gui/form_rdp_news.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
<rect>
<x>0</x>
<y>0</y>
<width>763</width>
<width>800</width>
<height>1156</height>
Guts marked this conversation as resolved.
Show resolved Hide resolved
</rect>
</property>
<property name="windowTitle">
<string>GeoRDP - News Form</string>
</property>
<property name="windowOpacity">
<double>0.98</double>
<double>0.980000000000000</double>
</property>
<property name="locale">
<locale language="English" country="UnitedStates"/>
Expand Down Expand Up @@ -267,14 +267,14 @@ p, li { white-space: pre-wrap; }
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label">
<widget class="QLabel" name="lbl_transparency">
<property name="text">
<string>Transparency:</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QCheckBox" name="checkBox">
<widget class="QCheckBox" name="chb_transparency">
<property name="text">
<string>I'm not related to the published content. If not, I give some details in the comment area.</string>
</property>
Expand All @@ -299,6 +299,9 @@ p, li { white-space: pre-wrap; }
</property>
</widget>
</item>
<item row="8" column="1">
<widget class="AuthoringWidget" name="wdg_author" native="true"/>
</item>
<item row="9" column="1">
<spacer name="verticalSpacer">
<property name="orientation">
Expand All @@ -322,9 +325,6 @@ p, li { white-space: pre-wrap; }
</property>
</widget>
</item>
<item row="8" column="1">
<widget class="AuthoringWidget" name="wdg_author" native="true"/>
</item>
<item row="11" column="1">
<spacer name="verticalSpacer_2">
<property name="orientation">
Expand Down Expand Up @@ -385,22 +385,6 @@ p, li { white-space: pre-wrap; }
</customwidgets>
<resources/>
<connections>
<connection>
<sender>btn_box</sender>
<signal>accepted()</signal>
<receiver>dlg_form_rdp_news</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>btn_box</sender>
<signal>rejected()</signal>
Expand Down
5 changes: 1 addition & 4 deletions qtribu/toolbelt/network_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,7 @@ def get_from_source(
)

req_reply = self.ntwk_requester.reply()
if (
not req_reply.rawHeader(b"Content-Type")
== response_expected_content_type
):
if req_reply.rawHeader(b"Content-Type") != response_expected_content_type:
raise TypeError(
f"Response mime-type is '{req_reply.rawHeader(b'Content-type')}' "
f"not '{response_expected_content_type}' as required.".format()
Expand Down
Loading