Skip to content

ahuimanu/CIDM6325

Repository files navigation

CIDM6325 - Fall 2024

README Outline:

  • Chapters 1-3 build the blog (assignment)
  • Chapters 4-7 build the bookmarks social website (assignment)
  • Chapters 8-11 build an ecommerce application (assignment)
  • Chapters 12-18 build a CMS for an online elearning platform (basis of final project)

Video Notes

  • Technical Notes CH 2 - 7 - Published October 2024
    • Docker - Part 1
    • Emails and Email Backends - Part 2
    • Docker and Postgres - Part 3
    • Social SSO - Part 4
  • Methods to implement Recipe (ch 1 - 3) - Not published yet
  • Working with Bootstrap - Not published yet
  • Technical Notes CH 8 - 11
    • Celery and Rabbit MQ - Part 1

SendGrid and Anymail

Steps from https://anymail.dev/en/v12.0/esps/sendgrid/

  1. pip install "django-anymail[sendgrid]"
  2. add anymail to INSTALLED_APPS

back to outline

Docker Stuff

WSL2 is probably the best option for Windows. WSL2

You can use the shell from within the Docker Interface Docker Interface

Or, if on Windows, use PowerShell. If on Mac, Terminal is fine. PowerShell

docker run --name=blog_db2 -e POSRGRES_DB=blog -e POSTGRES_USER=blog -e POSTGRES_PASSWORD=xxxxx -p 5432:5432 -d postgres:latest

steps:

  1. docker pull postgres - I left the version off, so it gets the latest
  2. docker run --name=blog_db -e POSRGRES_DB=blog -e POSTGRES_USER=blog -e POSTGRES_PASSWORD=password -p 5432:5432 -d postgres:latest - note, you will realize the best results if you just stick to what Antonio is showing.
  3. Although running pip install psycopg is a safe approach, it may not work entirely when doing a Docker container with Postgres, this mainly due to Postgres being virtualized where none of the resources used by Postgres are actually on your local system (Docker is). Your likely error message here will be "psycopg or psycopg2 not installed/found."
  4. So, in case of issues with step 3 above, try this: pip install "psycopg[binary]" - this is recommended in the psycopg documentation.
  5. Its also possible to rent a Postgres server from a number of places such as Digital Ocean.
  6. The best way to see if all of this goes well is to run python manage.py check - this checks to see if Django will properly "bootstrap" itself to life. A good resposne is "System check identified no issues (0 silenced)."
  7. dumpdata and loaddata may have failed depending on whether you followed the book exactly. Not a huge deal in development.
  8. You can run/pause/stop the Postgres Docker container using the desktop application.

Social Auth Notes

Clearly, the screens at the Developer Console for Google's Cloud Services are slightly different than the book. However, the screens are similar.

Other notes:

  1. It is CRITICAL that, in Windows, the hosts file is edited with a text editor that is started with evelated permissions (right click and "Run as Administrator")
  2. the Client ID from the "Client ID for Web Application" screen is the GOOGLE_OAUTH2_KEY
  3. the Client secrete from from the "Client ID for Web Application" is the GOOGLE_OAUTH2_SECRET
  4. django social auth is doing the rest of the "magic"

Google Cloud:

  1. The URL for the overall cloud developer console is: Google Cloud Console
  2. A new project screen: New Project
  3. Select your project: Select Project
  4. Select OAuth Consent screen: Select OAUTH
  5. Enter App Info (name and Authorized Domains): Enter OAuth Info
  6. Add Test User (must be the email you want to social auth with): Add Test User
  7. Create Credentials (dropdown shown and different from book): Select Credentials
  8. Create OAuth Cliend ID - Application Type "Web Application"; Add Authorized JavaScript Origings: Add Authorized redirect URIs: Client ID Settings
  9. Results Dialog - all info needed are on results diaglog: OAuth Dialog
  10. Credentials Dashboard - OAuth Credentials Dashboard
  11. Credentials Summary - OAuth Credentials Summary

RabbitMQ and Celery in Python

RabbitMQ RabbitMQ will run via Docker. The commands in the book are straightforward:

  1. Make sure Docker Desktop is running
  2. grab the latest rabbitmq-management image: docker pull rabbitmq:management
  3. run the image in a container: docker run -it --rm --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:management

Celery

  1. On Windows, you will likely need to run a separate Git-Bash session with administrative rights (right-click and "Run as Administrator")
  2. If not you will likely get this error [WinError 5] Access Is Denied, however, that is not the only source of this error on windows.

Let's look further into this:

  1. For now, don't run Celery within VS Code (on windows) until I find a VS Code setting for you.
  2. You WILL need to activiate the virtual environment in this new (separate) Git-Bash or PowerShell terminal
  3. You will also want to limit the number of concurrent worker to something small for our testing purposes.
  4. Lastly, Windows has a hard time spawning new subprocesses and will utilizately thrlow the Access Denied error.
  5. Although not mentioned in the book (because Antonio is likely developing on a Mac), we can use a Python "coroutine" trick using the gevent package pip install gevent which utilizes greenlet to "fake" Python into using a single process as threads or coroutines. After installing gevent we can move on (as far as I can tell, this is Windows stuff).
  6. So, try adjusting the command to the following: celery -A myshop worker --loglevel=INFO --concurrency=4 -P gevent and you should achieve success where the errors disappear.

You will be left with output that looks like the book, but the outlet is on the SEPARATE "Run As Admin" window.

pip install gevent

celery -A myshop worker --loglevel=info -P gevent

In the end, sending an email via a message queue like celery and RabbitMQ is going to be a "nice to have" feature in the project.

WeasyPrint and Windows

Again, in many cases, getting things to work on Windows can be, different. This is because most web deployment environments are not Windows based but rather *nix (Linux/Unix/OSX) based.

Doing the pip install WeasyPrint from the book is not going to work for Windows folks.

The truth is that the steps at the WeasyPrint website's documentation are the way to go.

At issue with these instructions is a vital additional set of steps discussed here: https://stackoverflow.com/a/78715011

  1. Install MSYS2 https://www.msys2.org/#installation
  2. After installation MSYS2 execute the following command in MSYS2 shell: pacman -S mingw-w64-x86_64-pango
  3. Then add path C:\msys64\mingw64\bin to enviroment variables and of course add weasyprint to your project. If you don't have experience with this, there are "how to" resouces.
  4. You will find this now works and the easy way to check is (with your virtual environment activated): python manage.py check

Again, we are faced with the following question: so, is WeasyPrint and the ability to print to PDF wor it worth it? It depends, but I'm putting it as a "nice to have" feature. The trick here is to skip those parts.

I think those on OSX will have an easier time.

Do not BOG DOWN.

I18N and gettext

In chapter 11, we again find ourselves faced with challenges as windows users. This speaks volumes about how the web is run as most HTTP servers execute in a Unix/Linux environment (of which Mac OSX is a part of). As windows users, Antonio tells us to:

Of those, almost all of us are using a 64-bit windows, and the static build is likely the safest. In all cases, I elected to use the installer as it will put things in the right place and offer this critical dialog with the needed options:

gettext installer

Sometimes, most of the time, it is a good idea to reboot your computer now as even though this installer should have included the installation directory into your PATH variable, this may not take effect until a reboot. I talk above setting the PATH in an earlier section of this README. NOTE: Rebooting is what made django-admin makemessages --all work for me after failing before a reboot.

FINAL NOTE on the ONLINE SHOP Project

For all of the functions in the online shop example, it is expected that the following are always running:

  1. Docker Desktop
  2. RabbitMQ
  3. Celery
  4. A proper Stripe API connection

The shop won't run without these. I know these external dependencies are a pain, but they represent a very realistic scenario for you to understand.

Educa Notes

Structure of project:

Subject 1
  Course 1
    Module 1
      Content 1 (image)
      Content 2 (text)
    Module 2
      Content 3 (text)
      Content 4 (file)
      Content 5 (video)
      ...

Docker Stuff

I don't specify a version of memchached, so it defaults to latest:

docker pull memcached - defaults to latest

docker run -it --rm --name memcached -p 11211:11211 memcached:latest -m 64

Also for redis:

docker run -it --rm --name redis -p 6379:6379 redis:latest

General Notes

  • DO NOT SKIP READING THE BOOK! Most of the tips, and links at the back of the chapter, provide VITAL clues.

  • DO use Antonio's GitHub Repostitory for book code.

  • We eventually start running the Django debug server wit this command: python manage.py runserver_plus --cert-file cert.crt

  • The Django Secret Key Generator - Source Code for the curious - you need this because the default secret key is not meant for deployment.

  • Commands to run when checking for outdated packages in your Virtual Environment (VE):

    • Be sure that your VE is activated (source .venv/Scripts/activate)
    • check for outdated packages python -m pip --outdated
    • update package(s) as appropriate python -m pip install --upgrade <name-of-package>
  • In addition to the book, this is a good resource: W3Schools Django

  • A Git Cheat Sheet is available as a quick reference for common git commands.

  • Boostrap Crash Course (freecodecamp): Full Bootstrap 5 Tutorial for Beginners

back to outline

Miscellaneous Notes

  • Templates and Bootstrap - I will strongly recommend that you use and develop familiarity with Bootstrap as a CSS framework for all of your work in this class.
  • Classy Class-Based Views - Should you wish to go in this direction: https://ccbv.co.uk/
  • Environment Variables - I have used python-dotenv forever and have done so in my code - Antonio uses python-decouple.
  • Candidate Heroku Replacements - Heroku used to be the best free Django deployment platform - it is no longer free. Here are some replacement services.
    • render.io
    • python anywhere
    • Fly.io
    • Railway

This README file is constructed using MarkDown. Here is another good Markdown reference.

back to outline

About

Repository for CIDM6325 Code

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published