Commit fda8332eee628a841f7b58157153219124ac9a5d

Authored by Zambom
1 parent 97e4e5c8

Fixing users no image display and updating admin's user form

app/templates/home.html
... ... @@ -64,7 +64,7 @@
64 64 {% block sidebar %}
65 65 <div class="panel panel-primary">
66 66 <div class="panel-heading">
67   - <img src="{{ user.image.url }}" id="img" class="img-rounded">
  67 + <img src="{{ user.image_url }}" id="img" class="img-rounded">
68 68 <p></p>
69 69 <div class="row">
70 70 <div class="col-xs-3 col-md-3">
... ... @@ -92,7 +92,7 @@
92 92 {% if user|has_role:'system_admin' %}
93 93 <li> <a href="{% url 'users:manage' %}">{% trans 'Manage Users' %}</a></li>
94 94 {% endif %}
95   - {% if user|has_role:'system_admin, professor' %}
  95 + {% if user|has_role:'system_admin' or user|has_role:'professor' %}
96 96 <li>
97 97 <a href="#courses_list" data-toggle="collapse">{% trans 'Manage Courses' %}</a>
98 98  
... ...
app/templates/home_teacher_student_content.html
1   -{% load i18n %}
  1 +{% load static i18n %}
2 2  
3 3 {% for notification in objects %}
4 4 <li {% if not notification.read %}class="not_read"{% endif %}>
5 5 <div class="avatar">
6   - <img src="{{ notification.user.image.url }}">
  6 + <img src="{{ notification.user.image_url }}">
7 7 </div>
8 8 <div class="bubble-container">
9 9 <div class="bubble">
... ...
core/static/img/no_image.jpg 0 → 100644

2.99 KB

users/admin.py
1 1 from django.contrib import admin
2 2 from .models import User
3   -from .forms import ProfileForm
  3 +from .forms import UserForm
4 4  
5 5 class UserAdmin(admin.ModelAdmin):
6 6 list_display = ['username', 'name', 'email', 'is_staff', 'is_active']
7 7 search_fields = ['username', 'name', 'email']
8   - form = ProfileForm
  8 + form = UserForm
9 9  
10 10 admin.site.register(User, UserAdmin)
11 11 \ No newline at end of file
... ...
users/forms.py
1 1 # coding=utf-8
2   -
  2 +import os
  3 +from django.conf import settings
3 4 from django import forms
4 5 from django.utils.translation import ugettext_lazy as _
  6 +from rolepermissions.shortcuts import assign_role
5 7 from .models import User
6 8  
7 9  
... ... @@ -25,10 +27,32 @@ class ProfileForm(forms.ModelForm):
25 27 }
26 28  
27 29 class UserForm(forms.ModelForm):
  30 + def save(self, commit=True):
  31 + super(UserForm, self).save(commit=False)
  32 +
  33 + #if not self.instance.image:
  34 + # self.instance.image = os.path.join(os.path.dirname(settings.BASE_DIR), 'uploads', 'no_image.jpg')
  35 +
  36 + self.instance.set_password(self.cleaned_data['password'])
  37 + self.instance.save()
  38 +
  39 + if self.instance.is_staff:
  40 + assign_role(self.instance, 'system_admin')
  41 + elif self.instance.type_profile == 2:
  42 + assign_role(self.instance, 'student')
  43 + elif self.instance.type_profile == 1:
  44 + assign_role(self.instance, 'professor')
  45 +
  46 + self.instance.save()
  47 +
  48 + return self.instance
28 49  
29 50 class Meta:
30 51 model = User
31   - fields = ['username', 'name', 'email', 'birth_date', 'city', 'state', 'gender', 'type_profile', 'cpf', 'phone', 'image', 'is_staff', 'is_active']
  52 + fields = ['username', 'name', 'email', 'password', 'birth_date', 'city', 'state', 'gender', 'type_profile', 'cpf', 'phone', 'image', 'is_staff', 'is_active']
  53 + widgets = {
  54 + 'password':forms.PasswordInput
  55 + }
32 56  
33 57 class EditUserForm(forms.ModelForm):
34 58  
... ...
users/migrations/0013_auto_20160915_2334.py 0 → 100644
... ... @@ -0,0 +1,20 @@
  1 +# -*- coding: utf-8 -*-
  2 +# Generated by Django 1.10 on 2016-09-16 02:34
  3 +from __future__ import unicode_literals
  4 +
  5 +from django.db import migrations, models
  6 +
  7 +
  8 +class Migration(migrations.Migration):
  9 +
  10 + dependencies = [
  11 + ('users', '0012_auto_20160908_1625'),
  12 + ]
  13 +
  14 + operations = [
  15 + migrations.AlterField(
  16 + model_name='user',
  17 + name='image',
  18 + field=models.ImageField(blank=True, upload_to='users/', verbose_name='Image'),
  19 + ),
  20 + ]
... ...
users/models.py
... ... @@ -4,6 +4,7 @@ from django.db import models
4 4 from django.core import validators
5 5 from django.utils.translation import ugettext_lazy as _
6 6 from django.contrib.auth.models import AbstractBaseUser, UserManager, PermissionsMixin
  7 +from django.contrib.staticfiles.templatetags.staticfiles import static
7 8  
8 9 class User(AbstractBaseUser, PermissionsMixin):
9 10  
... ... @@ -19,7 +20,7 @@ class User(AbstractBaseUser, PermissionsMixin):
19 20 city = models.CharField(_('City'), max_length = 90, blank = True)
20 21 state = models.CharField(_('State'), max_length = 30, blank = True)
21 22 gender = models.CharField(_('Gender'), max_length = 1, choices = (('M', _('Male')), ('F', _('Female'))))
22   - image = models.ImageField(verbose_name = _('Image'), blank = True, upload_to = 'users/', default = 'no_image.jpg')
  23 + image = models.ImageField(verbose_name = _('Image'), blank = True, upload_to = 'users/')
23 24 birth_date = models.DateField(_('Birth Date'), null = True, blank = True)
24 25 phone = models.CharField(_('Phone'), max_length = 30, blank = True)
25 26 cpf = models.CharField(_('Cpf'), max_length = 15, blank = True)
... ... @@ -40,8 +41,15 @@ class User(AbstractBaseUser, PermissionsMixin):
40 41 def __str__(self):
41 42 return self.name or self.username
42 43  
43   - def det_full_name(self):
  44 + def get_full_name(self):
44 45 return str(self)
45 46  
46 47 def get_short_name(self):
47   - return str(self).split(" ")[0]
48 48 \ No newline at end of file
  49 + return str(self).split(" ")[0]
  50 +
  51 + @property
  52 + def image_url(self):
  53 + if self.image and hasattr(self.image, 'url'):
  54 + return self.image.url
  55 + else:
  56 + return static('img/no_image.jpg')
49 57 \ No newline at end of file
... ...