Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. MAILCHIMP_API_KEY = '767ba************-us6'
  2. MAILCHIMP_SUBSCRIBE_LIST_ID = '45c*******'
  3.  
  4. import threading
  5. import mailchimp
  6. from django.conf import settings
  7.  
  8. class SendSubscribeMail(object):
  9. def __init__(self, email, fname):
  10. self.email = email
  11. self.fname = fname
  12. thread = threading.Thread(target=self.run, args=())
  13. thread.daemon = True
  14. thread.start()
  15.  
  16. def run(self):
  17. API_KEY = settings.MAILCHIMP_API_KEY
  18. LIST_ID = settings.MAILCHIMP_SUBSCRIBE_LIST_ID
  19. api = mailchimp.Mailchimp(API_KEY)
  20. try:
  21. api.lists.subscribe(LIST_ID, {'email': self.email}, merge_vars={'FNAME': self.fname}, double_optin=False)
  22. except:
  23. return False
  24.  
  25. def subscribe(request):
  26. if request.method == 'POST':
  27. fname = request.POST['fname_id']
  28. Subscribe.objects.create(fname_id = fname)
  29. email = request.POST['email_id']
  30. email_qs = Subscribe.objects.filter(email_id = email)
  31. if email_qs.exists():
  32. data = {"status" : "404"}
  33. return JsonResponse(data)
  34. else:
  35. Subscribe.objects.create(email_id = email)
  36. SendSubscribeMail(email, fname) # Send the Mail, Class available in utils.py
  37. return HttpResponse("/")
  38.  
  39. class Subscribe(models.Model):
  40. email_id = models.EmailField()
  41. fname_id = models.CharField(max_length=255, blank=True, null=True, default='')
  42. timestamp = models.DateTimeField(auto_now=True)
  43.  
  44. def __str__(self):
  45. return self.email_id
  46.  
  47. <div class="signup subs-form">
  48.  
  49. <div class="header">
  50. <p>Sign Up For Beta</p>
  51. </div>
  52. <div class="description">
  53. <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna.</p>
  54. </div>
  55.  
  56. <form method="POST" id="subscribe" class="subs-form">
  57. {% csrf_token %}
  58. <div>
  59. <input type="email" class="button-" id="email" name="email_id" placeholder="E-mail">
  60. <div class="input">
  61. <input type="text" class="button-" id="fname" name="fname_id" placeholder="First Name">
  62. <input type="submit" class="button-" id="submit" value="Join">
  63. </div>
  64. </div>
  65. </form>
  66.  
  67. </div>
  68.  
  69. $('#subscribe').submit(function(e){
  70. e.preventDefault();
  71. var email_id = $("#email").val();
  72. if(email_id){
  73. var csrfmiddlewaretoken = csrftoken;
  74. var fname_id = $("#fname").val();
  75. var email_data = {"email_id": email_id,
  76. "fname_id": fname_id,
  77. "csrfmiddlewaretoken" : csrfmiddlewaretoken};
  78. $.ajax({
  79. type : 'POST',
  80. url : '/subscribe/',
  81. data : email_data,
  82. success : function(response){
  83. $('#email').val('');
  84. if(response.status == "404"){
  85. alert("This Email is already been subscribed!");
  86. }
  87. else{
  88. alert("Thank you for Subscribing! Please Check your Email to Confirm the Subscription");
  89. }
  90. },
  91. error: function(response) {
  92. alert("Sorry Something went Wrong");
  93. $('#email').val('');
  94. }
  95. });
  96. return false;
  97. }
  98. else{
  99. alert("Please provide correct email!");
  100. }
  101. });
  102.  
  103.  
  104. function getCookie(name) {
  105. var cookieValue = null;
  106. if (document.cookie && document.cookie !== '') {
  107. var cookies = document.cookie.split(';');
  108. for (var i = 0; i < cookies.length; i++) {
  109. var cookie = jQuery.trim(cookies[i]);
  110. if (cookie.substring(0, name.length + 1) === (name + '=')) {
  111. cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
  112. break;
  113. }
  114. }
  115. }
  116. return cookieValue;
  117. }
  118. var csrftoken = jQuery("[name=csrfmiddlewaretoken]").val();
  119.  
  120. function csrfSafeMethod(method) {
  121. return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
  122. }
  123. $.ajaxSetup({
  124. beforeSend: function(xhr, settings) {
  125. if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
  126. xhr.setRequestHeader("X-CSRFToken", csrftoken);
  127. }
  128. }
  129. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement