Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- how to overide in forms queryset none() attribute and somehow allow to save the field?
- class Visit(Model):
- reference_visit = models.ForeignKey('self',
- help_text="Visit needs a refrence to Prior Visits",
- null=True, blank=True)
- show_prior_responses = models.BooleanField(default=False,
- help_text="Show PriorResponses")
- # has many field but i am making it short.
- def __unicode__(self):
- result = """Visit id:%s pt:%s""" % (self.id, self.patient.id)
- return result
- class VisitSetupForm(Form):
- list_visit_ids = ModelChoiceField(
- queryset=Visit.objects.none(),
- empty_label='Select Revisit ID',required=False)
- show_prior_visit = ModelChoiceField(
- queryset=User.objects.all(),
- empty_label="Select User for Revisit",required = False)
- def setup(request):
- """
- Allow an Admin user the ability to setup a patient & visit all at once.
- """
- if request.user.is_superuser:
- form_class = AdminVisitSetupForm
- all_topics = True
- else:
- form_class = VisitSetupForm
- all_topics = False
- f = form_class()
- # Get a list of topics for each report.
- report_topics = {}
- for r in Interview.objects.all():
- report_topics[r.id] = [t['ad'] for t in r.topics.values('ad')]
- data = {
- 'superuser':request.user.is_superuser,
- 'report_topics':simplejson.dumps(report_topics)
- }
- try:
- request.user.reviewer
- data['reviewer'] = True
- except:
- pass
- if request.method == "POST":
- f = form_class(request.POST)
- if f.is_valid():
- # Create the patient, generate a password, and send them on their way.
- cd = f.cleaned_data
- patient = None
- if cd['revisit']:
- # Check for an existing user first.
- try:
- patient = Patient.objects.get(username=cd['username'])
- except Patient.DoesNotExist, e:
- data['form'] = f
- data['msg'] = 'There is no user with this username.'
- return render_to_response('visit/setup.html', data, context_instance=RequestContext(request))
- admin_user = get_user(request)
- organization = None
- if admin_user:
- organization = admin_user.organization
- if patient and not request.user.is_superuser:
- # Make sure the patient they've selected is one of their own.
- if patient.organization != organization:
- return HttpResponseForbidden('You are not allowed to see this page.')
- if not patient:
- password = generate_password()
- user = User.objects.create_user(cd['username'], cd['contact_email'], password)
- user.first_name = cd['first_name']
- user.last_name = cd['last_name']
- user.save()
- patient = Patient(
- user=user,
- username=user.username,
- contact_phone=cd['contact_phone'],
- date_of_birth=cd['date_of_birth'],
- email=user.email,
- first_name=user.first_name,
- gender=cd['gender'],
- last_name=user.last_name,
- maiden_name=cd['maiden_name'],
- organization=organization,
- patient_type=cd['patient_type'],
- security_answer=cd['security_answer'],
- security_question=cd['security_question'],
- )
- patient.save()
- # Send them an email.
- t = loader.get_template('www/new_account.txt')
- c = Context({
- 'password':'%s-%s-%s' % (password[:3], password[3:5], password[5:]),
- 'patient':patient
- })
- msg = t.render(c)
- try:
- send_mail(
- 'A request by your physician to do an online medical history before your appointment.',
- msg,
- 'support@careprep.com',
- [user.email]
- )
- except Exception, e:
- log.error('Could not send email for new account %s because: [%s]' % (user.username, e))
- request.session['password'] = password
- # Create the Visit, too.
- interview = cd['interview']
- list_visit_ids = cd['list_visit_ids']
- print list_visit_ids
- visit = Visit(
- reference_visit = cd['list_visit_ids'],
- show_prior_responses = cd['show_prior_responses'],
- patient=patient
- )
- if request.user.is_superuser:
- topics = cd['topics']
- else:
- topics = set(list(interview.topics.all()) + list(cd['topics']))
- reviewer_mode = cd.get('reviewer_mode') or patient.patient_type == 'Reviewer'
- url, visit = initialize_visit(
- request,
- patient=patient,
- starting_section=interview.starting_section,
- visit_title='%s %s' % (patient, interview.title),
- topics=topics,
- reviewer_mode=reviewer_mode,
- chief_complaint=cd['chief_complaint'],
- location=cd['interview_site'],
- reference_visit = cd['list_visit_ids'],
- show_prior_responses = cd['show_prior_responses'],
- )
- next_url = "/visit/confirmation/%s/%s/?next=%s" % (patient.user.id, interview.id, url)
- else:
- v = Visit.objects.get(pk=request.POST['list_visit_ids'])
- print v
- return HttpResponseRedirect(next_url)
- # all the fields that are not given pls ignore.
- class MyForm(forms.Form):
- my_input = forms.CharField()
- def clean_my_input(self):
- input = self.cleaned_data.get('my_input')
- try:
- return MyModel.objects.get(pk=input) # add a filter here if you want
- # (whatever filters you were using in the queryset argument)
- except MyModel.DoesNotExist:
- raise forms.ValidationError("Doesn't exist / is invalid")
- return input
Add Comment
Please, Sign In to add comment