Skip to content

Instantly share code, notes, and snippets.

@anhtran
anhtran / sample.py
Created July 26, 2023 04:58
Ví dụ cách sử dụng redis Lock để khóa một tác vụ nào đó trong Python
from redis.lock import Lock
def process_job():
print('worked...')
lock_key = f"lock_save:1234"
lock = Lock(redis_client, lock_key, timeout=3600)
if lock.acquire(blocking=False):
try:
@anhtran
anhtran / sample.md
Created March 1, 2023 09:19
How to mimic style of antd Form Input with Tailwindcss

Step 1: Inspect elements to get the main color of theme. Example: #62bbde

Step 2: Using there compound of following CSS classess:

border border-gray-300/80 
rounded-lg w-full 
py-2 px-2 text-[16px] 
outline-none 
transition-all duration-200 
@anhtran
anhtran / commands.sql
Last active April 4, 2024 00:32
Cách tạo cấu hình cho tiếng Việt trong PostgreSQL (stopwords, ispell)
CREATE TEXT SEARCH DICTIONARY public.vietnamese (
TEMPLATE = pg_catalog.simple,
STOPWORDS = vietnamese
);
CREATE TEXT SEARCH CONFIGURATION public.vietnamese (
COPY = pg_catalog.english
);
ALTER TEXT SEARCH CONFIGURATION public.vietnamese
@anhtran
anhtran / 1.sql
Created September 5, 2022 12:51
Drop all tables with prefix use wildcard - Xóa toàn bộ bảng bắt đầu bằng tiền tố bất kỳ trong PostgreSQL (psql)
# remove all tables start with "django_"
DO $$ DECLARE
r RECORD;
BEGIN
FOR r IN (SELECT tablename FROM pg_tables WHERE tablename like 'django_%') LOOP
EXECUTE 'DROP TABLE IF EXISTS ' || quote_ident(r.tablename) || ' CASCADE';
END LOOP;
END $$;
@anhtran
anhtran / NextProgressbar.js
Created July 23, 2022 08:20
Next.js progress bar with minified CSS and emotion.js
import Router from 'next/router'
import * as NProgress from 'nprogress'
import * as React from 'react'
import { css, Global } from '@emotion/react'
export default function NextProgressbar ({
color = '#29D',
startPosition = 0.3,
stopDelayMs = 200,
height = 3,
@anhtran
anhtran / sample-nginx.conf
Last active July 22, 2022 03:45
Cấu hình mẫu nginx dành cho các website viết bằng Django - Sample nginx configurations for Django website
# === CẤU HÌNH MẪU NGINX DÀNH CHO 1 WEBSITE VIẾT BẰNG DJANGO ===
# Ở đây chúng ta sẽ cài đặt cho 3 domain: mysite.com | static.mysite.com | media.mysite.com
# Ngoài ra, nguyên tắc của tôi là mỗi 1 website sẽ có 1 linux user riêng với những quyền riêng tránh can thiệp vào các user khác.
# INSTRUCTION
# 1. Thay thế `mysite` với tên của domain website;
# 2. Điều chỉnh port 9999 thành port mà bạn sử dụng tại máy chủ WSGI hoặc ASGI.
# 3. Điều chỉnh MEDIA_ROOT và STATIC_ROOT lại cho phù hợp với cấu hình của nginx ở đây.
upstream mysite_django {
@anhtran
anhtran / cmd.sh
Created September 27, 2021 14:51
Setup mysql 8 for Ghost blog
mysqldump -u root -p my_blog_prod > db_backup.sql
mysql -u my_blog_user -p my_blog_prod < db_backup.sql
@anhtran
anhtran / ScrollButton.js
Created December 8, 2020 05:32
Simple scroll to top button for react
// <ScrollButton scrollStepInPx='50' delayInMs='16.66' />
import styled from '@emotion/styled'
import tw from 'twin.macro'
import ArrowUpIcon from 'bootstrap-icons/icons/arrow-up.svg'
import React, { useEffect, useState } from 'react'
import { useWindowScroll } from 'react-use'
function ScrollButton ({ scrollStepInPx, delayInMs }) {
const [intervalId, setIntervalId] = useState(0)
@anhtran
anhtran / remove_accents_vietnamese.js
Last active April 19, 2021 09:37
How to remove all accents in Vietnamese string | Xóa dấu trong tiếng Việt làm slug hoặc để tìm kiếm
const removeAccents = (function () {
const letters1 = 'ÀÁÂÃÈÉÊÌÍÒÓÔÕÙÚĂĐĨŨƠàáâãèéêìíòóôõùúăđĩũơƯĂẠẢẤẦẨẪẬẮẰẲẴẶẸẺẼỀỀỂưăạảấầẩẫậắằẳẵặẹẻẽềềểếỄỆỈỊỌỎỐỒỔỖỘỚỜỞỠỢỤỦỨỪễệỉịọỏốồổỗộớờởỡợụủứừỬỮỰỲỴÝỶỸửữựỳỵỷỹý'
const letters2 = 'AAAAEEEIIOOOOUUADIUOaaaaeeeiioooouuadiuoUAAAAAAAAAAAAAEEEEEEuaaaaaaaaaaaaaeeeeeeeEEIIOOOOOOOOOOOOUUUUeeiioooooooooooouuuuUUUYYYYYuuuyyyyy'
const patternLetters = new RegExp('[' + letters1 + ']', 'g')
const lookupLetters = {}
letters1.split('').forEach(function (letter, i) {
lookupLetters[letter] = letters2[i]
})
@anhtran
anhtran / snippet.py
Created August 19, 2020 04:04
How to check duplicate files in django-filer
import hashlib
from filer.models import Image
def check(file: Image, user=None):
"""
Check duplicate files in django-filer
"""
sha1 = hashlib.sha1()
for chunk in file.chunks():
sha1.update(chunk)