From a700cd0ca0753f12bfda57f8bc6a9727b8213e69 Mon Sep 17 00:00:00 2001 From: Sanj Date: Sun, 6 Mar 2011 18:23:57 +0530 Subject: [PATCH] added profiles --- itf/profiles/__init__.py | 0 itf/profiles/models.py | 79 ++++++++++++++++++++++++ itf/profiles/tests.py | 23 +++++++ itf/profiles/utils.py | 10 +++ itf/profiles/views.py | 128 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 240 insertions(+) create mode 100644 itf/profiles/__init__.py create mode 100644 itf/profiles/models.py create mode 100644 itf/profiles/tests.py create mode 100644 itf/profiles/utils.py create mode 100644 itf/profiles/views.py diff --git a/itf/profiles/__init__.py b/itf/profiles/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/itf/profiles/models.py b/itf/profiles/models.py new file mode 100644 index 0000000..1ec2476 --- /dev/null +++ b/itf/profiles/models.py @@ -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. diff --git a/itf/profiles/tests.py b/itf/profiles/tests.py new file mode 100644 index 0000000..2247054 --- /dev/null +++ b/itf/profiles/tests.py @@ -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 +"""} + diff --git a/itf/profiles/utils.py b/itf/profiles/utils.py new file mode 100644 index 0000000..24fa1f7 --- /dev/null +++ b/itf/profiles/utils.py @@ -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 diff --git a/itf/profiles/views.py b/itf/profiles/views.py new file mode 100644 index 0000000..40443be --- /dev/null +++ b/itf/profiles/views.py @@ -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') +