From 53c3acd39f2b173ecc81026f933ded83bfa202a0 Mon Sep 17 00:00:00 2001 From: Ojakoo Date: Mon, 25 Jul 2022 20:12:32 +0300 Subject: [PATCH 1/8] directory api working state --- .gitignore | 1 + members/views/applications.py | 4 + poetry.lock | 196 ++++++++++++++++++++++++++++++++-- pyproject.toml | 2 + sikweb/settings.py | 2 + webapp/utils.py | 24 +++++ 6 files changed, 220 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 88217a7..2d24eaf 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ node_modules/ *.code-workspace venv/ .venv/ +google-service-cred.json diff --git a/members/views/applications.py b/members/views/applications.py index dc5f033..ace850e 100644 --- a/members/views/applications.py +++ b/members/views/applications.py @@ -12,6 +12,7 @@ import logging import html from webapp.utils import send_email +from webapp.utils import add_to_mailinglist from members.views.utils import * from members.tables import RequestTable @@ -86,6 +87,9 @@ def application_accept(request, *args, **kwargs): ).format(application.email), ) + if application.jas: + add_to_mailinglist(application.email) + member = application.to_member() member.save() application.delete() diff --git a/poetry.lock b/poetry.lock index 1954dee..32e4dca 100644 --- a/poetry.lock +++ b/poetry.lock @@ -45,6 +45,14 @@ d = ["aiohttp (>=3.7.4)"] jupyter = ["ipython (>=7.8.0)", "tokenize-rt (>=3.2.0)"] uvloop = ["uvloop (>=0.15.2)"] +[[package]] +name = "cachetools" +version = "5.2.0" +description = "Extensible memoizing collections and decorators" +category = "main" +optional = false +python-versions = "~=3.7" + [[package]] name = "certifi" version = "2022.6.15" @@ -346,6 +354,85 @@ category = "main" optional = false python-versions = ">=3.6" +[[package]] +name = "google-api-core" +version = "2.8.2" +description = "Google API client core library" +category = "main" +optional = false +python-versions = ">=3.6" + +[package.dependencies] +google-auth = ">=1.25.0,<3.0dev" +googleapis-common-protos = ">=1.56.2,<2.0dev" +protobuf = ">=3.15.0,<5.0.0dev" +requests = ">=2.18.0,<3.0.0dev" + +[package.extras] +grpc = ["grpcio (>=1.33.2,<2.0dev)", "grpcio-status (>=1.33.2,<2.0dev)"] + +[[package]] +name = "google-api-python-client" +version = "2.54.0" +description = "Google API Client Library for Python" +category = "main" +optional = false +python-versions = ">=3.7" + +[package.dependencies] +google-api-core = ">=1.31.5,<2.0.0 || >2.3.0,<3.0.0dev" +google-auth = ">=1.19.0,<3.0.0dev" +google-auth-httplib2 = ">=0.1.0" +httplib2 = ">=0.15.0,<1dev" +uritemplate = ">=3.0.1,<5" + +[[package]] +name = "google-auth" +version = "2.9.1" +description = "Google Authentication Library" +category = "main" +optional = false +python-versions = ">=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*" + +[package.dependencies] +cachetools = ">=2.0.0,<6.0" +pyasn1-modules = ">=0.2.1" +rsa = {version = ">=3.1.4,<5", markers = "python_version >= \"3.6\""} +six = ">=1.9.0" + +[package.extras] +aiohttp = ["requests (>=2.20.0,<3.0.0dev)", "aiohttp (>=3.6.2,<4.0.0dev)"] +enterprise_cert = ["cryptography (==36.0.2)", "pyopenssl (==22.0.0)"] +pyopenssl = ["pyopenssl (>=20.0.0)"] +reauth = ["pyu2f (>=0.1.5)"] + +[[package]] +name = "google-auth-httplib2" +version = "0.1.0" +description = "Google Authentication Library: httplib2 transport" +category = "main" +optional = false +python-versions = "*" + +[package.dependencies] +google-auth = "*" +httplib2 = ">=0.15.0" +six = "*" + +[[package]] +name = "googleapis-common-protos" +version = "1.56.4" +description = "Common protobufs used in Google APIs" +category = "main" +optional = false +python-versions = ">=3.7" + +[package.dependencies] +protobuf = ">=3.15.0,<5.0.0dev" + +[package.extras] +grpc = ["grpcio (>=1.0.0,<2.0.0dev)"] + [[package]] name = "gunicorn" version = "20.1.0" @@ -360,6 +447,17 @@ gevent = ["gevent (>=1.4.0)"] setproctitle = ["setproctitle"] tornado = ["tornado (>=0.2)"] +[[package]] +name = "httplib2" +version = "0.20.4" +description = "A comprehensive HTTP client library." +category = "main" +optional = false +python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" + +[package.dependencies] +pyparsing = {version = ">=2.4.2,<3.0.0 || >3.0.0,<3.0.1 || >3.0.1,<3.0.2 || >3.0.2,<3.0.3 || >3.0.3,<4", markers = "python_version > \"3.0\""} + [[package]] name = "idna" version = "3.3" @@ -521,6 +619,14 @@ python-versions = ">=3.7" docs = ["furo (>=2021.7.5b38)", "proselint (>=0.10.2)", "sphinx-autodoc-typehints (>=1.12)", "sphinx (>=4)"] test = ["appdirs (==1.4.4)", "pytest-cov (>=2.7)", "pytest-mock (>=3.6)", "pytest (>=6)"] +[[package]] +name = "protobuf" +version = "4.21.3" +description = "" +category = "main" +optional = false +python-versions = ">=3.7" + [[package]] name = "psycopg2-binary" version = "2.8.6" @@ -529,6 +635,25 @@ category = "main" optional = false python-versions = ">=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*" +[[package]] +name = "pyasn1" +version = "0.4.8" +description = "ASN.1 types and codecs" +category = "main" +optional = false +python-versions = "*" + +[[package]] +name = "pyasn1-modules" +version = "0.2.8" +description = "A collection of ASN.1-based protocols modules." +category = "main" +optional = false +python-versions = "*" + +[package.dependencies] +pyasn1 = ">=0.4.6,<0.5.0" + [[package]] name = "pyexcel" version = "0.5.15" @@ -593,7 +718,7 @@ tests = ["pytest (>=6.0.0,<7.0.0)", "coverage[toml] (==5.0.4)"] name = "pyparsing" version = "3.0.9" description = "pyparsing module - Classes and methods to define and execute parsing grammars" -category = "dev" +category = "main" optional = false python-versions = ">=3.6.8" @@ -672,6 +797,17 @@ urllib3 = ">=1.21.1,<1.27" socks = ["PySocks (>=1.5.6,!=1.5.7)", "win-inet-pton"] use_chardet_on_py3 = ["chardet (>=3.0.2,<5)"] +[[package]] +name = "rsa" +version = "4.9" +description = "Pure-Python RSA implementation" +category = "main" +optional = false +python-versions = ">=3.6,<4" + +[package.dependencies] +pyasn1 = ">=0.1.3" + [[package]] name = "safety" version = "1.10.3" @@ -802,6 +938,14 @@ category = "dev" optional = false python-versions = ">=3.7" +[[package]] +name = "uritemplate" +version = "4.1.1" +description = "Implementation of RFC 6570 URI Templates" +category = "main" +optional = false +python-versions = ">=3.6" + [[package]] name = "urllib3" version = "1.26.9" @@ -849,14 +993,17 @@ testing = ["pytest (>=6)", "pytest-checkdocs (>=2.4)", "pytest-flake8", "pytest- [metadata] lock-version = "1.1" python-versions = "^3.9" -content-hash = "7bd8922d622b0f6a042c5508bb2789bb3fb69df3b3198ae0adca52ce12432887" +content-hash = "9eb27b8eb2c2f4a11c2c7912eef300810d154cb95f9f5b2591efe11ec2ad67f0" [metadata.files] attrs = [ {file = "attrs-21.4.0-py2.py3-none-any.whl", hash = "sha256:2d27e3784d7a565d36ab851fe94887c5eccd6a463168875832a1be79c82828b4"}, {file = "attrs-21.4.0.tar.gz", hash = "sha256:626ba8234211db98e869df76230a137c4c40a12d72445c45d5f5b716f076e2fd"}, ] -babel = [] +babel = [ + {file = "Babel-2.10.3-py3-none-any.whl", hash = "sha256:ff56f4892c1c4bf0d814575ea23471c230d544203c7748e8c68f0089478d48eb"}, + {file = "Babel-2.10.3.tar.gz", hash = "sha256:7614553711ee97490f732126dc077f8d0ae084ebc6a96e23db1482afabdb2c51"}, +] black = [ {file = "black-22.3.0-cp310-cp310-macosx_10_9_universal2.whl", hash = "sha256:2497f9c2386572e28921fa8bec7be3e51de6801f7459dffd6e62492531c47e09"}, {file = "black-22.3.0-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:5795a0375eb87bfe902e80e0c8cfaedf8af4d49694d69161e5bd3206c18618bb"}, @@ -882,7 +1029,11 @@ black = [ {file = "black-22.3.0-py3-none-any.whl", hash = "sha256:bc58025940a896d7e5356952228b68f793cf5fcb342be703c3a2669a1488cb72"}, {file = "black-22.3.0.tar.gz", hash = "sha256:35020b8886c022ced9282b51b5a875b6d1ab0c387b31a065b84db7c33085ca79"}, ] -certifi = [] +cachetools = [] +certifi = [ + {file = "certifi-2022.6.15-py3-none-any.whl", hash = "sha256:fe86415d55e84719d75f8b69414f6438ac3547d2078ab91b67e779ef69378412"}, + {file = "certifi-2022.6.15.tar.gz", hash = "sha256:84c85a9078b11105f04f3036a9482ae10e4621616db313fe045dd24743a0820d"}, +] charset-normalizer = [ {file = "charset-normalizer-2.0.12.tar.gz", hash = "sha256:2857e29ff0d34db842cd7ca3230549d1a697f96ee6d3fb071cfa6c7393832597"}, {file = "charset_normalizer-2.0.12-py3-none-any.whl", hash = "sha256:6881edbebdb17b39b4eaaa821b438bf6eddffb4468cf344f09f89def34a8b1df"}, @@ -891,7 +1042,10 @@ click = [ {file = "click-8.1.3-py3-none-any.whl", hash = "sha256:bb4d8133cb15a609f44e8213d9b391b0809795062913b383c62be0ee95b1db48"}, {file = "click-8.1.3.tar.gz", hash = "sha256:7682dc8afb30297001674575ea00d1814d808d6a36af415a82bd481d37ba7b8e"}, ] -colorama = [] +colorama = [ + {file = "colorama-0.4.5-py2.py3-none-any.whl", hash = "sha256:854bf444933e37f5824ae7bfc1e98d5bce2ebe4160d46b5edf346a89358e99da"}, + {file = "colorama-0.4.5.tar.gz", hash = "sha256:e6c6b4334fc50988a639d9b98aa429a0b57da6e17b9a44f0451f930b6967b7a4"}, +] coverage = [ {file = "coverage-5.5-cp27-cp27m-macosx_10_9_x86_64.whl", hash = "sha256:b6d534e4b2ab35c9f93f46229363e17f63c53ad01330df9f2d6bd1187e5eaacf"}, {file = "coverage-5.5-cp27-cp27m-manylinux1_i686.whl", hash = "sha256:b7895207b4c843c76a25ab8c1e866261bcfe27bfaa20c192de5190121770672b"}, @@ -1013,7 +1167,10 @@ djangorestframework = [ {file = "djangorestframework-3.13.1-py3-none-any.whl", hash = "sha256:24c4bf58ed7e85d1fe4ba250ab2da926d263cd57d64b03e8dcef0ac683f8b1aa"}, {file = "djangorestframework-3.13.1.tar.gz", hash = "sha256:0c33407ce23acc68eca2a6e46424b008c9c02eceb8cf18581921d0092bc1f2ee"}, ] -djangorestframework-simplejwt = [] +djangorestframework-simplejwt = [ + {file = "djangorestframework_simplejwt-5.2.0-py3-none-any.whl", hash = "sha256:bcc4cb74dcb637ca1e17eed35276bd618ab19491f8c53e65dee6271177c355e8"}, + {file = "djangorestframework_simplejwt-5.2.0.tar.gz", hash = "sha256:a60b09afb27d91ad1d7ac904cc632bd52cecead8f389f0fa1532ceb0fb757a74"}, +] dparse = [ {file = "dparse-0.5.1-py3-none-any.whl", hash = "sha256:e953a25e44ebb60a5c6efc2add4420c177f1d8404509da88da9729202f306994"}, {file = "dparse-0.5.1.tar.gz", hash = "sha256:a1b5f169102e1c894f9a7d5ccf6f9402a836a5d24be80a986c7ce9eaed78f367"}, @@ -1022,14 +1179,24 @@ et-xmlfile = [ {file = "et_xmlfile-1.1.0-py3-none-any.whl", hash = "sha256:a2ba85d1d6a74ef63837eed693bcb89c3f752169b0e3e7ae5b16ca5e1b3deada"}, {file = "et_xmlfile-1.1.0.tar.gz", hash = "sha256:8eb9e2bc2f8c97e37a2dc85a09ecdcdec9d8a396530a6d5a33b30b9a92da0c5c"}, ] +google-api-core = [] +google-api-python-client = [] +google-auth = [] +google-auth-httplib2 = [] +googleapis-common-protos = [] gunicorn = [ + {file = "gunicorn-20.1.0-py3-none-any.whl", hash = "sha256:9dcc4547dbb1cb284accfb15ab5667a0e5d1881cc443e0677b4882a4067a807e"}, {file = "gunicorn-20.1.0.tar.gz", hash = "sha256:e0a968b5ba15f8a328fdfd7ab1fcb5af4470c28aaf7e55df02a99bc13138e6e8"}, ] +httplib2 = [] idna = [ {file = "idna-3.3-py3-none-any.whl", hash = "sha256:84d9dd047ffa80596e0f246e2eab0b391788b0503584e8945f2368256d2735ff"}, {file = "idna-3.3.tar.gz", hash = "sha256:9d643ff0a55b762d5cdb124b8eaa99c66322e2157b69160bc32796e824360e6d"}, ] -importlib-metadata = [] +importlib-metadata = [ + {file = "importlib_metadata-4.11.4-py3-none-any.whl", hash = "sha256:c58c8eb8a762858f49e18436ff552e83914778e50e9d2f1660535ffb364552ec"}, + {file = "importlib_metadata-4.11.4.tar.gz", hash = "sha256:5d26852efe48c0a32b0509ffbc583fda1a2266545a78d104a6f4aff3db17d700"}, +] jdcal = [ {file = "jdcal-1.4.1-py2.py3-none-any.whl", hash = "sha256:1abf1305fce18b4e8aa248cf8fe0c56ce2032392bc64bbd61b5dff2a19ec8bba"}, {file = "jdcal-1.4.1.tar.gz", hash = "sha256:472872e096eb8df219c23f2689fc336668bdb43d194094b5cc1707e1640acfc8"}, @@ -1069,7 +1236,10 @@ pathspec = [ {file = "pathspec-0.9.0-py2.py3-none-any.whl", hash = "sha256:7d15c4ddb0b5c802d161efc417ec1a2558ea2653c2e8ad9c19098201dc1c993a"}, {file = "pathspec-0.9.0.tar.gz", hash = "sha256:e564499435a2673d586f6b2130bb5b95f04a3ba06f81b8f895b651a3c76aabb1"}, ] -phonenumbers = [] +phonenumbers = [ + {file = "phonenumbers-8.12.50-py2.py3-none-any.whl", hash = "sha256:56713403b4160b59ea1fef4e842ddeb70931055146d794d371b60cd4b5f05fb8"}, + {file = "phonenumbers-8.12.50.tar.gz", hash = "sha256:f00d67f20875804f4fade4803a9438294029982ac929c6ba303e1f0290cf5d45"}, +] pillow = [ {file = "Pillow-9.1.1-cp310-cp310-macosx_10_10_x86_64.whl", hash = "sha256:42dfefbef90eb67c10c45a73a9bc1599d4dac920f7dfcbf4ec6b80cb620757fe"}, {file = "Pillow-9.1.1-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:ffde4c6fabb52891d81606411cbfaf77756e3b561b566efd270b3ed3791fde4e"}, @@ -1114,6 +1284,7 @@ platformdirs = [ {file = "platformdirs-2.5.2-py3-none-any.whl", hash = "sha256:027d8e83a2d7de06bbac4e5ef7e023c02b863d7ea5d079477e722bb41ab25788"}, {file = "platformdirs-2.5.2.tar.gz", hash = "sha256:58c8abb07dcb441e6ee4b11d8df0ac856038f944ab98b7be6b27b2a3c7feef19"}, ] +protobuf = [] psycopg2-binary = [ {file = "psycopg2-binary-2.8.6.tar.gz", hash = "sha256:11b9c0ebce097180129e422379b824ae21c8f2a6596b159c7659e2e5a00e1aa0"}, {file = "psycopg2_binary-2.8.6-cp27-cp27m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl", hash = "sha256:d14b140a4439d816e3b1229a4a525df917d6ea22a0771a2a78332273fd9528a4"}, @@ -1151,6 +1322,8 @@ psycopg2-binary = [ {file = "psycopg2_binary-2.8.6-cp39-cp39-win32.whl", hash = "sha256:6422f2ff0919fd720195f64ffd8f924c1395d30f9a495f31e2392c2efafb5056"}, {file = "psycopg2_binary-2.8.6-cp39-cp39-win_amd64.whl", hash = "sha256:15978a1fbd225583dd8cdaf37e67ccc278b5abecb4caf6b2d6b8e2b948e953f6"}, ] +pyasn1 = [] +pyasn1-modules = [] pyexcel = [ {file = "pyexcel-0.5.15-py2.py3-none-any.whl", hash = "sha256:7fac067e65567c380933b4d382587a5ce6581d0ad85992f6f0bc7c3f16012184"}, {file = "pyexcel-0.5.15.tar.gz", hash = "sha256:f0a7797f3a0de9e6f81151c9581fa90c4e1afce207dc47d2f0ba728dd2e24467"}, @@ -1163,7 +1336,10 @@ pyexcel-xlsx = [ {file = "pyexcel-xlsx-0.5.8.tar.gz", hash = "sha256:ab3913b465d0d645a51e3c896dc006738a398d36ceaad2dad133056132facb92"}, {file = "pyexcel_xlsx-0.5.8-py2.py3-none-any.whl", hash = "sha256:9bae2820c5767440d8a387695e0f8e8f73c97bcde0a5680c200ae82a2f6d5cc6"}, ] -pyjwt = [] +pyjwt = [ + {file = "PyJWT-2.4.0-py3-none-any.whl", hash = "sha256:72d1d253f32dbd4f5c88eaf1fdc62f3a19f676ccbadb9dbc5d07e951b2b26daf"}, + {file = "PyJWT-2.4.0.tar.gz", hash = "sha256:d42908208c699b3b973cbeb01a969ba6a96c821eefb1c5bfe4c390c01d67abba"}, +] pyparsing = [ {file = "pyparsing-3.0.9-py3-none-any.whl", hash = "sha256:5026bae9a10eeaefb61dab2f09052b9f4307d44aee4eda64b309723d8d206bbc"}, {file = "pyparsing-3.0.9.tar.gz", hash = "sha256:2b020ecf7d21b687f219b71ecad3631f644a47f01403fa1d1036b0c6416d70fb"}, @@ -1247,6 +1423,7 @@ requests = [ {file = "requests-2.27.1-py2.py3-none-any.whl", hash = "sha256:f22fa1e554c9ddfd16e6e41ac79759e17be9e492b3587efa038054674760e72d"}, {file = "requests-2.27.1.tar.gz", hash = "sha256:68d7c56fd5a8999887728ef304a6d12edc7be74f1cfa47714fc8b414525c9a61"}, ] +rsa = [] safety = [ {file = "safety-1.10.3-py2.py3-none-any.whl", hash = "sha256:5f802ad5df5614f9622d8d71fedec2757099705c2356f862847c58c6dfe13e84"}, {file = "safety-1.10.3.tar.gz", hash = "sha256:30e394d02a20ac49b7f65292d19d38fa927a8f9582cdfd3ad1adbbc66c641ad5"}, @@ -1290,6 +1467,7 @@ typing-extensions = [ {file = "typing_extensions-4.2.0-py3-none-any.whl", hash = "sha256:6657594ee297170d19f67d55c05852a874e7eb634f4f753dbd667855e07c1708"}, {file = "typing_extensions-4.2.0.tar.gz", hash = "sha256:f1c24655a0da0d1b67f07e17a5e6b2a105894e6824b92096378bb3668ef02376"}, ] +uritemplate = [] urllib3 = [ {file = "urllib3-1.26.9-py2.py3-none-any.whl", hash = "sha256:44ece4d53fb1706f667c9bd1c648f5469a2ec925fcf3a776667042d645472c14"}, {file = "urllib3-1.26.9.tar.gz", hash = "sha256:aabaf16477806a5e1dd19aa41f8c2b7950dd3c746362d7e3223dbe6de6ac448e"}, diff --git a/pyproject.toml b/pyproject.toml index 7fc3cd6..9265ec4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -38,6 +38,8 @@ sentry-sdk = "^1.4.3" django-polymorphic = "^3.1.0" python-dotenv = "^0.20.0" djangorestframework-simplejwt = "^5.2.0" +google-auth = "^2.9.1" +google-api-python-client = "^2.54.0" [tool.poetry.dev-dependencies] coverage = "^5.5" diff --git a/sikweb/settings.py b/sikweb/settings.py index 2f771d9..24fea40 100644 --- a/sikweb/settings.py +++ b/sikweb/settings.py @@ -79,6 +79,8 @@ DATABASES = { } } +# Google api settings +GROUP_KEY = os.getenv("GROUP_KEY", "") # JWT authentication SIMPLE_JWT = { diff --git a/webapp/utils.py b/webapp/utils.py index c1f6dff..aaae579 100644 --- a/webapp/utils.py +++ b/webapp/utils.py @@ -23,9 +23,13 @@ from sikweb.settings import ( DEFAULT_EMAIL_FROM, DEFAULT_EMAIL_FROM_ADDR, ENABLE_AUTOMATIC_EMAILS, + GROUP_KEY, ) from datetime import timedelta +from google.oauth2 import service_account +from googleapiclient.discovery import build +from googleapiclient.errors import HttpError def get_file_extension(file_name, decoded_file): extension = imghdr.what(file_name, decoded_file) @@ -119,3 +123,23 @@ def send_signup_email(to, subject, id, uuid, content): def admin_send_email_signupees(list, subject, content): for to in list: send_email(to.email, subject, markdown.markdown(content), True) + + +def add_to_mailinglist(email): + try: + # get data + SCOPES = ["https://www.googleapis.com/auth/admin.directory.group"] + SERVICE_ACCOUNT_FILE = "google-service-cred.json" + + # create credentials, with subject is used to impersonate admin account + # jas_manager has groups editor rights in google admin + credentials = service_account.Credentials.from_service_account_file( + filename=SERVICE_ACCOUNT_FILE, scopes=SCOPES + ).with_subject("jas_manager@sahkoinsinoorikilta.fi") + + service = build("admin", "directory_v1", credentials=credentials) + service.members().insert(groupKey=GROUP_KEY, body={"email": email}).execute() + except HttpError as err: + logging.exception("Something fukd: {}".format(err.error_details)) + return err.error_details.message + From 3f6a719e9de40a5f6c19d43d291dba038c9adfd8 Mon Sep 17 00:00:00 2001 From: Ojakoo Date: Sun, 31 Jul 2022 11:12:51 +0300 Subject: [PATCH 2/8] Added error handling, send email to user if adding fails --- webapp/utils.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/webapp/utils.py b/webapp/utils.py index aaae579..fb944bd 100644 --- a/webapp/utils.py +++ b/webapp/utils.py @@ -125,7 +125,7 @@ def admin_send_email_signupees(list, subject, content): send_email(to.email, subject, markdown.markdown(content), True) -def add_to_mailinglist(email): +def add_to_mailinglist(email: str): try: # get data SCOPES = ["https://www.googleapis.com/auth/admin.directory.group"] @@ -140,6 +140,16 @@ def add_to_mailinglist(email): service = build("admin", "directory_v1", credentials=credentials) service.members().insert(groupKey=GROUP_KEY, body={"email": email}).execute() except HttpError as err: - logging.exception("Something fukd: {}".format(err.error_details)) - return err.error_details.message + # Already in list, do nothing + if err.status_code == 409: + pass + # Something went wrong, send notification to maintainer. + else: + logging.exception("Failed adding user to list") + + to = "ilari.ojakorpi@sahkoinsinoorikilta.fi" + subject = "Web error: Failed adding to google groups" + body = "Error code: {}\nError details: {}\nEmail that was not added: {}\n\nAdd user manually to jäsenet groups.".format(err.status_code, err.error_details, email) + + send_email(to, subject, body) From 72e91e3d62a11cfbdb4d29fa54ea31abb5aceed4 Mon Sep 17 00:00:00 2001 From: Ojakoo Date: Sun, 31 Jul 2022 16:28:57 +0300 Subject: [PATCH 3/8] Moved google creds to .env --- .gitignore | 3 +-- sikweb/settings.py | 2 ++ webapp/utils.py | 5 +++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 2d24eaf..ab0875c 100644 --- a/.gitignore +++ b/.gitignore @@ -11,5 +11,4 @@ node_modules/ .idea/ *.code-workspace venv/ -.venv/ -google-service-cred.json +.venv/ \ No newline at end of file diff --git a/sikweb/settings.py b/sikweb/settings.py index 24fea40..d635b4b 100644 --- a/sikweb/settings.py +++ b/sikweb/settings.py @@ -15,6 +15,7 @@ import sentry_sdk from sentry_sdk.integrations.django import DjangoIntegration from sikweb.base import * from datetime import timedelta +import json load_dotenv() # loads the configs from .env @@ -81,6 +82,7 @@ DATABASES = { # Google api settings GROUP_KEY = os.getenv("GROUP_KEY", "") +GOOGLE_SERVICE_ACCOUNT = json.loads(os.getenv("GOOGLE_CREDS_JSON", "")) # JWT authentication SIMPLE_JWT = { diff --git a/webapp/utils.py b/webapp/utils.py index fb944bd..8603d27 100644 --- a/webapp/utils.py +++ b/webapp/utils.py @@ -24,6 +24,7 @@ from sikweb.settings import ( DEFAULT_EMAIL_FROM_ADDR, ENABLE_AUTOMATIC_EMAILS, GROUP_KEY, + GOOGLE_SERVICE_ACCOUNT, ) from datetime import timedelta @@ -133,8 +134,8 @@ def add_to_mailinglist(email: str): # create credentials, with subject is used to impersonate admin account # jas_manager has groups editor rights in google admin - credentials = service_account.Credentials.from_service_account_file( - filename=SERVICE_ACCOUNT_FILE, scopes=SCOPES + credentials = service_account.Credentials.from_service_account_info( + info=GOOGLE_SERVICE_ACCOUNT, scopes=SCOPES ).with_subject("jas_manager@sahkoinsinoorikilta.fi") service = build("admin", "directory_v1", credentials=credentials) From 228938b695bdc216ffa0a4ede98d9a6c3f0d1eb1 Mon Sep 17 00:00:00 2001 From: Ojakoo Date: Sun, 31 Jul 2022 16:33:28 +0300 Subject: [PATCH 4/8] lint --- webapp/utils.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/webapp/utils.py b/webapp/utils.py index 8603d27..0f64d5d 100644 --- a/webapp/utils.py +++ b/webapp/utils.py @@ -32,6 +32,7 @@ from google.oauth2 import service_account from googleapiclient.discovery import build from googleapiclient.errors import HttpError + def get_file_extension(file_name, decoded_file): extension = imghdr.what(file_name, decoded_file) extension = "jpg" if extension == "jpeg" else extension @@ -150,7 +151,8 @@ def add_to_mailinglist(email: str): to = "ilari.ojakorpi@sahkoinsinoorikilta.fi" subject = "Web error: Failed adding to google groups" - body = "Error code: {}\nError details: {}\nEmail that was not added: {}\n\nAdd user manually to jäsenet groups.".format(err.status_code, err.error_details, email) + body = "Error code: {}\nError details: {}\nEmail that was not added: {}\n\nAdd user manually to jäsenet groups.".format( + err.status_code, err.error_details, email + ) send_email(to, subject, body) - From 9c66238b82b037b1fcdc801cdefbe4b7feb37985 Mon Sep 17 00:00:00 2001 From: Ojakoo Date: Wed, 3 Aug 2022 00:34:42 +0300 Subject: [PATCH 5/8] Remove old json route --- webapp/utils.py | 1 - 1 file changed, 1 deletion(-) diff --git a/webapp/utils.py b/webapp/utils.py index 0f64d5d..a1ac364 100644 --- a/webapp/utils.py +++ b/webapp/utils.py @@ -131,7 +131,6 @@ def add_to_mailinglist(email: str): try: # get data SCOPES = ["https://www.googleapis.com/auth/admin.directory.group"] - SERVICE_ACCOUNT_FILE = "google-service-cred.json" # create credentials, with subject is used to impersonate admin account # jas_manager has groups editor rights in google admin From c6be0e656282a09e5d1763d6a7f2a062990ab0e5 Mon Sep 17 00:00:00 2001 From: Ojakoo Date: Sat, 6 Aug 2022 10:07:00 +0300 Subject: [PATCH 6/8] Add google envs to deploy --- .env.sample | 2 ++ production_entrypoint.sh | 3 +++ sikweb/settings.py | 2 +- stack-compose.yml | 4 ++++ 4 files changed, 10 insertions(+), 1 deletion(-) diff --git a/.env.sample b/.env.sample index f9f3e95..1dcf8b6 100644 --- a/.env.sample +++ b/.env.sample @@ -9,3 +9,5 @@ DB_PASSWD=postgres DB_HOST=db DB_PORT=5432 EMAIL_API_KEY= +GROUP_KEY= +GOOGLE_CREDS_JSON='{}' diff --git a/production_entrypoint.sh b/production_entrypoint.sh index c58bfac..c39f2f1 100755 --- a/production_entrypoint.sh +++ b/production_entrypoint.sh @@ -10,6 +10,9 @@ fi if test -f "$DB_PASSWD_FILE"; then export DB_PASSWD=$(cat $DB_PASSWD_FILE) fi +if test -f "$GOOGLE_CREDS_JSON"; then + export GOOGLE_CREDS_JSON=$(cat $GOOGLE_CRED_JSON_FILE) +fi # Collect static files echo "Collect static files" diff --git a/sikweb/settings.py b/sikweb/settings.py index d635b4b..b18ebcc 100644 --- a/sikweb/settings.py +++ b/sikweb/settings.py @@ -82,7 +82,7 @@ DATABASES = { # Google api settings GROUP_KEY = os.getenv("GROUP_KEY", "") -GOOGLE_SERVICE_ACCOUNT = json.loads(os.getenv("GOOGLE_CREDS_JSON", "")) +GOOGLE_SERVICE_ACCOUNT = json.loads(os.getenv("GOOGLE_CREDS_JSON", '{}')) # JWT authentication SIMPLE_JWT = { diff --git a/stack-compose.yml b/stack-compose.yml index d90f298..15e6bdc 100644 --- a/stack-compose.yml +++ b/stack-compose.yml @@ -34,11 +34,13 @@ services: - SECRET_KEY_FILE=/run/secrets/BACKEND_SECRET_KEY - DB_PASSWD_FILE=/run/secrets/BACKEND_DB_PASSWD - EMAIL_API_KEY_FILE=/run/secrets/BACKEND_EMAIL_API_KEY + - GOOGLE_CREDS_JSON=/run/secrets/GOOGLE_CREDS_JSON secrets: - BACKEND_SECRET_KEY - BACKEND_DB_PASSWD - BACKEND_EMAIL_API_KEY + - GOOGLE_CREDS_JSON secrets: BACKEND_SECRET_KEY: external: true @@ -46,3 +48,5 @@ secrets: external: true BACKEND_EMAIL_API_KEY: external: true + GOOGLE_CREDS_JSON: + EXTERNAL: true From 4fbf5fe0a40a835fa5361684bb94f0fb7fc210c7 Mon Sep 17 00:00:00 2001 From: Ojakoo Date: Sat, 6 Aug 2022 10:17:43 +0300 Subject: [PATCH 7/8] Jas list error notification only in prod --- webapp/utils.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/webapp/utils.py b/webapp/utils.py index a1ac364..672e5c0 100644 --- a/webapp/utils.py +++ b/webapp/utils.py @@ -18,6 +18,7 @@ from sendgrid.helpers.mail import ( from django.template.loader import render_to_string from django.core.files.base import ContentFile from sikweb.settings import ( + DEPLOY_ENV, FRONTEND_URL, EMAIL_API_KEY, DEFAULT_EMAIL_FROM, @@ -144,14 +145,15 @@ def add_to_mailinglist(email: str): # Already in list, do nothing if err.status_code == 409: pass - # Something went wrong, send notification to maintainer. else: logging.exception("Failed adding user to list") + + # Send email notificcation to maintainer, only in prod + if DEPLOY_ENV == "production": + to = "ilari.ojakorpi@sahkoinsinoorikilta.fi" + subject = "Web error: Failed adding to google groups" + body = "Error code: {}\nError details: {}\nEmail that was not added: {}\n\nAdd user manually to jäsenet groups.".format( + err.status_code, err.error_details, email + ) - to = "ilari.ojakorpi@sahkoinsinoorikilta.fi" - subject = "Web error: Failed adding to google groups" - body = "Error code: {}\nError details: {}\nEmail that was not added: {}\n\nAdd user manually to jäsenet groups.".format( - err.status_code, err.error_details, email - ) - - send_email(to, subject, body) + send_email(to, subject, body) From 34659403a891679b632c9b304ea07f41d00cc135 Mon Sep 17 00:00:00 2001 From: Ojakoo Date: Sat, 6 Aug 2022 11:03:39 +0300 Subject: [PATCH 8/8] lint --- sikweb/settings.py | 2 +- webapp/utils.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sikweb/settings.py b/sikweb/settings.py index b18ebcc..e2d4af5 100644 --- a/sikweb/settings.py +++ b/sikweb/settings.py @@ -82,7 +82,7 @@ DATABASES = { # Google api settings GROUP_KEY = os.getenv("GROUP_KEY", "") -GOOGLE_SERVICE_ACCOUNT = json.loads(os.getenv("GOOGLE_CREDS_JSON", '{}')) +GOOGLE_SERVICE_ACCOUNT = json.loads(os.getenv("GOOGLE_CREDS_JSON", "{}")) # JWT authentication SIMPLE_JWT = { diff --git a/webapp/utils.py b/webapp/utils.py index 672e5c0..71f29b8 100644 --- a/webapp/utils.py +++ b/webapp/utils.py @@ -147,7 +147,7 @@ def add_to_mailinglist(email: str): pass else: logging.exception("Failed adding user to list") - + # Send email notificcation to maintainer, only in prod if DEPLOY_ENV == "production": to = "ilari.ojakorpi@sahkoinsinoorikilta.fi"