From 594c7011132efb6a991175f7fd4eac7ab7d6500a Mon Sep 17 00:00:00 2001 From: Praneeth Bhandaru Date: Mon, 19 Oct 2020 21:31:29 -0400 Subject: [PATCH 1/4] Added Notion Updater Functionality (NO CSS) --- .gitignore | 3 + config/settings.py | 16 +++-- config/urls.py | 3 +- my_secrets/definitions.py | 2 + my_secrets/secrets.py.sample.py | 11 +++ notion_updater/__init__.py | 0 notion_updater/admin.py | 5 ++ notion_updater/apps.py | 5 ++ notion_updater/migrations/0001_initial.py | 21 ++++++ notion_updater/migrations/__init__.py | 0 notion_updater/models.py | 19 ++++++ notion_updater/templates/meeting.html | 8 +++ notion_updater/templates/test.html | 13 ++++ notion_updater/tests.py | 3 + notion_updater/urls.py | 7 ++ notion_updater/views.py | 81 +++++++++++++++++++++++ requirements.txt | 1 + static/css/base.css | 4 ++ static/css/meeting.css | 30 +++++++++ templates/base.html | 18 +++++ 20 files changed, 243 insertions(+), 7 deletions(-) create mode 100644 my_secrets/secrets.py.sample.py create mode 100644 notion_updater/__init__.py create mode 100644 notion_updater/admin.py create mode 100644 notion_updater/apps.py create mode 100644 notion_updater/migrations/0001_initial.py create mode 100644 notion_updater/migrations/__init__.py create mode 100644 notion_updater/models.py create mode 100644 notion_updater/templates/meeting.html create mode 100644 notion_updater/templates/test.html create mode 100644 notion_updater/tests.py create mode 100644 notion_updater/urls.py create mode 100644 notion_updater/views.py create mode 100644 static/css/base.css create mode 100644 static/css/meeting.css create mode 100644 templates/base.html diff --git a/.gitignore b/.gitignore index a1b194b..e4e9202 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,9 @@ led / optimized / DLL files __pycache__/ *.py[cod] *$py.class +secrets.py +static_files +.idea/ # C extensions *.so diff --git a/config/settings.py b/config/settings.py index 81478af..ebad241 100644 --- a/config/settings.py +++ b/config/settings.py @@ -11,7 +11,10 @@ https://docs.djangoproject.com/en/3.1/ref/settings/ """ from pathlib import Path -from my_secrets import secrets +try: + from my_secrets.secrets import * +except ImportError: + pass # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent @@ -20,9 +23,6 @@ BASE_DIR = Path(__file__).resolve().parent.parent # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/ -# SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = secrets.SECRET_KEY - # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True @@ -38,7 +38,8 @@ INSTALLED_APPS = [ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', - + 'pages', + 'notion_updater', 'django_extensions', 'django_secrets' ] @@ -58,7 +59,7 @@ ROOT_URLCONF = 'config.urls' TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', - 'DIRS': [], + 'DIRS': [BASE_DIR / 'templates', BASE_DIR / 'notion_updater/templates'], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ @@ -122,3 +123,6 @@ USE_TZ = True # https://docs.djangoproject.com/en/3.1/howto/static-files/ STATIC_URL = '/static/' +import os +STATIC_ROOT = os.path.join(BASE_DIR, 'static_files') +STATICFILES_DIRS = [BASE_DIR / "static"] diff --git a/config/urls.py b/config/urls.py index d15c2c2..430cb55 100644 --- a/config/urls.py +++ b/config/urls.py @@ -14,8 +14,9 @@ Including another URLconf 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin -from django.urls import path +from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), + path('notes/', include('notion_updater.urls')) ] diff --git a/my_secrets/definitions.py b/my_secrets/definitions.py index 67c7212..3c4e567 100644 --- a/my_secrets/definitions.py +++ b/my_secrets/definitions.py @@ -8,4 +8,6 @@ SECRET_KEYS = [ # start with your Django secret key like this: "SECRET_KEY", + "NOTION_URL", + "NOTION_COOKIE", ] diff --git a/my_secrets/secrets.py.sample.py b/my_secrets/secrets.py.sample.py new file mode 100644 index 0000000..3e6e702 --- /dev/null +++ b/my_secrets/secrets.py.sample.py @@ -0,0 +1,11 @@ +# Django +SECRET_KEY = "" # Add your django secret key + +# Notion Client +NOTION_URL = "" # Add the notion url manually +NOTION_COOKIE = "" # Follow the steps listed below + +# 1. Login to notion and navigate to the home page of your page. (Not in Guest or Incognito) +# 2. Open up developer tools by pressing Ctrl + Shift + i, and navigate to the application tab +# 3. Click on the cookie dropdown and click on the single cookie that is located there. +# 4. Copy the token_v2 info and paste it into NOTION_COOKIE diff --git a/notion_updater/__init__.py b/notion_updater/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/notion_updater/admin.py b/notion_updater/admin.py new file mode 100644 index 0000000..0ce7c5c --- /dev/null +++ b/notion_updater/admin.py @@ -0,0 +1,5 @@ +from django.contrib import admin +from .models import NotionPage + +# Register your models here. +admin.site.register(NotionPage) \ No newline at end of file diff --git a/notion_updater/apps.py b/notion_updater/apps.py new file mode 100644 index 0000000..99f2bc8 --- /dev/null +++ b/notion_updater/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class NotionUpdaterConfig(AppConfig): + name = 'notion_updater' diff --git a/notion_updater/migrations/0001_initial.py b/notion_updater/migrations/0001_initial.py new file mode 100644 index 0000000..60aa7fb --- /dev/null +++ b/notion_updater/migrations/0001_initial.py @@ -0,0 +1,21 @@ +# Generated by Django 3.1.2 on 2020-10-19 21:40 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='NotionPage', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('url', models.URLField(max_length=300)), + ], + ), + ] diff --git a/notion_updater/migrations/__init__.py b/notion_updater/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/notion_updater/models.py b/notion_updater/models.py new file mode 100644 index 0000000..97a1f04 --- /dev/null +++ b/notion_updater/models.py @@ -0,0 +1,19 @@ +from django.db import models +from django.conf import settings +from notion.client import NotionClient + +client = NotionClient(token_v2=settings.NOTION_COOKIE) + + +# Create your models here. +class NotionPage(models.Model): + url = models.URLField(max_length=300) + page = None + + def __init__(self, *args, **kwargs): + super(NotionPage, self).__init__(*args, **kwargs) + if self.url: + self.page = client.get_block(self.url) + + def __str__(self): + return str(self.page and self.page.title) diff --git a/notion_updater/templates/meeting.html b/notion_updater/templates/meeting.html new file mode 100644 index 0000000..27e1639 --- /dev/null +++ b/notion_updater/templates/meeting.html @@ -0,0 +1,8 @@ +{% extends 'base.html' %} +{% load static %} +{% block css %} + +{% endblock css %} +{% block body %} +{{ html|safe }} +{% endblock body %} \ No newline at end of file diff --git a/notion_updater/templates/test.html b/notion_updater/templates/test.html new file mode 100644 index 0000000..9face0e --- /dev/null +++ b/notion_updater/templates/test.html @@ -0,0 +1,13 @@ + + + + + Test + + + +
+{{ html|safe }} +
+ + \ No newline at end of file diff --git a/notion_updater/tests.py b/notion_updater/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/notion_updater/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/notion_updater/urls.py b/notion_updater/urls.py new file mode 100644 index 0000000..737ef65 --- /dev/null +++ b/notion_updater/urls.py @@ -0,0 +1,7 @@ +from django.urls import path +import notion_updater.views as views + +urlpatterns = [ + path('', views.test), + path('meeting//', views.show_meeting, name='show_meetings'), +] \ No newline at end of file diff --git a/notion_updater/views.py b/notion_updater/views.py new file mode 100644 index 0000000..098c54a --- /dev/null +++ b/notion_updater/views.py @@ -0,0 +1,81 @@ +from django.shortcuts import render +from .models import NotionPage +from django.conf import settings + + +# Create your views here. +def test(request): + html = '' + + page = NotionPage.objects.get(url=settings.NOTION_URL).page + + meeting_block = None + for block in page.children: + if block.id == '383b2866-a0ae-4f4e-b246-4131117721c0': + meeting_block = block.collection + break + + for meeting in meeting_block.get_rows(): + html += f'

{meeting.title}

' + + html += '
' + return render(request, 'test.html', {'html': html}) + + +def has_children(block): + try: + block.children + return True + except AttributeError: + return False + + +def pprint(dicti): + print('{') + for k in dicti.keys(): + print(f'\t{k.title} : {dicti[k]}') + print('}') + + +def getHtml(html_dict, meeting_dict, node): + string = html_dict[node] + temp = '' + for child in meeting_dict.get(node, {}): + temp += getHtml(html_dict, meeting_dict, child) + return string.format(temp) + + +def show_meeting(request, meeting_id): + + page = NotionPage.objects.get(url=settings.NOTION_URL).page + + meeting = None + for block in page.children: + if block.id == '383b2866-a0ae-4f4e-b246-4131117721c0': + for row in block.collection.get_rows(): + if row.id == meeting_id: + meeting = row + break + + meeting_dict = dict() + html_dict = {meeting: '

' + meeting.title.strip() + '

'} + + q = [meeting] + + while q and has_children(temp := q.pop(0)): + for children in temp.children: + + if children.parent in meeting_dict.keys(): + meeting_dict[children.parent].append(children) + else: + meeting_dict[children.parent] = [children] + + q.append(children) + + block_type = str(type(children)).replace('', '').replace('notion.block.', '').replace("'", "").strip() + html_dict[children] = "
  • " + children.title.strip() + "
      {}
  • " + + html = getHtml(html_dict, meeting_dict, meeting) + print(html) + + return render(request, "meeting.html", {'html': html}) diff --git a/requirements.txt b/requirements.txt index 7c44035..1832db4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,3 +6,4 @@ future==0.18.2 pytz==2020.1 six==1.15.0 sqlparse==0.4.1 +notion diff --git a/static/css/base.css b/static/css/base.css new file mode 100644 index 0000000..0c81722 --- /dev/null +++ b/static/css/base.css @@ -0,0 +1,4 @@ +.content { + padding: 15px; + margin: 10px; +} \ No newline at end of file diff --git a/static/css/meeting.css b/static/css/meeting.css new file mode 100644 index 0000000..1a1b82a --- /dev/null +++ b/static/css/meeting.css @@ -0,0 +1,30 @@ +@import url('https://fonts.googleapis.com/css2?family=Lemonada&display=swap'); +h2 { + font-size: 40px; + margin-top: 10px; + text-align: center; + font-family: 'Lemonada', cursive; +} +.BulletedListBlock { +} +.TodoBlock { + font-weight: bold; +} +.SubheaderBlock { + list-style-type: none; + padding: 0px; + margin: 0px; + font-size: 30px; + margin-top: 5px; + font-family: 'Lemonada', cursive; +} +.SubsubheaderBlock { + list-style-type: none; + padding: 0px; + margin: 0px; + font-size: 30px; + margin-top: 5px; + font-family: 'Lemonada', cursive; +} +.TextBlock { +} \ No newline at end of file diff --git a/templates/base.html b/templates/base.html new file mode 100644 index 0000000..086c05e --- /dev/null +++ b/templates/base.html @@ -0,0 +1,18 @@ +{% load static %} + + + + + TJ 2023 - {% block title %}{% endblock title %} + + {% block css %}{% endblock css %} + {% block js %}{% endblock js %} + + +
    + {% block body %} + + {% endblock body %} +
    + + \ No newline at end of file From da2ae820eefa9280598506b10df3c9a7bc04ebae Mon Sep 17 00:00:00 2001 From: Rushil Umaretiya <2023rumareti@tjhsst.edu> Date: Mon, 19 Oct 2020 21:59:19 -0400 Subject: [PATCH 2/4] added notion to deps --- requirements.txt | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 1832db4..40ec83f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,9 +1,23 @@ asgiref==3.2.10 +beautifulsoup4==4.9.3 +bs4==0.0.1 +cached-property==1.5.2 +certifi==2020.6.20 +chardet==3.0.4 +commonmark==0.9.1 +dictdiffer==0.8.1 Django==3.1.2 django-extensions==3.0.9 django-secrets==1.0.2 future==0.18.2 +idna==2.10 +notion==0.0.25 +python-slugify==4.0.1 pytz==2020.1 +requests==2.24.0 six==1.15.0 +soupsieve==2.0.1 sqlparse==0.4.1 -notion +text-unidecode==1.3 +tzlocal==2.1 +urllib3==1.25.11 From 390b3f704b6c9d6e4425895b582caa1a77053f98 Mon Sep 17 00:00:00 2001 From: Rushil Umaretiya <2023rumareti@tjhsst.edu> Date: Tue, 20 Oct 2020 01:21:22 -0400 Subject: [PATCH 3/4] moved notions_updater to notes, merged the settings and apps --- config/settings.py | 6 +++--- config/urls.py | 4 ++-- {notion_updater => notes}/__init__.py | 0 {notion_updater => notes}/admin.py | 0 notes/apps.py | 5 +++++ {notion_updater => notes}/models.py | 0 {static => notes/static/notes}/css/base.css | 0 .../static/notes}/css/meeting.css | 0 .../templates/notes}/base.html | 0 .../templates/notes}/meeting.html | 2 +- .../templates/notes}/test.html | 0 {notion_updater => notes}/tests.py | 0 {notion_updater => notes}/urls.py | 2 +- {notion_updater => notes}/views.py | 0 notion_updater/apps.py | 5 ----- notion_updater/migrations/0001_initial.py | 21 ------------------- notion_updater/migrations/__init__.py | 0 pages/migrations/__init__.py | 0 18 files changed, 12 insertions(+), 33 deletions(-) rename {notion_updater => notes}/__init__.py (100%) rename {notion_updater => notes}/admin.py (100%) create mode 100644 notes/apps.py rename {notion_updater => notes}/models.py (100%) rename {static => notes/static/notes}/css/base.css (100%) rename {static => notes/static/notes}/css/meeting.css (100%) rename {templates => notes/templates/notes}/base.html (100%) rename {notion_updater/templates => notes/templates/notes}/meeting.html (83%) rename {notion_updater/templates => notes/templates/notes}/test.html (100%) rename {notion_updater => notes}/tests.py (100%) rename {notion_updater => notes}/urls.py (80%) rename {notion_updater => notes}/views.py (100%) delete mode 100644 notion_updater/apps.py delete mode 100644 notion_updater/migrations/0001_initial.py delete mode 100644 notion_updater/migrations/__init__.py delete mode 100644 pages/migrations/__init__.py diff --git a/config/settings.py b/config/settings.py index fac603c..387beea 100644 --- a/config/settings.py +++ b/config/settings.py @@ -11,7 +11,7 @@ https://docs.djangoproject.com/en/3.1/ref/settings/ """ from pathlib import Path -from my_secrets import secrets +from my_secrets.secrets import * # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent @@ -30,6 +30,7 @@ ALLOWED_HOSTS = [] INSTALLED_APPS = [ 'pages', + 'notes', 'django.contrib.admin', 'django.contrib.auth', @@ -37,8 +38,7 @@ INSTALLED_APPS = [ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', - 'pages', - 'notion_updater', + 'django_extensions', 'django_secrets' ] diff --git a/config/urls.py b/config/urls.py index f6676c1..5ec3a6d 100644 --- a/config/urls.py +++ b/config/urls.py @@ -18,6 +18,6 @@ from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), - path('notes/', include('notion_updater.urls')), + path('notes/', include('notes.urls')), path('', include('pages.urls')) -] \ No newline at end of file +] diff --git a/notion_updater/__init__.py b/notes/__init__.py similarity index 100% rename from notion_updater/__init__.py rename to notes/__init__.py diff --git a/notion_updater/admin.py b/notes/admin.py similarity index 100% rename from notion_updater/admin.py rename to notes/admin.py diff --git a/notes/apps.py b/notes/apps.py new file mode 100644 index 0000000..b6155ac --- /dev/null +++ b/notes/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class NotesConfig(AppConfig): + name = 'notes' diff --git a/notion_updater/models.py b/notes/models.py similarity index 100% rename from notion_updater/models.py rename to notes/models.py diff --git a/static/css/base.css b/notes/static/notes/css/base.css similarity index 100% rename from static/css/base.css rename to notes/static/notes/css/base.css diff --git a/static/css/meeting.css b/notes/static/notes/css/meeting.css similarity index 100% rename from static/css/meeting.css rename to notes/static/notes/css/meeting.css diff --git a/templates/base.html b/notes/templates/notes/base.html similarity index 100% rename from templates/base.html rename to notes/templates/notes/base.html diff --git a/notion_updater/templates/meeting.html b/notes/templates/notes/meeting.html similarity index 83% rename from notion_updater/templates/meeting.html rename to notes/templates/notes/meeting.html index 27e1639..60729a8 100644 --- a/notion_updater/templates/meeting.html +++ b/notes/templates/notes/meeting.html @@ -1,4 +1,4 @@ -{% extends 'base.html' %} +{% extends 'notion/base.html' %} {% load static %} {% block css %} diff --git a/notion_updater/templates/test.html b/notes/templates/notes/test.html similarity index 100% rename from notion_updater/templates/test.html rename to notes/templates/notes/test.html diff --git a/notion_updater/tests.py b/notes/tests.py similarity index 100% rename from notion_updater/tests.py rename to notes/tests.py diff --git a/notion_updater/urls.py b/notes/urls.py similarity index 80% rename from notion_updater/urls.py rename to notes/urls.py index 737ef65..ae2334a 100644 --- a/notion_updater/urls.py +++ b/notes/urls.py @@ -1,5 +1,5 @@ from django.urls import path -import notion_updater.views as views +from . import views urlpatterns = [ path('', views.test), diff --git a/notion_updater/views.py b/notes/views.py similarity index 100% rename from notion_updater/views.py rename to notes/views.py diff --git a/notion_updater/apps.py b/notion_updater/apps.py deleted file mode 100644 index 99f2bc8..0000000 --- a/notion_updater/apps.py +++ /dev/null @@ -1,5 +0,0 @@ -from django.apps import AppConfig - - -class NotionUpdaterConfig(AppConfig): - name = 'notion_updater' diff --git a/notion_updater/migrations/0001_initial.py b/notion_updater/migrations/0001_initial.py deleted file mode 100644 index 60aa7fb..0000000 --- a/notion_updater/migrations/0001_initial.py +++ /dev/null @@ -1,21 +0,0 @@ -# Generated by Django 3.1.2 on 2020-10-19 21:40 - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - initial = True - - dependencies = [ - ] - - operations = [ - migrations.CreateModel( - name='NotionPage', - fields=[ - ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('url', models.URLField(max_length=300)), - ], - ), - ] diff --git a/notion_updater/migrations/__init__.py b/notion_updater/migrations/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/pages/migrations/__init__.py b/pages/migrations/__init__.py deleted file mode 100644 index e69de29..0000000 From 2174bb8fa5b8084435e9930628f61137d3906bb0 Mon Sep 17 00:00:00 2001 From: Rushil Umaretiya <2023rumareti@tjhsst.edu> Date: Tue, 20 Oct 2020 01:25:23 -0400 Subject: [PATCH 4/4] fixed template routes --- notes/views.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/notes/views.py b/notes/views.py index 098c54a..b1dce7f 100644 --- a/notes/views.py +++ b/notes/views.py @@ -19,7 +19,7 @@ def test(request): html += f'

    {meeting.title}

    ' html += '
    ' - return render(request, 'test.html', {'html': html}) + return render(request, 'notes/test.html', {'html': html}) def has_children(block): @@ -78,4 +78,4 @@ def show_meeting(request, meeting_id): html = getHtml(html_dict, meeting_dict, meeting) print(html) - return render(request, "meeting.html", {'html': html}) + return render(request, "notes/meeting.html", {'html': html})