Skip to content

Instantly share code, notes, and snippets.

View sanjaykrishnan's full-sized avatar

sanjay krishnan sanjaykrishnan

  • Sayone Technologies
  • Kerala
View GitHub Profile
@sanjaykrishnan
sanjaykrishnan / export_users.py
Created December 20, 2018 04:58
Management Command
from django.core.management import BaseCommand
from apps.accounts.admin import UserResource
from django.contrib.auth import get_user_model
import time
class Command(BaseCommand):
# Show this when the user types help
help = "Export all User data"
@sanjaykrishnan
sanjaykrishnan / sum.js
Created January 17, 2019 13:27
Sum of 2 fields using jquery
$(document).ready(function() {
$(".expenses").on('keyup change', calculateSum);
});
function calculateSum() {
var $input = $(this);
var $row = $input.closest('tr');
var sum = 0;
$row.find(".expenses").each(function() {
@sanjaykrishnan
sanjaykrishnan / gist:1d08a4aeb6d1393e5d61023cbafeb503
Last active April 25, 2019 15:32
Create New DB and user in postgres
sudo -u postgres psql
create role db_user with password 'db_pass';
create database db_name with owner db_user;
alter role db_user WITH LOGIN;
@sanjaykrishnan
sanjaykrishnan / Postgres_dump_restore.txt
Created April 30, 2019 04:41
Postgres dump restore
pg_restore -U fmfb_user -h localhost --password -d fmfb_db < /home/sayone/Downloads/12sep18-dump
from django.db.models import OuterRef, Subquery
unique_users_query = (CouponApplication
.objects
.all()
.filter(coupon=OuterRef('id'))
.values('user_id') # group cities by state
.order_by() # reset ordering
.annotate(cnt=Count('*'))
.values('cnt')[:1] # Only have 1 row & 1 value allowed
@sanjaykrishnan
sanjaykrishnan / pg_dump.txt
Created August 5, 2019 07:40
Postgres Dump
pg_dump -U username -p 5433 -h localhost -F c database_name > dk_dump.dump
@sanjaykrishnan
sanjaykrishnan / base.html
Last active May 11, 2021 11:59
Django messages integration with toastr
<!-- Download the js and css file and place at the below specified location. -->
<!-- In html add this to head tag -->
<link href="{% static 'css/toastr.min.css' %}" rel="stylesheet">
<!-- In html add this just before end of body tag -->
<script src="{% static 'js/toastr.js' %}" type="text/javascript"></script>
@sanjaykrishnan
sanjaykrishnan / query_counter_mixin.py
Created August 29, 2019 10:56
Mixin to print query count and time of execution.
from django.db import connection
from django.http import HttpResponse
from django.utils.deprecation import MiddlewareMixin
# from django.utils.log import getLogger
# logger = getLogger(__name__)
class QueryCountDebugMiddleware(MiddlewareMixin):
"""
This middleware will log the number of queries run
@sanjaykrishnan
sanjaykrishnan / datatables.py
Last active September 13, 2019 11:17
jquery datatables with django
from django.db.models import QuerySet
from django.views import View
class DataTableMixin(View):
queryset = None
columns = None
serializer_class = None
page_size = 10
def get_queryset(self):