Makes me want to be good at hacking :/ https://t.co/egLx8ibQWW
— Louis D. (@LouWii59) 20 décembre 2016
Category: Dev
IT Jobs Explained With A Broken Lightbulb
IT Jobs Explained With A Broken Lightbulb https://t.co/xI4jUM4lcp
— Louis D. (@LouWii59) 19 décembre 2016
MySQL Docker Containers: Understanding the basics
#MySQL #Docker Containers: Understanding the basics https://t.co/EMzvxcxTIa https://t.co/EMzvxcxTIa #web #dev
— Louis D. (@LouWii59) 17 décembre 2016
DBeaver : free multi os database manager
DBeaver : free multi os database manager https://t.co/EogsOvWMsc #MySQL #PostgreSQL #SQLite #MongoDB #dev #web
— Louis D. (@LouWii59) 17 décembre 2016
scrollMonitor: A Simple and Fast API to Monitor Elements As You Scroll
scrollMonitor: A Simple and Fast API to Monitor Elements As You Scroll – https://t.co/jGnHSKOXPr
— FrontEnd Daily (@FrontEndDaily) 16 décembre 2016
Top JavaScript Frameworks & Topics to Learn in 2017
Top JavaScript Frameworks & Topics to Learn in 2017 https://t.co/sMFLGbqmq3 #js #dev #web
— Louis D. (@LouWii59) 16 décembre 2016
Animating Angular app: ngView
Animating #Angular app: ngView https://t.co/aLk0jHY5An #web #dev #JS
— Louis D. (@LouWii59) 3 septembre 2016
Extend Django User Model
Django CMS has its own User model but you might need to add attributes and other properties to it (like an avatar, right ?). The easiest way to do that is to extend that model with another model (we’ll call it UserProfile) and link them together using a one-to-one relationship.
Creating the UserProfile model
Nothing special here, I’m sure you know how it works.
1 2 3 4 5 6 7 |
class UserProfile(models.Model): user = models.OneToOneField(settings.AUTH_USER_MODEL, related_name='user_profile', primary_key=True) avatar = models.ImageField(upload_to='uploads/avatars/%Y/%m/%d/') website = models.URLField(default='', blank=True) def __str__(self): return 'Profile of user: {}'.format(self.user.username) |
Creating a UserProfile when creating a User
Each User needs to have a UserProfile object. To make sure that applies to every user, we’ll create a UserProfile each time a User is created. To do that. we’ll use the post_save signal (signals doc).
Just under the UserProfile code, you can add that code
1 2 3 4 5 6 7 8 9 |
def create_profile(sender, **kwargs): """ Listen to post_save signal to create a UserProfile whenever a User is created """ user = kwargs["instance"] if kwargs["created"]: user_profile = UserProfile(user=user) user_profile.save() post_save.connect(create_profile, sender=User) |
And you’re pretty much good to go. You can access the user’s UserProfile using user.user_profile.
Find Application Support Directory in Swift (OSX, iOS)
It’s quite easy actually, once you know the answer…
1 2 3 |
let paths = NSSearchPathForDirectoriesInDomains(.ApplicationSupportDirectory, .UserDomainMask, true) let appSupportDir = paths[0] |
Django REST Framework : add an attribute on the fly in a Serializer
Seralizers from DRF are doing a very good job at converting any Model to JSON data. They are even more awesome when you know how to add custom attributes to them.
In my case, I needed to add an attribute based on the current logged in user and related data from my model. To do that, I used a SerializerMethodField.
From the doc :
This is a read-only field. It gets its value by calling a method on the serializer class it is attached to. It can be used to add any sort of data to the serialized representation of your object.
And the example :
1 2 3 4 5 6 7 8 9 10 11 12 |
from django.contrib.auth.models import User from django.utils.timezone import now from rest_framework import serializers class UserSerializer(serializers.ModelSerializer): days_since_joined = serializers.SerializerMethodField() class Meta: model = User def get_days_since_joined(self, obj): return (now() - obj.date_joined).days |
Here’s a preview of how I implemented it :
1 2 3 4 5 |
class PostSerializer(serializers.ModelSerializer): user_is_liking = serializers.SerializerMethodField() def get_user_is_liking(self, obj): return obj.likers.filter(id=self._context.get('request').user.id).exists() |
Basically, this sets a field to True or False if the current user is ‘liking’ the Post object.
This is a pretty neat feature that I don’t want to forget about, so that’s why I’m writing it down here.