def PublishQuestionToFacebook(request, graph, question_slug=''): # debugprint('starting publish func: ' + str(datetime.datetime.now())) # debugprint('method=' + request.method) # #should never happen, but if we don't get a graph object passed in from the facebook decorator, #we must go back to the question page with an error if not graph: return utils.custom_redirect('ShowQuestionDetails', question_slug, fb_post_error=True) # skip exception handling here, the decorator already checked theQuestion = GetQuestionFromSlug(question_slug) # #first create a publishing object to represent that this question is now in 'published' state # and no longer be changed try: IsAlreadyPublished = (theQuestion.linked_survey.publishing_set.count() > 0) new_publishing = FacebookPublishing(linked_survey=theQuestion.linked_survey, publish_date = datetime.datetime.now(), dummy_field='dummy_data') new_publishing.save() #create bit.ly url's for the voting url's theAnswers = theQuestion.multiplechoiceansweritem_set.all().order_by('pk') ViewHelpers.CreateURLsForAnswersIfNeeded(question_slug, IsAlreadyPublished, theAnswers) #need to add rollback/transaction here, actually the whole view should be in a transaction #and rolled back if any problems except (DatabaseError, IntegrityError) as e: return DefaultErrorPage(e, mesg="There was a problem posting your question to facebook due to a database or other error. Please try again later.") fb_post_str = ConstructFBPostForQuestion(theQuestion,theAnswers) # debugprint('before call to post to FB ' + str(datetime.datetime.now()) + ' ' + request.method) # debugprint('method=' + request.method) # #result = graph.set('me/feed', message=fb_post_str) #creates an asynchronous task using RQ to post to Facebook passing it the access token # and post text try: asynch_post_job = ViewHelpers.post_to_fb_async.delay(graph.access_token, fb_post_str) except Exception as e: return utils.custom_redirect('ShowQuestionDetails', question_slug, fb_post_error=True) # debugprint('after call to post to FB ' + str(datetime.datetime.now()) + ' ' + request.method) # debugprint('method=' + request.method) return utils.custom_redirect('ShowQuestionDetails', question_slug, just_published_on_FB=True)
def PostResultsToFacebook(request, graph, question_slug=''): if not graph: return utils.custom_redirect('ShowQuestionDetails', question_slug, fb_post_error=True) theQuestion = GetQuestionFromSlug(question_slug) fb_post_str = ConstructFBResultsPostForQuestion(theQuestion) try: #result = graph.set('me/feed', message=fb_post_str) asynch_post_job = ViewHelpers.post_to_fb_async.delay(graph.access_token, fb_post_str) except Exception as e: return utils.custom_redirect('ShowQuestionDetails', question_slug, fb_post_error=True) return utils.custom_redirect('ShowQuestionDetails', question_slug, just_published_results_on_FB=True)
def ShowResultsEmailForm(request, question_slug=''): theQuestion = GetQuestionFromSlug(question_slug) current_user = request.user if request.method == "POST": form = EmailResultsContactForm(request.POST) if form.is_valid(): #save working email if not already there try: user_prof = UserProfile.objects.get( linked_user__pk=current_user.pk) except UserProfile.DoesNotExist: user_prof = UserProfile() user_prof.linked_user = current_user user_prof.working_email = form.cleaned_data['user_email'] user_prof.save() list_of_emails = ViewHelpers.ExtractListOfEmails( form.cleaned_data['list_of_email_addr']) #send the email SendResultsViaEmail(theQuestion, current_user, form, list_of_emails) return utils.custom_redirect('ShowQuestionDetails', question_slug, just_published_results_via_email=True) else: return render_to_response('email_contact_form.html', { 'form': form, 'question': theQuestion }, context_instance=RequestContext(request)) else: userprof = None try: userprof = UserProfile.objects.get(linked_user__id=request.user.id) except UserProfile.DoesNotExist: pass form = EmailResultsContactForm( initial={'user_email': userprof.working_email if userprof else ''}) return render_to_response('email_contact_form.html', { 'form': form, 'question': theQuestion }, context_instance=RequestContext(request))
def PostResultsToFacebook(request, graph, question_slug=''): if not graph: return utils.custom_redirect('ShowQuestionDetails', question_slug, fb_post_error=True) theQuestion = GetQuestionFromSlug(question_slug) fb_post_str = ConstructFBResultsPostForQuestion(theQuestion) try: #result = graph.set('me/feed', message=fb_post_str) asynch_post_job = ViewHelpers.post_to_fb_async.delay( graph.access_token, fb_post_str) except Exception as e: return utils.custom_redirect('ShowQuestionDetails', question_slug, fb_post_error=True) return utils.custom_redirect('ShowQuestionDetails', question_slug, just_published_results_on_FB=True)
def ShowEmailForm(request, question_slug=''): theQuestion = GetQuestionFromSlug(question_slug) #if POST we are processing the email form and we need to validate and send out the email if request.method == "POST": form = EmailContactForm(request.POST) if form.is_valid(): #process the form by setting the question to published, extracting the email addresses to send to, #and the user's own address publishing_and_email_dict = ViewHelpers.ParseInputAndSaveModels( form, request.user, theQuestion) #send the email ViewHelpers.SendQuestionViaEmail(publishing_and_email_dict, request.user, form, theQuestion) return utils.custom_redirect('ShowQuestionDetails', question_slug, just_published_via_email=True) else: return render_to_response('email_contact_form.html', { 'form': form, 'question': theQuestion }, context_instance=RequestContext(request)) #if this is a GET show the email form else: #if user has a profile, get it so we can display the user's email address, else don't worry about it userprof = None try: userprof = UserProfile.objects.get(linked_user__id=request.user.id) except UserProfile.DoesNotExist: pass form = EmailContactForm( initial={'user_email': userprof.working_email if userprof else ''}) return render_to_response('email_contact_form.html', { 'form': form, 'question': theQuestion }, context_instance=RequestContext(request))
def ShowResultsEmailForm(request, question_slug=''): theQuestion = GetQuestionFromSlug(question_slug) current_user = request.user if request.method == "POST": form = EmailResultsContactForm(request.POST) if form.is_valid(): #save working email if not already there try: user_prof=UserProfile.objects.get(linked_user__pk=current_user.pk) except UserProfile.DoesNotExist: user_prof = UserProfile() user_prof.linked_user = current_user user_prof.working_email = form.cleaned_data['user_email'] user_prof.save() list_of_emails = ViewHelpers.ExtractListOfEmails(form.cleaned_data['list_of_email_addr']) #send the email SendResultsViaEmail(theQuestion, current_user, form, list_of_emails) return utils.custom_redirect('ShowQuestionDetails', question_slug, just_published_results_via_email=True) else: return render_to_response('email_contact_form.html', {'form' : form, 'question' : theQuestion}, context_instance=RequestContext(request)) else: userprof = None try: userprof = UserProfile.objects.get(linked_user__id=request.user.id) except UserProfile.DoesNotExist: pass form = EmailResultsContactForm(initial={'user_email' : userprof.working_email if userprof else ''}) return render_to_response('email_contact_form.html', {'form' : form, 'question' : theQuestion}, context_instance=RequestContext(request))
def DeleteQuestion(request, question_slug=''): if request.method != "POST": return DefaultErrorPage(mesg="Delete does not work with GET.") theQuestion = GetQuestionFromSlug(question_slug) try: DeleteRelatedPublishings(theQuestion) linked_survey = theQuestion.linked_survey theQuestion.delete() linked_survey.delete() #need to add transcation/rollback here except (DatabaseError, IntegrityError) as e: return DefaultErrorPage(e, "Could not delete this question due to a database error.") return utils.custom_redirect('ShowQuestionsList', just_deleted_question=True)
def DeleteQuestion(request, question_slug=''): if request.method != "POST": return DefaultErrorPage(mesg="Delete does not work with GET.") theQuestion = GetQuestionFromSlug(question_slug) try: DeleteRelatedPublishings(theQuestion) linked_survey = theQuestion.linked_survey theQuestion.delete() linked_survey.delete() #need to add transcation/rollback here except (DatabaseError, IntegrityError) as e: return DefaultErrorPage( e, "Could not delete this question due to a database error.") return utils.custom_redirect('ShowQuestionsList', just_deleted_question=True)
def ShowEmailForm(request, question_slug=''): theQuestion = GetQuestionFromSlug(question_slug) #if POST we are processing the email form and we need to validate and send out the email if request.method == "POST": form = EmailContactForm(request.POST) if form.is_valid(): #process the form by setting the question to published, extracting the email addresses to send to, #and the user's own address publishing_and_email_dict=ViewHelpers.ParseInputAndSaveModels(form, request.user, theQuestion) #send the email ViewHelpers.SendQuestionViaEmail(publishing_and_email_dict, request.user, form, theQuestion) return utils.custom_redirect('ShowQuestionDetails', question_slug, just_published_via_email=True) else: return render_to_response('email_contact_form.html', {'form' : form, 'question' : theQuestion}, context_instance=RequestContext(request)) #if this is a GET show the email form else: #if user has a profile, get it so we can display the user's email address, else don't worry about it userprof = None try: userprof = UserProfile.objects.get(linked_user__id=request.user.id) except UserProfile.DoesNotExist: pass form = EmailContactForm(initial={'user_email' : userprof.working_email if userprof else ''}) return render_to_response('email_contact_form.html', {'form' : form, 'question' : theQuestion}, context_instance=RequestContext(request))
def PublishQuestionToFacebook(request, graph, question_slug=''): # debugprint('starting publish func: ' + str(datetime.datetime.now())) # debugprint('method=' + request.method) # #should never happen, but if we don't get a graph object passed in from the facebook decorator, #we must go back to the question page with an error if not graph: return utils.custom_redirect('ShowQuestionDetails', question_slug, fb_post_error=True) # skip exception handling here, the decorator already checked theQuestion = GetQuestionFromSlug(question_slug) # #first create a publishing object to represent that this question is now in 'published' state # and no longer be changed try: IsAlreadyPublished = (theQuestion.linked_survey.publishing_set.count() > 0) new_publishing = FacebookPublishing( linked_survey=theQuestion.linked_survey, publish_date=datetime.datetime.now(), dummy_field='dummy_data') new_publishing.save() #create bit.ly url's for the voting url's theAnswers = theQuestion.multiplechoiceansweritem_set.all().order_by( 'pk') ViewHelpers.CreateURLsForAnswersIfNeeded(question_slug, IsAlreadyPublished, theAnswers) #need to add rollback/transaction here, actually the whole view should be in a transaction #and rolled back if any problems except (DatabaseError, IntegrityError) as e: return DefaultErrorPage( e, mesg= "There was a problem posting your question to facebook due to a database or other error. Please try again later." ) fb_post_str = ConstructFBPostForQuestion(theQuestion, theAnswers) # debugprint('before call to post to FB ' + str(datetime.datetime.now()) + ' ' + request.method) # debugprint('method=' + request.method) # #result = graph.set('me/feed', message=fb_post_str) #creates an asynchronous task using RQ to post to Facebook passing it the access token # and post text try: asynch_post_job = ViewHelpers.post_to_fb_async.delay( graph.access_token, fb_post_str) except Exception as e: return utils.custom_redirect('ShowQuestionDetails', question_slug, fb_post_error=True) # debugprint('after call to post to FB ' + str(datetime.datetime.now()) + ' ' + request.method) # debugprint('method=' + request.method) return utils.custom_redirect('ShowQuestionDetails', question_slug, just_published_on_FB=True)