Change author of last commit in git

If you use multiple emails in your repos (ex. work and private) you may have encountered a situation where your commit was rejected from the remote repository, due to bad email used. In order to fix that all you need to do is to set the correct e-mail for current repository:

git config user.email "your_correct@email.com"

and then, reset the author of the last commit:

git commit --amend --reset-author --no-edit

This command can also be used with git rebase -i on edited commits.

neovim as git difftool (mergetool)

I moved completely to neovim. It’s my default editor, to the point that I don’t bother installing new plugins for vim. And that’s how I realised that git difftool is still using vim.

I recently installed a new colorscheme using Plugged, and launching git difftool would give me errors, because I didn’t install it for vim. The solution turned out to be simple. Git’s mergetool has the option to configure path for the desired tool. So I’ve set up mergetool to use vimdiff (diftool will use the same tool by default).

git config --global merge.tool vimdiff
git config --global mergetool.vimdiff.path nvim

 

Handling files upload in django & python 3 without Forms

Sometimes you need to accept a file from a user, and you don’t want to use django Forms. The details come down to how many files you need to accept and do you know the names of the files.

If the file is from your app (or frontend) and you expect it you’ll know the file name, because you named it. But if it is a user selected file the name will be unknown.

Model

class FileModel(Model):
    file = FileField(upload_to='uploaded_files/' default='')
    created = DateTimeField(auto_now=True)

If the expected file is an image, change it to ImageField.

View

This paragraph shows the base code, for a single file and unknown filename.

class FileUploadView(View):

    def post(self, request):
        file_model = FileModel()
        _, file = request.FILES.popitem()  # get first element of the uploaded files

        file = file[0]  # get the file from MultiValueDict

        file_model.file = file
        file_model.save()

        return HttpResponse(content_type='text/plain', content='File uploaded')

In Python 3.x the request.FILES object is a dictionary that contains a fieldname as a key and a MultiValueDictionary as value. That’s because ex. in your HTML form you can upload multiple files from the same key. This way they will all be under the same key, and their names will be in the MultiValueDict. So the line:

file = file[0]

is only valid when we’re sure that only one file was sent.

Multiple files

In order to accept and save multiple files we need to add a loop to the main logic of our view. The loop will look like this, if the files were sent from the same form field (ex. <input type="file" name="files_to_upload"> ):

_, files = request.FILES.popitem()  # the files have been sent form the same form field (multiple files selected)

for f in file:
    filename = f.name

Alternatively you can use the key name to get the dictionary:

for f in request.FILES.getlist('files_to_upload'):
    filename = f.name

If there are multiple fields in the form, each uploading a file, you need to loop over the items of the request.FILES object.

for field_name, file in request.FILES.iteritems():
    filename = file[0].name

 

 

 

 

Ignore files in git without editing .gitignore

You work on a repository where the .gitignore file is defined globally and already tracked by git, but you have some files in your local directory that you don’t want to be tracked (logs, or build files, *.pyc etc.) and would like to ignore them (because who doesn’t like git status to show “working tree clean” or the status bar in zsh to be green).

Continue reading “Ignore files in git without editing .gitignore”