Hey guys I'm building a Python/Django linked-in app to reinforce my knowledge. Literally have only learned it for a week so don't be too hard on me. The idea of the app is so that when a user logs in, they can see their profile, and go to a users page to see everyone with whom they're not connected. Upon hitting "connect", the user sends a connection request to the member, and a request should appear in that users profile upon login. When a user accepts that request, it should appear in both users connections, and if a user hits ignore, it should go back to all users table. I've added my code, i guess I'm just not seeing the logic behind it.
models.py
from django.db import models
class User(models.Model):
name = models.TextField(blank=False, max_length=55)
email = models.TextField(blank=False, max_length=55)
password = models.TextField(blank=False, max_length=55)
description = models.TextField(blank=False, max_length=255)
class Connection(models.Model):
user = models.ForeignKey(User, related_name="user")
status = models.IntegerField(default=0)
friend = models.ForeignKey(User, related_name="friend")
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Login/Registration</title>
<link rel="stylesheet" href="http://ift.tt/1K1B2rp">
</head>
<body>
<div class="container">
<h3>Register</h3>
<form action="/register" method="post">
{%csrf_token %}
Name: <input type="name" name="name" required>
<br>
Email : <input type="email" name="email" required>
<br>
Password : <input type="password" name="password" required>
<br>
Description: <textarea name="description" required></textarea>
<br>
<br>
<input type="submit" value="Register">
</form>
<h3>Login</h3>
<form action="/login" method="post">
{%csrf_token %}
Email: <input type="email" name="email" required>
<br>
Password: <input type="password" name="password" required>
<br>
<input type="submit" value="Login">
</form>
</div>
</body>
</html>
profile.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>User Profile</title>
<link rel="stylesheet" href="http://ift.tt/1K1B2rp">
</head>
<body>
<div class="container">
<a href="/logout">Logout</a>
<a href="/users"> All Users</a>
<hr>
<br>
<h3>Welcome, {{user.name}} </h3>
<br>
Your email: {{user.email}}
<hr>
<div class="box">
<br>
Here is your profile description:
<br>
{{user.description}}
</div>
<hr>
<h3>Your Professional Network</h3>
<div class="box">
</div>
<hr>
<h3>Invitations</h3>
<h5>The following people asked you to be in their network</h5>
{% for invitation in invitations %}
<td>{{invitation}}</td>
{% endfor %}
</div>
</body>
</html>
show.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<link rel="stylesheet" href="http://ift.tt/1K1B2rp">
</head>
<body>
<div class="container">
<a href="/logout">Logout</a>
<span>|</span>
<a href="/user_home">My Profile</a>
<h3>User # {{user.id}}</h3>
<p>Name: {{user.name}}</p>
<p>Description: {{user.description}}</p>
</div>
</body>
</html>
users.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Users</title>
<link rel="stylesheet" href="http://ift.tt/1K1B2rp">
</head>
<body>
<div class="container">
<a href="/user_home">My Profile </a>
<span>|</span>
<a href="/logout">Logout</a>
<h3>All Users</h3>
<h5>Users Your May Want to Connect with</h5>
<table class="table class table-bordered table-striped table-hovered">
<thead>
<tr>
<th>Name</th>
<th>Action</th>
</tr>
</thead>
<tbody>
{% for user in users %}
<tr>
<td><a href="/users/{{user.id}}/show">{{user.name}}</a></td>
<td><a href="/users/{{user.id}}/connect">Connect</a></td>
</tr>
{%endfor %}
</tbody>
</table>
</div>
</body>
</html>
views.py
from django.http import HttpResponse, Http404
from django.shortcuts import render, redirect
from django.core.exceptions import ValidationError
from apps.network.models import User, Connection
from django.contrib import messages
from datetime import datetime
def index(request):
print request.GET
print request.method
return render(request, 'network/index.html')
def register(request):
print "In registration"
user = User.objects.filter(email= request.POST.get('email'), password=request.POST.get('password'))
if len(user) > 0 and len(request.POST.get('email'))<3:
return redirect('/')
else:
user = User()
user.name = request.POST.get('name')
user.email = request.POST.get('email')
user.password = request.POST.get('password')
user.description = request.POST.get('description')
user.save()
print user.name
print user.email
print user.password
print "Successful registration"
return redirect('/')
def login(request):
print "In login"
print request.POST.get('email')
print request.POST.get('password')
user = User.objects.filter(email=request.POST.get('email'), password=request.POST.get('password'))
if len(user)<1:
print "Failed"
return redirect('/')
else:
print "Success"
request.session['user_id'] = user[0].id
print "Logging in"
return redirect('/user_home')
def user_home(request):
print "User Home"
if "user_id" in request.session:
user = User.objects.get(id=request.session['user_id'])
invitation = Connection.objects.get(user=user, status=1).exclude(status=1)
invitations = invitation.friend.name
print user.name
print user.email
print user.description
content = {
'user':user,
'invitations':invitations,
}
return render(request, 'network/profile.html', content)
else:
del request.session
return redirect('/')
def users(request):
users = User.objects.exclude(id=request.session['user_id'])
content = {
'users': users
}
return render(request, 'network/users.html', content)
def show(request, user_id):
user = User.objects.get(id=user_id)
content = {
'user': user
}
return render(request, 'network/show.html', content)
def connect(request, user_id):
print "Connecting"
friend = User.objects.get(id=user_id)
user = User.objects.get(id=request.session['user_id'])
connection = Connection.objects.create(friend=friend, user=user, status=1)
friend.status = 0
friend.save()
user.status = 1
user.save()
return redirect('/user_home')
def logout(request):
print "Logging out"
del request.session['user_id']
return redirect('/')
urls.py
from django.conf.urls import patterns, url
from apps.network import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^register$', views.register, name='register'),
url(r'^login$', views.login, name='login'),
url(r'^user_home$', views.user_home, name='user_home'),
url(r'^logout$', views.logout, name='logout'),
url(r'^users$', views.users, name='users'),
url(r'^users/(?P<user_id>\d+)/show$', views.show),
url(r'^users/(?P<user_id>\d+)/connect$', views.connect),
)
show.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<link rel="stylesheet" href="http://ift.tt/1K1B2rp">
</head>
<body>
<div class="container">
<a href="/logout">Logout</a>
<span>|</span>
<a href="/user_home">My Profile</a>
<h3>User # {{user.id}}</h3>
<p>Name: {{user.name}}</p>
<p>Description: {{user.description}}</p>
</div>
</body>
</html>
Aucun commentaire:
Enregistrer un commentaire