Guest User

Untitled

a guest
Aug 15th, 2018
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.43 KB | None | 0 0
  1. how to overide in forms queryset none() attribute and somehow allow to save the field?
  2. class Visit(Model):
  3. reference_visit = models.ForeignKey('self',
  4. help_text="Visit needs a refrence to Prior Visits",
  5. null=True, blank=True)
  6. show_prior_responses = models.BooleanField(default=False,
  7. help_text="Show PriorResponses")
  8.  
  9. # has many field but i am making it short.
  10. def __unicode__(self):
  11. result = """Visit id:%s pt:%s""" % (self.id, self.patient.id)
  12. return result
  13.  
  14. class VisitSetupForm(Form):
  15. list_visit_ids = ModelChoiceField(
  16. queryset=Visit.objects.none(),
  17. empty_label='Select Revisit ID',required=False)
  18. show_prior_visit = ModelChoiceField(
  19. queryset=User.objects.all(),
  20. empty_label="Select User for Revisit",required = False)
  21.  
  22. def setup(request):
  23. """
  24. Allow an Admin user the ability to setup a patient & visit all at once.
  25. """
  26. if request.user.is_superuser:
  27. form_class = AdminVisitSetupForm
  28. all_topics = True
  29. else:
  30. form_class = VisitSetupForm
  31. all_topics = False
  32.  
  33. f = form_class()
  34.  
  35. # Get a list of topics for each report.
  36. report_topics = {}
  37.  
  38. for r in Interview.objects.all():
  39. report_topics[r.id] = [t['ad'] for t in r.topics.values('ad')]
  40.  
  41.  
  42. data = {
  43. 'superuser':request.user.is_superuser,
  44. 'report_topics':simplejson.dumps(report_topics)
  45. }
  46.  
  47. try:
  48. request.user.reviewer
  49. data['reviewer'] = True
  50. except:
  51. pass
  52.  
  53. if request.method == "POST":
  54. f = form_class(request.POST)
  55.  
  56. if f.is_valid():
  57. # Create the patient, generate a password, and send them on their way.
  58. cd = f.cleaned_data
  59.  
  60. patient = None
  61. if cd['revisit']:
  62. # Check for an existing user first.
  63. try:
  64. patient = Patient.objects.get(username=cd['username'])
  65. except Patient.DoesNotExist, e:
  66. data['form'] = f
  67. data['msg'] = 'There is no user with this username.'
  68. return render_to_response('visit/setup.html', data, context_instance=RequestContext(request))
  69.  
  70. admin_user = get_user(request)
  71. organization = None
  72. if admin_user:
  73. organization = admin_user.organization
  74.  
  75. if patient and not request.user.is_superuser:
  76. # Make sure the patient they've selected is one of their own.
  77. if patient.organization != organization:
  78. return HttpResponseForbidden('You are not allowed to see this page.')
  79.  
  80. if not patient:
  81. password = generate_password()
  82. user = User.objects.create_user(cd['username'], cd['contact_email'], password)
  83. user.first_name = cd['first_name']
  84. user.last_name = cd['last_name']
  85. user.save()
  86. patient = Patient(
  87. user=user,
  88. username=user.username,
  89. contact_phone=cd['contact_phone'],
  90. date_of_birth=cd['date_of_birth'],
  91. email=user.email,
  92. first_name=user.first_name,
  93. gender=cd['gender'],
  94. last_name=user.last_name,
  95. maiden_name=cd['maiden_name'],
  96. organization=organization,
  97. patient_type=cd['patient_type'],
  98. security_answer=cd['security_answer'],
  99. security_question=cd['security_question'],
  100.  
  101. )
  102. patient.save()
  103.  
  104. # Send them an email.
  105. t = loader.get_template('www/new_account.txt')
  106. c = Context({
  107. 'password':'%s-%s-%s' % (password[:3], password[3:5], password[5:]),
  108. 'patient':patient
  109. })
  110. msg = t.render(c)
  111.  
  112. try:
  113. send_mail(
  114. 'A request by your physician to do an online medical history before your appointment.',
  115. msg,
  116. 'support@careprep.com',
  117. [user.email]
  118. )
  119. except Exception, e:
  120. log.error('Could not send email for new account %s because: [%s]' % (user.username, e))
  121.  
  122. request.session['password'] = password
  123.  
  124. # Create the Visit, too.
  125. interview = cd['interview']
  126.  
  127. list_visit_ids = cd['list_visit_ids']
  128. print list_visit_ids
  129.  
  130. visit = Visit(
  131. reference_visit = cd['list_visit_ids'],
  132. show_prior_responses = cd['show_prior_responses'],
  133. patient=patient
  134. )
  135.  
  136. if request.user.is_superuser:
  137. topics = cd['topics']
  138. else:
  139. topics = set(list(interview.topics.all()) + list(cd['topics']))
  140.  
  141. reviewer_mode = cd.get('reviewer_mode') or patient.patient_type == 'Reviewer'
  142.  
  143. url, visit = initialize_visit(
  144. request,
  145. patient=patient,
  146. starting_section=interview.starting_section,
  147. visit_title='%s %s' % (patient, interview.title),
  148. topics=topics,
  149. reviewer_mode=reviewer_mode,
  150. chief_complaint=cd['chief_complaint'],
  151. location=cd['interview_site'],
  152. reference_visit = cd['list_visit_ids'],
  153. show_prior_responses = cd['show_prior_responses'],
  154. )
  155.  
  156. next_url = "/visit/confirmation/%s/%s/?next=%s" % (patient.user.id, interview.id, url)
  157. else:
  158. v = Visit.objects.get(pk=request.POST['list_visit_ids'])
  159. print v
  160.  
  161. return HttpResponseRedirect(next_url)
  162. # all the fields that are not given pls ignore.
  163.  
  164. class MyForm(forms.Form):
  165. my_input = forms.CharField()
  166.  
  167. def clean_my_input(self):
  168. input = self.cleaned_data.get('my_input')
  169. try:
  170. return MyModel.objects.get(pk=input) # add a filter here if you want
  171. # (whatever filters you were using in the queryset argument)
  172. except MyModel.DoesNotExist:
  173. raise forms.ValidationError("Doesn't exist / is invalid")
  174. return input
Add Comment
Please, Sign In to add comment