Skip to content

Commit

Permalink
permite usuario subir uma foto como avatar
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiomontefuscolo committed May 27, 2015
1 parent 2239b13 commit b91b874
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 4 deletions.
14 changes: 14 additions & 0 deletions assets/scripts/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,20 @@
$this.prop('tabindex', i + 1);
});

$('[data-file-preview]').each(function(){
var $image = $(this);
var $target = $($image.attr('data-file-preview'));

$target.change(function(evt){
if (this.files && this.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$image.attr('src', e.target.result);
};
reader.readAsDataURL(this.files[0]);
}
});
});
});
$(document).on('keydown.radio.data-api', '[data-toggle^=radio], .radio', function (e) {
if( e.type === 'keydown' && e.keyCode === 32 ){
Expand Down
7 changes: 7 additions & 0 deletions assets/styles/base/common.less
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ body {
text-transform: uppercase;
}

label.btn,
.forms label.btn {
font-weight: 600;
font-size: 16px;
line-height: 1.6em;
}

.close {
opacity: 0.9;
}
Expand Down
19 changes: 19 additions & 0 deletions cyclists/migrations/0002_user_avatar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


class Migration(migrations.Migration):

dependencies = [
('cyclists', '0001_initial'),
]

operations = [
migrations.AddField(
model_name='user',
name='avatar',
field=models.ImageField(upload_to=b'avatars', verbose_name='Avatar', blank=True),
),
]
32 changes: 32 additions & 0 deletions cyclists/models.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# -*- coding: utf-8 -*-
import os
from datetime import date
from django.conf import settings
from django.contrib.auth.models import AbstractUser
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.core.files.storage import FileSystemStorage

GENDER = (
('male', _('Male')),
Expand Down Expand Up @@ -55,11 +58,37 @@
)


class AvatarStorage(FileSystemStorage):
def get_available_name(self, name):
if self.exists(name):
os.remove(name)
return name

def url(self, name):
basename = os.path.basename(name)
subfolder = getattr(type(self), 'subfolder', '')
relative = os.path.join(subfolder, basename)
return super(AvatarStorage, self).url(relative)

@classmethod
def set_folder(cls, subfolder=''):
cls.subfolder = subfolder
base_folder = os.path.join(settings.MEDIA_ROOT, subfolder)

def _path_gen(user, filename):
extension = filename.rsplit('.', 1)[-1]
new_name = '%s.%s' % (user.username, extension)
return os.path.join(base_folder, new_name)
return _path_gen


class User(AbstractUser):
class Meta:
verbose_name = _('User')
verbose_name_plural = _('Users')

avatar = models.ImageField(_('Avatar'), upload_to=AvatarStorage.set_folder('avatars'),
storage=AvatarStorage(), blank=True)
country = models.CharField(_('Country'), max_length=32, blank=True)
city = models.CharField(_('City'), max_length=32, blank=True)
gender = models.CharField(_('Gender'), max_length=24, blank=True)
Expand All @@ -72,6 +101,9 @@ class Meta:
accepted_agreement = models.BooleanField(_('Accepted agreement'), default=False)

def get_avatar_url(self):
if self.avatar:
return self.avatar.url

social = self.socialaccount_set.first()
if social:
return social.get_avatar_url()
Expand Down
4 changes: 2 additions & 2 deletions front/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ class Meta:

class BikeanjoUserInforForm(forms.ModelForm):
class Meta:
fields = ('first_name', 'last_name', 'email', 'country', 'city', 'gender', 'birthday',)
fields = ('avatar', 'first_name', 'last_name', 'email', 'country', 'city', 'gender', 'birthday',)
model = models.User


Expand All @@ -226,7 +226,7 @@ class RequesterUserInforForm(forms.ModelForm):
bike_use = forms.ChoiceField(label=_('Bike user'), choices=models.BIKE_USE)

class Meta:
fields = ('first_name', 'last_name', 'email', 'country', 'city', 'gender', 'birthday',
fields = ('avatar', 'first_name', 'last_name', 'email', 'country', 'city', 'gender', 'birthday',
'ride_experience', 'bike_use', 'initiatives',)
model = models.User

Expand Down
21 changes: 20 additions & 1 deletion templates/bikeanjo_dashboard_userinfo.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<section class="dashboard-content col-md-7">

<form class="card list" method="POST">{% csrf_token %}
<form class="card list" method="POST" enctype="multipart/form-data">{% csrf_token %}
<header class="card-header negative">
<h2 class="card-title"><a href="{% url 'user_register' %}"><i class="fa fa-arrow-left"></i><span class="sr-only">Voltar</span></a> Cadastro pessoal</h2>
</header>
Expand All @@ -26,6 +26,25 @@ <h2 class="card-title"><a href="{% url 'user_register' %}"><i class="fa fa-arrow

<div class="forms">
{% csrf_token %}
<div class="form-group">
<label class="primary-label">Avatar</label>
<span class="user-icon">
<img src="{{ user.get_avatar_url }}"
data-file-preview="#{{ form.avatar.auto_id }}"
class="img-circle"
width="85" height="85"
title="{{ user.get_full_name }}"
alt="{% trans "Photo from" %} {{ user.get_full_name }}"/>
</span>

<label for="{{ form.avatar.auto_id }}"
class="col-sm-offset-2 btn btn-success">Adicionar</label>

<div class="hidden">
<input id="{{ form.avatar.auto_id }}" name="{{ form.avatar.html_name }}" type="file"/>
</div>
</div>

<div class="form-group {% if form.first_name.errors %}has-error has-feedback{% endif %}">
<label class="primary-label" for="{{ form.first_name.id_for_label }}">{{ form.first_name.label }}</label>
<input class="form-control" type="text" name="{{ form.first_name.html_name }}" id="{{ form.first_name.id_for_label }}" value="{{ form.first_name.value|default:"" }}" placeholder="Seu nome completo">
Expand Down
22 changes: 21 additions & 1 deletion templates/requester_dashboard_userinfo.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<section class="dashboard-content col-md-7">

<form class="card list" method="POST">{% csrf_token %}
<form class="card list" method="POST" enctype="multipart/form-data">{% csrf_token %}
<header class="card-header negative">
<h2 class="card-title"><a href="{% url 'user_register' %}"><i class="fa fa-arrow-left"></i><span class="sr-only">Voltar</span></a> Cadastro pessoal</h2>
</header>
Expand All @@ -26,6 +26,26 @@ <h2 class="card-title"><a href="{% url 'user_register' %}"><i class="fa fa-arrow

<div class="forms">
{% csrf_token %}

<div class="form-group">
<label class="primary-label">Avatar</label>
<span class="user-icon">
<img src="{{ user.get_avatar_url }}"
data-file-preview="#{{ form.avatar.auto_id }}"
class="img-circle"
width="85" height="85"
title="{{ user.get_full_name }}"
alt="{% trans "Photo from" %} {{ user.get_full_name }}"/>
</span>

<label for="{{ form.avatar.auto_id }}"
class="col-sm-offset-2 btn btn-success">Adicionar</label>

<div class="hidden">
<input id="{{ form.avatar.auto_id }}" name="{{ form.avatar.html_name }}" type="file"/>
</div>
</div>

<div class="form-group {% if form.first_name.errors %}has-error has-feedback{% endif %}">
<label class="primary-label" for="{{ form.first_name.id_for_label }}">{{ form.first_name.label }}</label>
<input class="form-control" type="text" name="{{ form.first_name.html_name }}" id="{{ form.first_name.id_for_label }}" value="{{ form.first_name.value|default:"" }}" placeholder="Seu nome completo">
Expand Down

0 comments on commit b91b874

Please sign in to comment.