Skip to content

Instantly share code, notes, and snippets.

View jerinisready's full-sized avatar
💭
A catalyst of Change

JK jerinisready

💭
A catalyst of Change
View GitHub Profile
@jerinisready
jerinisready / trim_input_onchange.html
Created May 7, 2018 06:04
trim any spaces at the start of the text box and trim any spaces at the end of the textbox.
<script>
function trim(el){
el.value = el.value.
replace (/(^\s*)|(\s*$)/gi, ""). // removes leading and trailing spaces
replace (/[ ]{2,}/gi," "). // replaces multiple spaces with one space
replace (/\n +/,"\n"); // Removes spaces after newlines
return true;
}
</script>
@jerinisready
jerinisready / django_image_store_as_blob_in_models.py
Created May 7, 2018 10:27
django image store as blob in models.py
class imageStorageAsThumpnail(models.Model)
image = models.ImageField(_("Image"), upload_to='chapter_images')
_thumbnail = models.TextField(
db_column='thumbnail',
blank=True)
def set_thumbnail(self, data):
self._data = base64.b64encode(data)
def get_thumbnail(self):
@jerinisready
jerinisready / gist:1bcc5ca615765d76058860293c3144ec
Created May 13, 2018 10:54 — forked from thraxil/gist:3123935
django project git pre-commit hook
#!/usr/bin/env python
import os
import re
import subprocess
import sys
modified = re.compile('^(?:M|A)(\s+)(?P<name>.*)')
CHECKS = [
@jerinisready
jerinisready / django_api_detail_view.py
Last active June 20, 2018 07:01
APIDetailView Django Rest Framework / DRF Detail View With pk
class APIDetailView(APIView):
authentication_classes = (CsrfExemptSessionAuthentication, BasicAuthentication)
http_method_names = ('get', )
serializer_class = ModelSerializer
def get(self, request, pk, **kwargs):
obj = Chapter.objects.get(id=pk)
serializer = ModelSerializer(obj, context={"request": request})
return Response(serializer.data, status=status.HTTP_200_OK)
@jerinisready
jerinisready / .git_commands
Last active May 21, 2018 15:49
This is how to remove any large files form git history! This is adviced only after proper backup of your repository!
git filter-branch --index-filter 'git rm --cached --ignore-unmatch <file_path_or_pattern>' --tag-name-filter cat -- --all
git push origin --force --all
<style>
.classWithShadow{
-moz-box-shadow: 1px 1px 2px grey, 0 0 25px lightgrey;
-webkit-box-shadow: 1px 1px 2px grey, 0 0 25px lightgrey;
box-shadow: 1px 1px 2px grey, 0 0 25px lightgrey;
}
</style>
<!doctype HTML>
<head></head>
<div class="shadowclass">
@jerinisready
jerinisready / django_bootstrap_models.py
Created June 5, 2018 20:52
Sample Django Form To add Bootstrap's Form Control class to every field.
class BootstrapMixinForm(forms.ModelForm):
"""
Sample Django Form To add Bootstrap's Form Control class to every field.
"""
def __init__(self, *args, **kwargs):
super(BootstrapMixinForm, self).__init__(*args, **kwargs)
for myField in self.fields:
self.fields[myField].widget.attrs['class'] = 'form-control'
@jerinisready
jerinisready / reset_javascript_element.js
Created June 8, 2018 07:05
Sometimes we require to reset a canvas when we want to replace the graph with another set of data. that time simply resigning may cause issue, we can reomove and recreate elements for that purpose
function reset_element(element_id, element_type){
var child = document.getElementById(div_id);
var parent = child.parentElement;
var new_child = document.createElement(element_type);
parent.removeChild(child);
new_child.setAttribute('id', div_id);
parent.appendChild(new_child);
}
// <span id="my_canvas_id"></span>
@jerinisready
jerinisready / load_image_to _model.py
Created July 2, 2018 11:23
If you want to download and save an image from an external url, this snippet might help you!
from django.core.files import File
from django.core.files.uploadedfile import InMemoryUploadedFile
from django.core.files.temp import NamedTemporaryFile
from newspaper import Article
import requests
from myapp.models import Blog, Category, Tags
__author__ = 'jerinisready'
@jerinisready
jerinisready / login_required_mixin_over_drf.py
Last active July 2, 2018 15:14
A Quick Login Required Mixin For Django Rest Framework
from rest_framework.response import Response
import rest_framework.views
class APIView(rest_framework.views.APIView):
""" A Quick Login Required Mixin For Django Rest Framework """
def dispatch(self, request, *args, **kwargs):
if not request.user.is_authenticated():
return Response({'error': 'Please Authenticate to continue'}, status=405)
return super(self.__class__, self).dispatch(request, *args, **kwargs)