Commit 803ab53b authored by Robert's avatar Robert

Add colored default avatars for gestalten #81

parent e3db37d7
# -*- coding: utf-8 -*-
# Generated by Django 1.9.7 on 2016-07-15 08:41
from __future__ import unicode_literals
from django.db import migrations, models
import entities.models
def set_default_colors(apps, schema_editor):
Gestalt = apps.get_model('entities', 'Gestalt')
for gestalt in Gestalt.objects.all():
gestalt.avatar_color = entities.models.get_random_color()
gestalt.save()
class Migration(migrations.Migration):
dependencies = [
('entities', '0036_auto_20160705_0852'),
]
operations = [
migrations.AddField(
model_name='gestalt',
name='avatar_color',
field=models.CharField(default=entities.models.get_random_color, max_length=7),
),
migrations.RunPython(set_default_colors),
]
......@@ -15,6 +15,7 @@ class Gestalt(models.Model):
about = models.TextField('Selbstauskunft', blank=True)
addressed_content = models.ManyToManyField('content.Content', related_name='gestalten', through='GestaltContent')
avatar = models.ImageField(blank=True)
avatar_color = models.CharField(max_length=7, default=get_random_color)
background = models.ImageField('Hintergrundbild', blank=True)
public = models.BooleanField(
'Benutzerseite veröffentlichen',
......@@ -30,6 +31,16 @@ class Gestalt(models.Model):
def get_absolute_url(self):
return urlresolvers.reverse('gestalt', args=[type(self).objects.get(pk=self.pk).user.username])
# FIXME: move to template filter
def get_initials(self):
import re
initials = ''
for w in str(self).split():
m = re.search('[a-zA-Z0-9]', w)
initials += m.group(0) if m else ''
return initials
class GestaltContent(models.Model):
content = models.OneToOneField('content.Content')
gestalt = models.ForeignKey('Gestalt')
......
{% load staticfiles thumbnail %}
{% load thumbnail %}
<{% if link %}a href="{{ gestalt.get_absolute_url }}"{% else %}div{% endif %} class="avatar">
{% thumbnail gestalt.avatar '100x100' format='PNG' as avatar %}
<img src="{{ avatar.url }}" title="{{ gestalt }}" alt="{{ gestalt }}" class="avatar">
{% empty %}
<img src="{% static 'avatar.png' %}" title="{{ gestalt }}" alt="{{ gestalt }}" class="avatar">
<span title="{{ group.name }}" class="avatar avatar-initials" data-initials="{{ gestalt.get_initials|length }}" style="background-color: {{ gestalt.avatar_color }}">
{{ gestalt.get_initials|truncatechars:4 }}
</span>
{% endthumbnail %}
</{% if link %}a{% else %}div{% endif %}>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment