Skip to content

Instantly share code, notes, and snippets.

View abhiabhi94's full-sized avatar
😃
हम दोनों इस अकाउंट का उपयोग करते हैं।

Abhyudai abhiabhi94

😃
हम दोनों इस अकाउंट का उपयोग करते हैं।
View GitHub Profile
@abhiabhi94
abhiabhi94 / get_django_translations.py
Created November 25, 2020 13:31
Generate translation into a django project for a specific language
"""
This file can be used to generate translation into a django project for a specific language.
Things to keep in mind:
- This may not work on larger files.
- This script not be helpful for(it should be able to translate almost everything else):
- Strings that span multiple lines.
- Strings that contain some parameter.(e.g `msg_id "This {value} is strange"`)
- Install googletrans using the command `pip install googletrans`(https://github.com/ssut/py-googletrans).\
Many thanks to them for this project
@abhiabhi94
abhiabhi94 / deploy.sh
Last active November 29, 2020 08:14
An automated script to deploy a django project.
# exit if any command fails
set -e
# this function redirects and prints error when an error is encountered
exit_on_error(){
exit_code=$1
last_command=$2
if [ $exit_code -ne 0 ]; then
>&2 echo "\"${last_command}\" command failed with exit code ${exit_code}."
exit $exit_code
@abhiabhi94
abhiabhi94 / permissions.sh
Last active May 2, 2020 09:23
Commands to set up permissions for a django project with apache
project_name=staycurious
db_file_name=db.sqlite3
sudo chown :www-data ~/$project_name/db.sqlite3
# This will work only for sqlite
sudo chmod 664 ~/$project_name/$db_file_name
sudo chown :www-data ~/$project_name/
sudo chown -R :www-data ~/$project_name/media
sudo chmod -R 775 ~/$project_name/media
@abhiabhi94
abhiabhi94 / TwoWayDict.py
Created April 5, 2020 09:03
Create a two way dict to be useful for symmetric mapping
class SymmetricMapping(dict):
"""
Construct a bidirectional mapping when a list of tuple is passed.
The mapping is usable as a two way dictionaty which has the same length\
as the original one.
Example usage:
obj = SymmetricMapping([[(1, "Male"), (2, "Female"), (3, "Non-binary")]])
>>> obj
{
@abhiabhi94
abhiabhi94 / cool_timesince.py
Last active February 8, 2020 12:30
A django template tag display time in a more intutive format e.g. Just now, 2 hours ago, 3 weeks ago etc. Makes use of the default timesince filter in django.
from datetime import timedelta
from django import template
from django.utils import timezone
from django.utils.timesince import timesince
register = template.Library()
@register.filter(name='cool_timesince', is_safe=False)
def cool_timesince(val, now=1):
@abhiabhi94
abhiabhi94 / cool_num.py
Last active February 9, 2020 10:56
A django template tag to display numbers in a more readable format e.g: 1K, 123.4K, 111.42M
from django import template
register = template.Library()
@register.filter(name='cool_view', is_safe=False)
def cool_num(val, precision=2):
"""
Convert numbers to a cool format e.g: 1K, 123.4K, 111.42M.
Return