Static Files Versioning in Django

Installation

It is pretty easy in 2.2+, just add to your settings:

STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage'

Don’t forget to set:

DEBUG = False

And do collectstatic. This way Django will run over all of your files and search for them in your statics directory.

ValueError: the file <file_path> could not be found

Be careful with urls – they might work on the website, but it the storage module may not identify the paths correctly. This error occurs:

ValueError: The file 'css/plugins/fonts/fontawesome-webfont.eot' could not be found with <django.contrib.staticfiles.storage.ManifestStaticFilesStorage object at 0x12ae4e220>.

Because it has a path like this

fonts/fontawesome-webfont.eot

You need to make it relative:

../../fonts/fontawesome-webfont.eot

RuntimeError: Max post-process passes exceeded.

If you see an error like this:

Post-processing 'All' failed!
RuntimeError: Max post-process passes exceeded.

Then check your @import statements – remove the commented once and try to find where the import is circular.

Conclusion

Versioning does not tolerate uncarefully made static files. You may spend a few hours removing rudimental imports, files, altering paths, removing urls to non-existent files. That’s totally normal.

Celery – schedule tasks with pauses between them

I needed to schedule 10 tasks that have these conditions

  1. A time gap between tasks is fixed (but varies)
  2. No need to pass results of one task to another
  3. Need to read a database in every task (important: at the moment of the execution)

I tried a few options, and this one worked

 ret = chain(task1.si(args).set(countdown=30*1), ..., task10.si(args).set(countdown=30*10)).apply_sync()

Important note – the signature here is not .s() but .si() this way you don’t pass the result of task1 to task2. You set the countdown with set() and not inside the apply_sync().

It is simple but it took me some time to figure out.

A short note about JS

I have been working with JavaScript lately, and I enjoy it so much. It gives me the whole “mobile development vibe” but it is much quicker and easier – no need to build all the time etc. I have to say, it is inclining me to resuming mobile dev activities… not gonna lie.

Just a short note to tell you about the fun I’ve having 😉

macOS (M1/M1 Pro) – ffmpeg with mp3 support and OpenCV

This is not funny – I spent a few evening fixing and fixing and fixing. Now ./configure then make and sudo make, but where is binary? Why is it not is /usr/bin/ OR /usr/local/bin/? Oh you do brew install opencv – but it does not work in terminal? Amazing. I was THIS close to wiping mac os and installing Linux on my macbook guys, THIS CLOSE.

Anyhow it is simple if you do the right thing.

ffmpeg with mp3 support

brew uninstall lame —force
brew uninstall homebrew-ffmpeg/ffmpeg/ffmpeg
brew install homebrew-ffmpeg/ffmpeg/ffmpeg —with-fdk-aac

In your virtualenv

pip uninstall ffmpeg
pip uninstall ffmpeg-python
pip install ffmpeg-python

And then install OpenCV (it is now working with pip):

pip install opencv-python

How to speed up Django Admin: Foreignkey and ManyToManyField

If you have a Foreignkey or ManyToManyField in your admin form, but you don’t need to change it in any way, replace it with a label. Say, you have a model:

class Student(models.Model):
    ...
    class_obj = models.ForeignKey('Class', on_delete=models.CASCADE)

And in your Django Admin you want to have thic field:

@admin.register(Student)
class StudentAdmin(admin.ModelAdmin):
    fields = ('class_obj',)

There is no problem if you have, say, 10-100 Class instances, but if you have thousands of them – the interface would take a lot of time to load.

Instead of that, make a label property and replace the original field with it.

class Student(models.Model):
    ...
    class_obj = models.ForeignKey('Class', on_delete=models.CASCADE)

    @property
    def class_obj_str(self):
        return self.class.name

And then in the ModelAdmin:

@admin.register(Student)
class StudentAdmin(admin.ModelAdmin):
    readonly_fields = ('class_str',)
    fields = ('class_obj_str',)

What I am up to these unsettling days

The world is crazy, but I don’t let it deter me. I am a great believer in doing things during challenging times. So my brother and I are spending our spare (from work) time at Codewars and Hackerrank.

HackerRank

First, I returned to HackerRank to do some SQL, and I completed everything I found relevant to my current studies. It seems like from the lack of use I lost some of my SQL knowledge and I want to fix that.

My modest achievements at HackerRank

I plan to do much more with Problem Solving (there are a few data structures and algorithms I am not yet very familiar with). Maybe I will return and do more of Python, but at this point I just feel bored and uninspired.

SQL Academy

Then I jumped to SQL Academy, and I loved it, but it has too many bugs to my taste

UPD. 24.03.22 – I am a moron. I don’t see bugs, I just missed some details and therefore couldn’t successfully submit a few tasks. Now I bought a Premium account for $10 and realized my mistakes. I use it after I complete the tasks to see how a DB expert would solve it. Can’t wait to finish the task list by end of the week (wish me luck hehe).

One downside for me – it requires MySQL syntax, and I am not really interested in it. I personally find PostgreSQL more accomodating to Python users and those who don’t specialize solely in databases too.

The interface is fantastic and I love how you can see queries and tables and an ERM. I will be completing everything that bugs will let me – it has been very useful to me.

Codewars

Meanwhile my brother has been devoured by Codewars, so after all these websites I returned to it. I have a rather old account there (since 2018), but I haven’t touched it in a while. I have had 5 kyu since forever, now I really want to get 4 kyu, even though it is not just in Python hehe.

My modest achievements at Codewars

In February I upgraded my DS/Algorithms skills with Udemy courses, Yandex Youtube lectures and personal studies here and there. I am excited to return to Problem Solving katas at Codewars with all this acquired knowledge.

Summing Up

All in all, right now I am very much into coding simulators. They bring me a sense of accomplishment that I desperately need right now. Badges and certificates – small achievements you can touch. That brings comfort if anything. So I encourage everybody I know who struggles with reality right now – to do the same. At very least, you came out of it more educated and with brighter prospects.

GitPython – Diff between two branches

My dear brother started his own blog on Airflow, Python, PostgreSQL and Javascript!!!

Congrats, Ilia!

Ilia's Airflow, Python, PostgreSQL, Javascript Journey

Code:

import os
from pathlib import Path 
from git import Repo

# path to repo folder
path = Path(os.path.dirname(os.path.abspath(__file__)))

# repo variable
repo = Repo(path)

# Your last commit of the current branch
commit_feature = repo.head.commit.tree

# Your last commit of the dev branch
commit_origin_dev = repo.commit("origin/dev")
new_files = []
deleted_files = []
modified_files = []

# Comparing 
diff_index = commit_origin_dev.diff(commit_feature)

# Collection all new files
for file in diff_index.iter_change_type('A'):
    new_files.append(file)

# Collection all deleted files
for file in diff_index.iter_change_type('D'):
    deleted_files.append(file)

# Collection all modified files
for file in diff_index.iter_change_type('M'):
    modified_files.append(file)

Analyze:

Let’s imagine the situation:

You are before code review and you need to get all changes between feature and develop with GitPython.
First you need to get last commits from two branches:

# Your last commit of the current branch
commit_dev = repo.head.commit.tree

# Your last commit of the dev branch
commit_origin_dev = repo.commit("origin/dev")

Then get your changes:

View original post 81 more words