Skip to content

Commit d92a645

Browse files
authored
Merge pull request #469 from RishabhJain2018/master
Add profile for users
2 parents faf9607 + 7685efc commit d92a645

File tree

11 files changed

+202
-5
lines changed

11 files changed

+202
-5
lines changed

junction/profiles/admin.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from django.contrib import admin
2+
from .models import Profile
3+
4+
5+
class ProfileAdmin(admin.ModelAdmin):
6+
list_display = ('user', 'city', 'contact_no')
7+
search_fields = ('contact_no', 'city')
8+
9+
admin.site.register(Profile, ProfileAdmin)

junction/profiles/forms.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from django import forms
2+
from .models import Profile
3+
4+
5+
class ProfileForm(forms.ModelForm):
6+
class Meta:
7+
model = Profile
8+
fields = ['city', 'contact_no', ]
9+
exclude = ['user', ]
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# -*- coding: utf-8 -*-
2+
from __future__ import unicode_literals
3+
4+
from django.db import models, migrations
5+
from django.conf import settings
6+
7+
8+
class Migration(migrations.Migration):
9+
10+
dependencies = [
11+
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
12+
]
13+
14+
operations = [
15+
migrations.CreateModel(
16+
name='Profile',
17+
fields=[
18+
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
19+
('created_at', models.DateTimeField(auto_now_add=True, verbose_name='Created At')),
20+
('modified_at', models.DateTimeField(auto_now=True, verbose_name='Last Modified At')),
21+
('city', models.CharField(max_length=100, null=True, blank=True)),
22+
('contact_no', models.CharField(max_length=15, null=True, blank=True)),
23+
('created_by', models.ForeignKey(related_name='created_profile_set', verbose_name='Created By', blank=True, to=settings.AUTH_USER_MODEL, null=True)),
24+
('modified_by', models.ForeignKey(related_name='updated_profile_set', verbose_name='Modified By', blank=True, to=settings.AUTH_USER_MODEL, null=True)),
25+
('user', models.OneToOneField(to=settings.AUTH_USER_MODEL)),
26+
],
27+
options={
28+
'abstract': False,
29+
},
30+
bases=(models.Model,),
31+
),
32+
]

junction/profiles/models.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1-
# from django.db import models
1+
from django.contrib.auth.models import User
2+
from junction.base.models import AuditModel
3+
from django.db import models
24

3-
# Create your models here.
5+
6+
class Profile(AuditModel):
7+
'''
8+
It stores the City/Phone Details of the User.
9+
'''
10+
user = models.OneToOneField(User)
11+
city = models.CharField(max_length=100, blank=True, null=True)
12+
contact_no = models.CharField(max_length=15, blank=True, null=True)
13+
14+
def __unicode__(self):
15+
return self.user.username

junction/profiles/tests.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1-
# from django.test import TestCase
1+
from django.test import TestCase
2+
from .models import Profile
3+
from django.contrib.auth.models import User
24

3-
# Create your tests here.
5+
# models test
6+
7+
8+
class ProfileTest(TestCase):
9+
10+
def setUp(self):
11+
self.user = User.objects.create(username='user1', password='123456')
12+
Profile.objects.create(city='noida', contact_no='1234567890')
13+
14+
def test_create_profile(self):
15+
user = User.objects.get(username='user1')
16+
profile_details = Profile.objects.get(user=user)
17+
self.assertEqual(profile_details.city, 'noida')

junction/profiles/urls.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@
55

66
urlpatterns = [
77
url(r'^$', views.dashboard, name='dashboard'),
8+
url(r'^edit/$', views.profile, name='profile'),
9+
10+
811
]

junction/profiles/views.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,17 @@
55
from django.contrib.auth.decorators import login_required
66
from django.shortcuts import render
77
from django.views.decorators.http import require_http_methods
8+
from django.http import HttpResponseRedirect
9+
from django.core.urlresolvers import reverse
10+
from django.contrib.auth.models import User
811

912
# Junction Stuff
1013
from junction.conferences.models import Conference
1114

15+
# Profile Stuff
16+
from .models import Profile
17+
from .forms import ProfileForm
18+
1219

1320
@login_required
1421
@require_http_methods(['GET'])
@@ -22,3 +29,36 @@ def dashboard(request):
2229
conf_proposals[conf.name] = [proposal]
2330
return render(request, 'profiles/dashboard.html',
2431
{'conf_proposals': conf_proposals})
32+
33+
34+
@login_required
35+
def profile(request):
36+
username = request.user
37+
detail = None
38+
39+
if request.method == "POST" and username == request.user:
40+
user = User.objects.get(pk=username.id)
41+
detail = Profile.objects.filter(user=user).exists()
42+
if detail:
43+
detail = Profile.objects.get(user=user)
44+
detail_form = ProfileForm(request.POST, instance=detail)
45+
if detail_form.is_valid():
46+
detail = detail_form.save()
47+
return HttpResponseRedirect(reverse('profiles:dashboard'))
48+
else:
49+
user = User.objects.get(pk=username.id)
50+
detail_form = ProfileForm(request.POST)
51+
if detail_form.is_valid():
52+
detail_form = detail_form.save(commit=False)
53+
detail_form.user = user
54+
detail_form.save()
55+
return HttpResponseRedirect(reverse('profiles:dashboard'))
56+
57+
elif request.method == "GET":
58+
user = User.objects.get(pk=username.id)
59+
detail = Profile.objects.filter(user=user).exists()
60+
if detail:
61+
detail = Profile.objects.get(user=user)
62+
return render(request, 'profiles/userprofile.html', {'detail': detail})
63+
else:
64+
return render(request, 'profiles/userprofile.html')
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# -*- coding: utf-8 -*-
2+
from __future__ import unicode_literals
3+
4+
from django.db import models, migrations
5+
6+
7+
class Migration(migrations.Migration):
8+
9+
dependencies = [
10+
('proposals', '0020_auto_20160806_2023'),
11+
]
12+
13+
operations = [
14+
migrations.AlterModelOptions(
15+
name='proposalcomment',
16+
options={'ordering': ('created_at',)},
17+
),
18+
]

junction/templates/base.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
<ul class="dropdown-menu" role="menu">
110110
<li><a href="{% url 'profiles:dashboard' %}">Dashboard</a></li>
111111
<li><a href="{% url 'socialaccount_connections' %}">Social Accounts</a></li>
112+
<li><a href="{% url 'profiles:profile' %}">Profile</a></li>
112113
{% if user.is_staff %}
113114
<li><a href="{% url 'admin:index' %}" target="_blank">Admin</a></li>
114115
{% endif %}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
{% extends 'base.html' %}
2+
3+
{% load django_markdown %}
4+
{% load static from staticfiles %}
5+
{% load django_bootstrap_breadcrumbs %}
6+
{% load proposal_filters %}
7+
8+
{% block head_title %} Profile {% endblock %}
9+
{% block og_title %} Profile {% endblock %}
10+
{% block og_description %} Profile {% endblock %}
11+
{% block page_description %} Profile {% endblock %}
12+
13+
{% block endhead %}
14+
<!-- Custom CSS -->
15+
<link href="{% static 'css/list.css' %}" rel="stylesheet">
16+
{% endblock %}
17+
18+
{% block breadcrumbs %}
19+
{{ block.super }}
20+
{% breadcrumb "Dashboard" "user-proposals-list" %}
21+
{% breadcrumb "Profile" "user-profile"%}
22+
{% endblock %}
23+
24+
{% block navbar_logo %}
25+
<a href="#" class="navbar-brand">Junction</a>
26+
{% endblock navbar_logo %}
27+
28+
{% block content %}
29+
<div class="row">
30+
<div class="col-md-8 col-md-offset-2 col-lg-6 col-lg-offset-3">
31+
<div class="form-container space-2-bottom {% if form.errors %} has-error {% endif %}">
32+
<h2 class="text-center">Edit Profile</h2>
33+
<div class="pad-2 push-half-top push-2-bottom">
34+
<form class="form-horizontal login-form" method="POST">
35+
{% csrf_token %}
36+
<div class="form-group">
37+
<label for="city" class="col-lg-4 control-label"> City :</label>
38+
<div class="col-lg-8">
39+
<input autofocus="autofocus" name="city" placeholder="Enter Your City" type="text" value="{{detail.city}}">
40+
</div>
41+
</div>
42+
<div class="form-group">
43+
<label for="contact" class="col-lg-4 control-label"> Contact :</label>
44+
<div class="col-lg-8">
45+
<input autofocus="autofocus" name="contact_no" placeholder="Enter Your Contact No:" type="text" pattern="\d{10}" value="{{detail.contact_no}}" >
46+
</div>
47+
</div>
48+
<div class="form-group">
49+
<div class="col-lg-8 col-lg-offset-4">
50+
<button class="btn btn-primary" type="submit">
51+
Submit
52+
</button>
53+
</div>
54+
</div>
55+
</form>
56+
</div>
57+
</div>
58+
</div>
59+
</div>
60+
{% endblock %}

0 commit comments

Comments
 (0)