Skip to content

Instantly share code, notes, and snippets.

View oscarychen's full-sized avatar

Oscar Y Chen oscarychen

  • Calgary
View GitHub Profile
@oscarychen
oscarychen / postgres_ltree.sql
Created March 11, 2023 02:07
Postgres Ltree Cheatsheet
CREATE EXTENSION ltree;
CREATE TABLE test (path ltree);
-- Top
-- / | \
-- Science Hobbies Collections
-- / | \
-- Astronomy Amateurs_Astronomy Pictures
@oscarychen
oscarychen / go_notes.md
Last active February 2, 2024 18:27
Go notes

Go commands

go mod init: start new module, this will put a "go.mod" file in the current directory

go get <package>: install dependency

go run <module_name>: Run

go build <module_name>: compile executable

Language basics

@oscarychen
oscarychen / rust_notes.md
Last active November 15, 2022 16:50
Rust notes

Cargo commands

cargo new <project_name>: start new project

cargo run: compile and run project

cargo build: build executable

Language basics

Primitive types

bool: boolean

@oscarychen
oscarychen / drf-exception-handling.md
Last active January 27, 2024 08:39
Exception handling in Django REST Framework

Exception Handling in Django REST Framework

In Django REST Framework views (this includes anything that might be called from a view), anytime when an exception occurs it will get handled by the framework.

  • If the Exception is DRF APIException, or Django PermissionDenied, the View will return the appropriate HTTP response with a HTTP status code and detail about the error.
  • If the Exception is other types of Django or Python Exceptions, HTTP 500 response will be returned.

To provide more customized error response with the appropriate status code, you will want to raise a subclass of APIException:

from rest_framework.exceptions import ValidationError
@oscarychen
oscarychen / csp.md
Last active January 19, 2023 04:07
Content Security Policy explained

Content Security Policy (CSP)

CSP limits our site from making requests to other sites, controls what resources the page is allowed to load. It limits the damage even if malicious code is running in a user's browser within our site's context.

Common examples

  • Content-Security-Policy: default-src ‘self’ Prevents loading resources from other domains. Prevents inline scripts, such as <script>alert('hello')</script>.

  • Content-Security-Policy: default-src ‘self’ *.trusted.com

@oscarychen
oscarychen / xss.md
Last active December 9, 2021 19:27
cross-site scripting explained

Cross-site scripting (XSS)

What is XSS?

  • Unexpected JavaScript code running in an HTML document
  • Unexpected code in SQL query
  • Any code that combines a command with user data is susceptible

Attacker may:

@oscarychen
oscarychen / csrf.md
Last active December 9, 2021 19:22
cross site request forgery explained

Cross Site Request Forgery (CSRF)

Session Hijacking

Cookie sent over unencrypted HTTP connection

Mitigation

Use Secure attribute on cookie to prevent it from being sent over unencrypted connection: Set-Cookie: key=value; Secure

@oscarychen
oscarychen / cookies_same_origin_policy.md
Last active May 15, 2024 11:42
Cookies and Same Origin Policy explained

Cookies and Same Origin Policy

Origin

origin

_Origin_ is defined as the protocol-host-port tuple

Same Origin Policy

Ensures host document can only be accessed by JavaScript execution context from the same origin.

@oscarychen
oscarychen / Django_hierarchical_tree_data.md
Last active April 22, 2024 05:50
Storing and retrieving tree data structure efficiently in Django - the materialized path approach

Working with hierarchical / tree data structure in PostgresSQL and Django

There are two typical models for dealing with tree data structure in SQL databases: adjacency list model and nested set model. There are several packages for Django that implements these models such as django-mptt, django-tree-queries, and django-treebeard.

In this article, I want to explain a different approach I took in a project for storing hierachichal data, as well as search and retrieval of sub-trees. This approach is similar in principle to materialzed path, which django-treebeard package also provides an implementation. This article will show you how I achieved this without any of the specific packages, and using on

@oscarychen
oscarychen / Django_custom_query.md
Last active December 6, 2021 19:56
Django custom lookup 'startswith' on model field

Django Custom Lookups

This article is based on my Stackoverflow answer - Django query lookup 'startswith' on table fields.

Let's say you have a table of partial postal codes:

| codes  |
----------
| A1A |