def set_country_preferences(request, school, conference): if request.method == 'POST': try: delegation_request = DelegationRequest.objects.get( school=school, conference=conference) except ObjectDoesNotExist: delegation_request = DelegationRequest() delegation_request.conference = conference delegation_request.school = school delegation_request.save() # remove current preferences CountryPreference.objects.filter(request=delegation_request).delete() DelegateCountPreference.objects.filter( request=delegation_request).delete() country_names = [] count = 0 country_index = 0 now = datetime.now().strftime( "%Y-%m-%d %H:%M:" ) # MySQL does not support microseconds, so we have to manually order the countries by seconds for pref_num, country_pk in request.POST.items(): if pref_num == 'total_count': try: count = int(country_pk) count_pref = DelegateCountPreference() count_pref.request = delegation_request count_pref.delegate_count = count count_pref.save() except TypeError: pass else: try: country = Country.objects.get(pk=country_pk) except Country.DoesNotExist: pass else: if is_school_registered(country.conference, school): # make sure this preference doesnt already exist if country.name not in country_names: pref = CountryPreference() pref.country = country pref.request = delegation_request pref.last_modified = now + str(country_index) pref.save() country_index = country_index + 1 country_names.append(country.name) if len(country_names) > 0: # send notification email sender = conference.email_address to = conference.email_address # Create message container - the correct MIME type is multipart/alternative. msg = MIMEMultipart('alternative') msg['Subject'] = "Country Preferences Submission: " + school.name msg['From'] = sender msg['To'] = to # Body of the message (a plain-text and an HTML version). text = "Country preferences submitted for " + school.name + "\n" for i in range(len(country_names)): text += str(i + 1) + ": " + country_names[i] + "\n" html = """\ <html> <head></head> <body> <p>Country preferences submitted for <b> """ html += school.name + "</b>:<ol>" for name in country_names: html += "<li>" + name + "</li>" html += "</ol>Total delegates requested: " + str( count) + "</p></body></html>" # Record the MIME types of both parts - text/plain and text/html. part1 = MIMEText(text, 'plain') part2 = MIMEText(html, 'html') # Attach parts into message container. # According to RFC 2046, the last part of a multipart message, in this case # the HTML message, is best and preferred. msg.attach(part1) msg.attach(part2) # Send the message via local SMTP server. s = smtplib.SMTP('localhost') s.sendmail(sender, to, msg.as_string()) s.quit() return simplejson.dumps( {'prefs': get_country_preferences_html(school, conference)})
def set_country_preferences(request, school, conference): if request.method == 'POST': try: delegation_request = DelegationRequest.objects.get(school=school,conference=conference) except ObjectDoesNotExist: delegation_request = DelegationRequest() delegation_request.conference = conference delegation_request.school = school delegation_request.save() # remove current preferences CountryPreference.objects.filter(request=delegation_request).delete() DelegateCountPreference.objects.filter(request=delegation_request).delete() country_names = [] count = 0 country_index = 0 now = datetime.now().strftime("%Y-%m-%d %H:%M:") # MySQL does not support microseconds, so we have to manually order the countries by seconds for pref_num, country_pk in request.POST.items(): if pref_num == 'total_count': try: count = int(country_pk) count_pref = DelegateCountPreference() count_pref.request = delegation_request count_pref.delegate_count = count count_pref.save() except TypeError: pass else: try: country = Country.objects.get(pk=country_pk) except Country.DoesNotExist: pass else: if is_school_registered(country.conference, school): # make sure this preference doesnt already exist if country.name not in country_names: pref = CountryPreference() pref.country = country pref.request = delegation_request pref.last_modified = now + str(country_index) pref.save() country_index = country_index + 1 country_names.append(country.name) if len(country_names) > 0: # send notification email sender = conference.email_address to = conference.email_address subject = "Country Preferences Submission: " + school.name # Body of the message (a plain-text and an HTML version). text = "Country preferences submitted for " + school.name + "\n" for i in range(len(country_names)): text += str(i+1) + ": " + country_names[i] + "\n" # Not sending HTML version for now, turning this off # html = """\ # <html> # <head></head> # <body> # <p>Country preferences submitted for <b> # """ # html += school.name + "</b>:<ol>" # for name in country_names: # html += "<li>" + name + "</li>" # # html += "</ol>Total delegates requested: " + str(count) + "</p></body></html>" send_mail( subject=subject, message=text, from_email=sender, recipient_list=[to], ) return simplejson.dumps({'prefs':get_country_preferences_html(school,conference)})
def set_country_preferences(request, school, conference): if request.method == 'POST': try: delegation_request = DelegationRequest.objects.get(school=school,conference=conference) except ObjectDoesNotExist: delegation_request = DelegationRequest() delegation_request.conference = conference delegation_request.school = school delegation_request.save() # remove current preferences CountryPreference.objects.filter(request=delegation_request).delete() DelegateCountPreference.objects.filter(request=delegation_request).delete() country_names = [] count = 0 country_index = 0 now = datetime.now().strftime("%Y-%m-%d %H:%M:") # MySQL does not support microseconds, so we have to manually order the countries by seconds for pref_num, country_pk in request.POST.items(): if pref_num == 'total_count': try: count = int(country_pk) count_pref = DelegateCountPreference() count_pref.request = delegation_request count_pref.delegate_count = count count_pref.save() except TypeError: pass else: try: country = Country.objects.get(pk=country_pk) except Country.DoesNotExist: pass else: if is_school_registered(country.conference, school): # make sure this preference doesnt already exist if country.name not in country_names: pref = CountryPreference() pref.country = country pref.request = delegation_request pref.last_modified = now + str(country_index) pref.save() country_index = country_index + 1 country_names.append(country.name) if len(country_names) > 0: # send notification email sender = conference.email_address to = conference.email_address # Create message container - the correct MIME type is multipart/alternative. msg = MIMEMultipart('alternative') msg['Subject'] = "Country Preferences Submission: " + school.name msg['From'] = sender msg['To'] = to # Body of the message (a plain-text and an HTML version). text = "Country preferences submitted for " + school.name + "\n" for i in range(len(country_names)): text += str(i+1) + ": " + country_names[i] + "\n" html = """\ <html> <head></head> <body> <p>Country preferences submitted for <b> """ html += school.name + "</b>:<ol>" for name in country_names: html += "<li>" + name + "</li>" html += "</ol>Total delegates requested: " + str(count) + "</p></body></html>" # Record the MIME types of both parts - text/plain and text/html. part1 = MIMEText(text, 'plain') part2 = MIMEText(html, 'html') # Attach parts into message container. # According to RFC 2046, the last part of a multipart message, in this case # the HTML message, is best and preferred. msg.attach(part1) msg.attach(part2) # Send the message via local SMTP server. s = smtplib.SMTP('localhost') s.sendmail(sender, to, msg.as_string()) s.quit() return simplejson.dumps({'prefs':get_country_preferences_html(school,conference)})