Example #1
0
def register(request,reg_form=None,coll_form=None):
    dajax=Dajax()
    if request.method=='POST':
        form = RegistrationForm(deserialize_form(reg_form))
        collegeform = CollegeForm(deserialize_form(coll_form))
        if form.is_valid():
            if not collegeform.is_valid():
                collist = form.cleaned_data['college'].split('|')
                collquery = College.objects.using(mainsite_db).filter(name = collist[0],city=collist[1], state=collist[2])
                if collquery.count():
                    college=collquery[0]
                else:
                    college = College(name=collist[0],city=collist[1],state=collist[2])
                    college.save(using='mainsite')
            else:
                college = collegeform.instance
                college.save(using='mainsite')

            cleaned_form = form.cleaned_data
            shaastraid = cleaned_form['shaastra_id']
            if not id_in_db(shaastraid):
                new_user = User(first_name=cleaned_form['first_name'],last_name=cleaned_form['last_name'],username=cleaned_form['username'],email=cleaned_form['email']) 
                new_user.set_password('default')
                new_user.is_active = True
                new_user.save(using='mainsite')
                userprofile = UserProfile(user=new_user,
                        gender = cleaned_form['gender'],
                        branch = cleaned_form['branch'],
                        age = cleaned_form['age'],
                        mobile_number = cleaned_form['mobile_number'],
                        college_roll = cleaned_form['college_roll'],
                        college = college,
                        shaastra_id = shaastraid,
                        )
                userprofile.save(using='mainsite')
            else:
                userprofile = get_userprofile(shaastraid)                
                userprofile.gender = cleaned_form['gender']
                userprofile.branch = cleaned_form['branch']
                userprofile.age = cleaned_form['age']
                userprofile.mobile_number = cleaned_form['mobile_number']
                userprofile.college_roll = cleaned_form['college_roll']
                userprofile.college = college
                userprofile.save(using='mainsite')

            new_form = IndividualForm(initial={'shaastra_ID':shaastraid})
            html_content = render_to_string('hospi/Checkin_form.html',locals(),RequestContext(request))
            dajax.assign('#tab3',"innerHTML",html_content)
            return dajax.json()
        else:
            show_alert(dajax,"error","Form is invalid")
            return dajax.json()