added profiles

This commit is contained in:
Sanj 2011-03-06 18:23:57 +05:30
parent 3981f14739
commit a700cd0ca0
5 changed files with 240 additions and 0 deletions

0
itf/profiles/__init__.py Normal file
View File

79
itf/profiles/models.py Normal file
View File

@ -0,0 +1,79 @@
from django.db import models
from datetime import datetime
from utils import generate_validate_token
class Person(models.Model):
#Basic Info
user = models.ForeignKey(User, blank=True, null=True, db_index=True)
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)
email = models.EmailField(blank=True, null=True, unique=True, db_index=True)
email_validated = models.BooleanField(default=False)
tel_no = models.CharField(max_length=100, blank=True)
about = models.TextField(blank=True, null=True)
#Occupation info
occupations = models.ManyToManyField("Occupation", through='PersonOccupation', blank=True, null=True)
is_practitioner = models.BooleanField(default=False)
is_enthusiast = models.BooleanField(default=True)
#Personal info
dob = models.DateField(null=True, verbose_name="Date of Birth")
gender = models.CharField(max_length=255, choices=GENDER_CHOICES, blank=True)
image = models.ImageField(upload_to='images/', blank=True, null=True)
locations = models.ManyToManyField("Location", blank=True, null=True)
#Groups and Connections
is_freelancer = models.BooleanField(default=False)
groups = models.ManyToManyField("TheatreGroup", blank=True, null=True, through='PersonGroup')
connections = models.ManyToManyField('Person', blank=True, null=True, through='PersonPerson')
productions = models.ManyToManyField("Production", blank=True, null=True, through='PersonProduction')
# trainings = models.ManyToManyField("Training", blank=True, null=True)
photos = models.ManyToManyField("PhotoAlbum", blank=True, null=True)
# orphans = models.ManyToManyField("Orphan", blank=True, null=True, through='PersonOrphan')
#Meta
joined = models.DateTimeField(auto_now_add=True)
last_modified = models.DateTimeField(auto_now=True)
last_accessed = models.DateTimeField(default=datetime.now)
#Tokens
reset_token = modes.CharField(max_length=256, null=True)
validate_token = models.CharField(max_length=256, null=True, default=generate_validate_token)
def __unicode__(self):
return self.first_name + " " + self.last_name
def is_orphan(self):
return self.user == None
class Occupation(models.Model):
name = models.CharField(max_length=255)
parent = models.ForeignKey('Occupation', blank=True, null=True)
def __unicode__(self):
return self.name
def is_child(self):
return self.parent != None
def is_parent(self):
if self.is_child():
return False
else:
if Occupation.objects.filter(parent=self).count() > 0: #self has children
return True
return False
class PersonOccupation(models.Model):
person = models.ForeignKey(Person, db_index=True)
occupation = models.ForeignKey(Occupation)
order = models.IntegerField()
# Create your models here.

23
itf/profiles/tests.py Normal file
View File

@ -0,0 +1,23 @@
"""
This file demonstrates two different styles of tests (one doctest and one
unittest). These will both pass when you run "manage.py test".
Replace these with more appropriate tests for your application.
"""
from django.test import TestCase
class SimpleTest(TestCase):
def test_basic_addition(self):
"""
Tests that 1 + 1 always equals 2.
"""
self.failUnlessEqual(1 + 1, 2)
__test__ = {"doctest": """
Another way to test that 1 + 1 is equal to 2.
>>> 1 + 1 == 2
True
"""}

10
itf/profiles/utils.py Normal file
View File

@ -0,0 +1,10 @@
import ox
from models import Profile
import random
def generate_validate_token():
while True:
token = ox.to32(random.randint(32768, 1048575))
if Profile.objects.filter(validate_token=token).count() == 0:
break
return token

128
itf/profiles/views.py Normal file
View File

@ -0,0 +1,128 @@
# Create your views here.
from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login, logout
from ox.django.shortcuts import render_to_json_response, json_response
from django import forms
import json
def register(request):
'''
param data {
username: 'username',
password: 'password',
email: 'emailaddress'
}
return {
status: {'code': int, 'text': string}
data: {
errors: {
username: 'Unknown Username',
password: 'Incorrect Password'
}
user: {
...
}
}
}
'''
data = json.loads(request.POST['data'])
form = RegisterForm(data, request.FILES)
if form.is_valid():
if models.User.objects.filter(username=form.data['username']).count() > 0:
response = json_response({
'errors': {
'username': 'Username already exists'
}
})
elif models.User.objects.filter(email=form.data['email']).count() > 0:
response = json_response({
'errors': {
'email': 'Email address already exits'
}
})
elif not form.data['password']:
response = json_response({
'errors': {
'password': 'Password can not be empty'
}
})
else:
first_user = models.User.objects.count() == 0
user = models.User(username=form.data['username'], email=form.data['email'])
user.set_password(form.data['password'])
#make first user admin
user.is_superuser = first_user
user.is_staff = first_user
user.save()
user = authenticate(username=form.data['username'],
password=form.data['password'])
login(request, user)
user_json = models.get_user_json(user)
response = json_response({
'user': user_json
}, text='account created')
else:
response = json_response(status=400, text='invalid data')
return render_to_json_response(response)
class LoginForm(forms.Form):
username = forms.TextInput()
password = forms.TextInput()
def api_login(request):
'''
param data {
username: 'username',
password: 'password'
}
return {
status: {'code': 200, 'text': 'ok'}
data: {
errors: {
username: 'Unknown Username',
password: 'Incorrect Password'
}
user: {
...
}
}
}
'''
data = json.loads(request.POST['data'])
form = LoginForm(data, request.FILES)
if form.is_valid():
if models.User.objects.filter(username=form.data['username']).count() == 0:
response = json_response({
'errors': {
'username': 'Unknown Username'
}
})
else:
user = authenticate(username=data['username'], password=data['password'])
if user is not None:
if user.is_active:
login(request, user)
user_json = models.get_user_json(user)
response = json_response({
'user': user_json
})
else:
response = json_response({
'errors': {
'username': 'User Disabled'
}
})
else:
response = json_response({
'errors': {
'password': 'Incorrect Password'
}
})
else:
response = json_response(status=400, text='invalid data')
return render_to_json_response(response)
actions.register(api_login, 'login')