Hi all,
im a newbie, developing online address book application using django,
mod_python, apache2, postgresql.

i have two tables in my database viz login_table and contact_table..
login_table stores username and password.. contact table stores first
name, last name, phone number, email id.. username field in
login_table is foreignkey to contact_table.. now my application want
to access all the records from contact_table which corresponds to a
particular username in the login_table.. but my code gives
IntegrityError when try to do so.. how to fix this problem?

===forms.py====
import re
from django.contrib.auth.models import User
from OnlineAddressBook.app.models import *
from django.core.exceptions import ObjectDoesNotExist
from django.forms.widgets import Widget
from django import forms

class LoginForm(forms.Form):
        usr_name = forms.CharField(label="User Name:", max_length=50)
        pswd = forms.CharField(label='Enter
password:',widget=forms.PasswordInput(render_value=False))

        def clean_usr_name(self):
                usr_name = self.cleaned_data['usr_name']
                if not re.search(r'^[a-z]|[A-Z]$', usr_name):
                        raise forms.ValidationError('user Name can only contain
alphabets.')
                        return usr_name




class RegisterForm(forms.Form):
        usr_name = forms.CharField(label='Enter your user
name:',max_length=50)
        pswd = forms.CharField(label='Password:',widget=forms.PasswordInput
(render_value=False))



class AddForm(forms.Form):
        fname = forms.CharField(label='Enter First Name:',max_length=50)
        lname = forms.CharField(label='Enter Last Name:',max_length=50)
        ph_num = forms.CharField(label='Enter Mobile Number:',max_length=10)
        email = forms.EmailField(label='Enter Email ID:',max_length=75)

        def clean_fname(self):
                fname = self.cleaned_data['fname']
                if not re.search(r'^[a-z]|[A-Z]$',fname):
                        raise forms.ValidationError('Username can only contain 
alphanumeric
characters and the underscore.')
                return fname

        def clean_lname(self):
                lname = self.cleaned_data['lname']
                if not re.search(r'^[a-z]|[A-Z]$',lname):
                        raise forms.ValidationError('Username can only contain 
alphanumeric
characters and the underscore.')
                return lname

        def clean_ph_num(self):
                ph_num = self.cleaned_data['ph_num']
                if not re.search(r'^\d+$',ph_num):
                        raise forms.ValidationError('Mobile number should 
contain 10 digits
only')
                return ph_num



class EditForm(forms.Form):
        fname = forms.CharField(label='First Name:',max_length=50)
        lname = forms.CharField(label='Last Name:',max_length=50)
        ph_num = forms.CharField(label='Number:',max_length=10)
        email = forms.EmailField(label='Email ID:',max_length=75)

class RemoveForm(forms.Form):
        fname = forms.CharField(label='Enter the First Name of the
person:',max_length=50)
        ph_num = forms.CharField(label='Enter the Mobile Number of the
person:',max_length=10)

===views.py===
from django.http import HttpResponse
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.template import RequestContext
from django.shortcuts import get_object_or_404
from OnlineAddressBook.app.models import *
from OnlineAddressBook.app.forms import *
from django.contrib.auth import logout
from django.contrib import auth


def index(request):
        return render_to_response('index.html')
def error(request):
        return render_to_response('error.html')

def login(request):
        if request.method == 'POST':
                form = LoginForm(request.POST)
                if form.is_valid():
                        try:
                                obj = 
login_table.objects.get(usr_name=request.POST['usr_name'])
                                if obj.pswd == request.POST['pswd']:
                                        #return 
HttpResponseRedirect('/user_page')
                                        return 
render_to_response('user_page.html',{'form':form})
                        except login_table.DoesNotExist:
                                        #return HttpResponse('password doesnt 
match')
                                        return HttpResponseRedirect('/error')
        else:
                form = LoginForm()
                variables = RequestContext(request, {'form':form})
                return render_to_response('login.html',variables)

def register(request):
        if request.method == 'POST':
                form = RegisterForm(request.POST)
                if form.is_valid():
                        reg = login_table( usr_name = 
form.cleaned_data['usr_name'], pswd =
form.cleaned_data['pswd'])
                        reg.save()
                        return HttpResponseRedirect('/thanks/')
        else:
                form = RegisterForm()
                variables = RequestContext(request, {'form':form})
                return render_to_response('register.html',variables)


def thanks(request):
        return render_to_response('thanks.html')

def logout_page(request):
        try:
                del request.session['usr_name_id']
        except keyError:
                pass
        return HttpResponseRedirect('/')
def add(request):
        if request.method=='POST':
                form = AddForm(request.POST)
                if form.is_valid():
                        con = contact_table(fname = 
form.cleaned_data['fname'],lname =
form.cleaned_data['lname'],ph_num = form.cleaned_data['ph_num'],email
= form.cleaned_data['email'])
                        con.save()
                        return HttpResponseRedirect('/')
        else:
                form = AddForm()
                variables = RequestContext(request,{'form':form})
                return render_to_response('add.html',variables)

====models.py====
from django.conf import settings
import binascii
from os import urandom
from base64 import b64encode, b64decode
from django.db import models
from Crypto.Cipher import ARC4



def get_value(usr_name):
    def f(self):
        return login_table.decrypt(getattr(self, 'e_%s'%usr_name))
    return f

def set_value(usr_name):
    def f(self, value):
        setattr(self, 'e_%s'%usr_name, login_table.encrypt(value))
    return f


class login_table(models.Model):
        SALT_SIZE = 8
        usr_name = models.CharField(max_length=100,unique=True,blank=True)
        pswd = models.CharField(max_length=50,blank=True)
        def encrypt(plaintext):
                salt = urandom(login_table.SALT_SIZE)
                arc4 = ARC4.new(salt + settings.SECRET_KEY)
                plaintext = "%3d%s%s" % 
(len(plaintext),plaintext,urandom(256-len
(plaintext)))
                return "%s$%s" % (b64encode(salt), b64encode(arc4.encrypt
(plaintext)))
        def decrypt(ciphertext):
                salt, ciphertext = map(b64decode, ciphertext.split('$'))
        arc4 = ARC4.new(salt + settings.SECRET_KEY)
        plaintext = arc4.decrypt(ciphertext)
        return plaintext[3:3+int(plaintext[:3].strip())]
        def encrypted_property(username):
        return property(get_value(username), set_value(username))
        usr_name = encrypted_property('usr_name')
    pswd = encrypted_property('pswd')

        def __unicode__(self):
                return self.usr_name


class contact_table(models.Model):
        fname = models.CharField(max_length=50,unique=True)
        lname = models.CharField(max_length=50)
        ph_num = models.CharField(max_length=50)
        email = models.EmailField(max_length=75)
        usr_name = models.ForeignKey(login_table)
        def __unicode__(self):
                return '%s%s%s%s' % 
(self.fname,self.lname,self.ph_num,self.email)











--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to