Skip to content

Instantly share code, notes, and snippets.

View DmytroLitvinov's full-sized avatar
🇺🇦

Dmytro Litvinov DmytroLitvinov

🇺🇦
View GitHub Profile
@DmytroLitvinov
DmytroLitvinov / ADR_template.md
Created March 11, 2024 08:47
Decision record template

Decision record template (based on Michael Nygard template)

This is the template in Documenting architecture decisions - Michael Nygard.

In each ADR file, write these sections:

Title

The title is not supposed to be about the decision; it is the decision. You see this vagueness in ADR titles like "Database decision". The title is not supposed to be about the decision; it is the decision.

@DmytroLitvinov
DmytroLitvinov / update.py
Last active March 28, 2021 06:46 — forked from bmispelon/update.py
[Django ORM] Updating a JSONField based on the value of another field
"""
How to update JSONField based on the value of another field.
For example:
class MyModel(models.Model):
name = models.CharField(...)
data = models.JSONField()
How to update the MyModel table to store the `name` field inside the `data`
@DmytroLitvinov
DmytroLitvinov / gunicorn_start
Last active April 25, 2017 07:44
[gunicorn_start example] Example file of gunicorn_start inside virtualenv #tags: gunicorn
#!/bin/bash
NAME="instagram_app_django" # Name of the application
DJANGODIR=/webapps/dillysocks/instagram-app-django/instagram-app # Django project directory
SOCKFILE=/webapps/dillysocks/instagram-app-django/run/gunicorn.sock # we will communicte using this unix socket
USER=dillysocks # the user to run as
GROUP=webapps # the group to run as
NUM_WORKERS=3 # how many worker processes should Gunicorn spawn
DJANGO_SETTINGS_MODULE=instagram_service.prod_settings # which settings file should Django use
DJANGO_WSGI_MODULE=instagram_service.wsgi # WSGI module name
@DmytroLitvinov
DmytroLitvinov / pip_commands.md
Created March 17, 2017 20:14
[PIP commands] Usefull pip commands for working with python packages. #tags: python, pip
  • Uninstall anything not in requirements.txt pip freeze | grep -v -f requirements.txt - | grep -v '^#' | xargs pip uninstall -y
@DmytroLitvinov
DmytroLitvinov / unique_slug_django.py
Last active February 10, 2017 10:05
Create unique slug field in Django models.
def save(self, *args, **kwargs):
self.do_unique_slug()
super(Klass, self).save(*args, **kwargs)
def do_unique_slug(self):
"""
Ensures that the slug is always unique for this post
"""
if not self.id:
# make sure we have a slug first
@DmytroLitvinov
DmytroLitvinov / messages.html
Last active September 13, 2021 22:18
[Django middleware for AJAX messages] Middleware for JSON responses. It adds to each JSON response array with messages from django.contrib.messages framework. It allows handle messages on a page with javascript. Django 1.10. #tags: django, python, ajax
<div id="messages">
{% for message in messages %}
<div {% if message.tags %}class="alert alert-dismissable alert-{{ message.tags }}"{% endif %}>
<a class="close" data-dismiss="alert" href="#">&times;</a>
{{ message }}
</div>
{% endfor %}
</div>
@DmytroLitvinov
DmytroLitvinov / templatetags.py
Created February 8, 2017 07:42
Django query_transform templatetag
from django import template
register = template.Library()
@register.simple_tag(takes_context=True)
def query_transform(context, **kwargs):
'''
Returns the URL-encoded querystring for the current page,
updating the params with the key/value pairs passed to the tag.
1. SELECT DISTINCT status FROM tasks ORDER BY name ASC;
2. SELECT count(*) projects.name FROM tasks INNER JOIN projects ON tasks.project_id=projects.id GROUP BY projects.id ORDER BY count(*) DESC;
3. SELECT count(*) projects.name FROM tasks INNER JOIN projects ON tasks.project_id=projects.id GROUP BY projects.id ORDER BY projects.name ASC;
4. SELECT * FROM tasks WHERE name LIKE "N%";
5. SELECT p.name as [Project name], count(tasks) as Quantity FROM projects as p INNGER JOIN tasks as t ON p.id=t.project_Id WHERE mid(p.name, len(name), 1) = 'a' GROUP BY p.name;
6. SELECT name, count(name) as CountOf FROM tasks GROUP BY name, CountOf HAVING count(name) > 1 ORDER BY name ASC;
7. SELECT t.name, t.status, count(*) as "Quantity" FROM projects as p INNER JOIN tasks as t ON p.id=t.project_id WHERE p.name='Garage' GROUP BY t.name, t.status HAVING count(t.name, t.status) > 1 ORDER BY count(t.name, t.status) DESC;
8. SELECT p.name FROM projects as p, tasks as t WHERE p.id=t.project_id AND t.status = 'completed' GROUP BY