Skip to content

Commit

Permalink
Merge pull request #23 from justicehub-in/subscribe_list
Browse files Browse the repository at this point in the history
Add subscribable list to mailchimp
  • Loading branch information
Swati Jaiswal authored Jun 22, 2020
2 parents 83a7f0a + 1b0746b commit 948a6ea
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 7 deletions.
26 changes: 26 additions & 0 deletions ckanext/justicehub_theme/fanstatic/subscribe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
ckan.module('justicehub_theme_subscribe', function ($) {
return {
initialize: function () {
function subscribeSubmit(event) {
console.log(event);
var url = event.target.action;
var request = new XMLHttpRequest();
request.open('POST', url, true);
request.onload = function() {
// success
console.log(request.responseText);
};

request.onerror = function() {
// request failed
};

request.send(new FormData(event.target));
event.preventDefault();
}
console.log("initialized");
document.getElementById("subscribe-form").addEventListener("submit", subscribeSubmit);
}
};
});

51 changes: 49 additions & 2 deletions ckanext/justicehub_theme/plugin.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
import json

import requests

import ckanext.justicehub_theme.logic.action as jh_action
from sqlalchemy import MetaData
from sqlalchemy.sql import select

import ckan.logic as logic
import ckan.lib.base as base
import ckan.model as model
import ckan.plugins as plugins
import ckan.plugins.toolkit as toolkit
from ckan.common import c,request
import ckan.lib.navl.dictization_functions as dict_fns

from pylons import config

import ckan.controllers.organization as org

cached_tables = {}

clean_dict = logic.clean_dict
parse_params = logic.parse_params
tuplize_dict = logic.tuplize_dict

def package_activity_stream(id):
context = {'model': model, 'session': model.Session,
Expand Down Expand Up @@ -184,6 +191,9 @@ def after_map(self, map):
map.connect('jhorg_stats', '/jhorg/stats/{id}',
controller='ckanext.justicehub_theme.plugin:JHOrgController',
action='org_stats')
map.connect('jhsubscribe', '/subscribe',
controller='ckanext.justicehub_theme.plugin:SubscribeController',
action='subscribe')
return map

def before_map(self, map):
Expand All @@ -193,6 +203,9 @@ def before_map(self, map):
map.connect('jhorg_stats', '/jhorg/stats/{id}',
controller='ckanext.justicehub_theme.plugin:JHOrgController',
action='org_stats')
map.connect('jhsubscribe', '/subscribe',
controller='ckanext.justicehub_theme.plugin:SubscribeController',
action='subscribe')
return map


Expand Down Expand Up @@ -248,4 +261,38 @@ def org_stats(self, id):
downloads += get_package_avg_downloads(package)
visits += get_package_visits(package)['total']
c.group_dict.update({'downloads':downloads, 'visits': visits})
return plugins.toolkit.render('organization/stats.html', extra_vars={'group_type': group_type, 'downloads': downloads})
return plugins.toolkit.render('organization/stats.html', extra_vars={'group_type': group_type, 'downloads': downloads})

class SubscribeController(base.BaseController):
def subscribe(self):
basic_auth_key = config.get("mailchimp_api_key", "")
subscriber_list_id = config.get("mailchimp_list_id", "")
subscriber_tag_id = config.get("mailchimp_tag_id", "")
mailchimp_base_url = config.get("mailchimp_base_url", "")
url = mailchimp_base_url + subscriber_list_id + "/members"

request_body = clean_dict(dict_fns.unflatten(
tuplize_dict(parse_params(request.params))))

payload = {
"email_address": str(request_body.get("email", "")),
"status": "subscribed"
}
headers = {
'Content-Type': 'application/json',
'Authorization': 'Basic '+ basic_auth_key
}

response = requests.request("POST", url, headers=headers, data = json.dumps(payload))

print(response.text.encode('utf8'))

tag_url = mailchimp_base_url + subscriber_list_id + "/segments/" + subscriber_tag_id + "/members"

payload = {
"email_address": str(request_body.get("email", ""))
}

response = requests.request("POST", tag_url, headers=headers, data = json.dumps(payload))

print response.text.encode('utf8')
12 changes: 7 additions & 5 deletions ckanext/justicehub_theme/templates/footer.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<footer class="site-footer">

{% resource "justicehub_theme/subscribe.js"%}
<div class="container">

{% block footer_content %}
Expand Down Expand Up @@ -38,13 +38,15 @@
<div class="col col-md-1 social-links">
</div>

<div class="col col-md-5 social-links">
<div class="col col-md-5 social-links" data-module="justicehub_theme_subscribe">
<form id="subscribe-form" method="post" action="{{h.url_for('jhsubscribe')}}">
<div class="input-group">
<input type="url" class="form-control input-lg" placeholder="Enter you email id">
<input type="email" class="form-control input-lg" placeholder="Enter you email id" name="email">
<span class="input-group-btn">
<button class="btn btn-default btn-lg" type="button">Subscribe</button>
<button class="btn btn-default btn-lg" type="submit">Subscribe</button>
</span>
</div>
</form>
</div>
</div>

Expand Down Expand Up @@ -101,4 +103,4 @@
{% endif %}
{% endblock %}

</footer>
</footer>

0 comments on commit 948a6ea

Please sign in to comment.