Innovate anywhere, anytime withruncode.io Your cloud-based dev studio.
Amazon Web Services

How to Setup Custom Domain for Amazon Cloudfront

2022-07-19

We need two things for that to happen:

  • DNS should be served from amazon Route53
  • SSL for our custom domain
aws-mp-banner

You need to upload your SSL to amazon to setup in CloudFront and to do that use the following command

aws iam upload-server-certificate --server-certificate-name myServerCertificate --certificate-body file://public_key_cert_file.pem --private-key file://my_private_key.pem --certificate-chain file://my_certificate_chain_file.pem

Then you need to map a domain to CloudFront. Create a CNAME record in Route53 to link with your CloudFront URL. Thats it and you are ready to use your own domain name like cdn.yourdomain.com instead of random-name.cloudfront.net.

I have a setup with letsencrypt SSL and other commercial services too. It's the same way and it is as simple as it is.

We are here to help. Let us know if you need assistance [email protected]

Configure SSL with LetsEncrypt and nginx

$ git clone https://github.com/letsencrypt/letsencrypt

  $ cd letsencrypt

  $ ./letsencrypt-auto --agree-dev-preview --server \
    https://acme-v01.api.letsencrypt.org/directory auth

Note: Select Standalone option.

Note: Enter your Email address when asked.

By default, the certificates will be created at /etc/letsencrypt/live/

Now you need to configure Nginx by adding your pem files as showed below.

server {

      listen 443 ssl;

      server_name micropyramid.com;

      ssl_certificate /etc/letsencrypt/live/micropyramid.com/fullchain.pem;

      ssl_certificate_key /etc/letsencrypt/live/micropyramid.com/privkey.pem;

  }

You need to change the domain name to the one you want to configure.

Then, reload the Nginx using -

$ sudo service nginx reload

We are happy to help, ask if you have any difficulties by writing to us at [email protected]

Google Analytics Graphs to your Dashboard in Python Web Frameworks

E-commerce solution providers like OpenCart, Magento Provide extensions to see Google analytics data in their own dashboards as graphs. whereas there are no such plugins available in Python. In this tutorial, we will learn how to display google graphs on your website.

Open Google Developers Console, Create a new project or open existing project. enable google analytics API. create a service account and generate a JSON Key File. Use the below function to generate an access token for analytics in read-only mode.

Python:

from oauth2client.client import SignedJwtAssertionCredentials
import json

def get_access_token():
    ''' Get Access Token From GOOGLE'''
    SCOPE = 'https://www.googleapis.com/auth/analytics.readonly'
    KEY_FILEPATH = <location to key file>
    with open(KEY_FILEPATH) as key_file:
        key_data = json.load(key_file)
    credentials = SignedJwtAssertionCredentials(
        key_data['client_email'], key_data['private_key'], SCOPE)
    return credentials.get_access_token().access_token

you need to pass Google UserID and Google Acess Token to template. 

Javascript:

//script to load analytics
<script>
(function(w,d,s,g,js,fs){
  g=w.gapi||(w.gapi={});g.analytics={q:[],ready:function(f){this.q.push(f);}};
  js=d.createElement(s);fs=d.getElementsByTagName(s)[0];
  js.src='https://apis.google.com/js/platform.js';
  fs.parentNode.insertBefore(js,fs);js.onload=function(){g.load('analytics');};
}(window,document,'script'));
</script>

//script to show graphs
<script>
gapi.analytics.ready(function() {

  gapi.analytics.auth.authorize({
    'serverAuth': {
      'access_token': "{{google_access_token}}"
    }
  });
var dataChart1 = new gapi.analytics.googleCharts.DataChart({
    query: {
      'ids': '{{google_userid}}',
      'start-date': '30daysAgo',
      'end-date': 'today',
      'metrics': 'ga:sessions',
      'dimensions': 'ga:date',
      'sort': '-ga:date'
    },
    chart: {
      'container': 'chart-1-container',
      'type': 'LINE',
      'options': {
        'width': '100%'
      }
    }
  });
  dataChart1.execute();

//To change when selectbox value is changed

$("#chart-1").change(function(){
  dataChart1.set({query: {"start-date": $("#chart-1").val()}, chart:{"type":$("#chart-type-1").val()}})
  dataChart1.execute();
})
$("#chart-type-1").change(function(){
  dataChart1.set({query: {"start-date": $("#chart-1").val()}, chart:{"type":$("#chart-type-1").val()}})
  dataChart1.execute();
})
});
</script>

HTML

<div class='col-md-12 graph_wrap'>
<span>Users Sessions</span>
# select box to change type of graph
<span class="col-md-4 select_box">
  <select id="chart-type-1">
    <option value="LINE">Line</option>
    <option value="COLUMN">Column</option>
    <option value="TABLE">Table</option>
  </select>
</span>
# select box to switch between 1 week and 1 month 
<span class="col-md-4 select_box"> 
  <select id="chart-1">
    <option value="30daysAgo">Last 30 Days</option>
    <option value="7daysAgo">Last 7 Days</option>
  </select>
</span>
<div class="clearfix"></div>
<div id="chart-1-container"></div>

With above code you will generate the user session graph which can switch between week view and month view and also change type of Graph 

Other configuration options like dimensions,metrics and filters >are available at Google Metrics Explorer.

Working with Django Plugins

Django-plugins is a package that helps you to build apps more reusable. It is currently tested with Python 2.7, 3.2, 3.3, and 3.4 along with Django versions 1.7 and 1.8. It might well work with other versions.

By using 'Django-plugins', you can define, access plugins and plugin points.

Installation:

$ pip install django-plugins

Plugins are stored as python objects and synchronized to database called plugin models(PluginPoint, Plugin).

Both Plugin and plugin models has a name and title attributes. And each plugin point/plugin can be marked as enabled/disabled/removed using 'status' field in respective models from Django admin panel.

Plugins are the classes which are hardcoded, which means they cannot be modified directly by the users. But they can change the database objects of those plugins. In this way, you can provide the flexibility to the user to change plugins.

NOTE: You should always use plugin attributes (name, title) from model objects but not from plugins.

All defined plugins and plugin points are synchronized to database tables using Django management command 'sync plugins' or 'sync DB'. 'sync plugins' command detects the removed plugin points, plugins and marks them as 'REMOVED' in the database.

Features of 'Django-plugins':

  • Synchronization with the database.
  • Plugin management from Django admin - enable/disable, order.
  • Many ways to access plugins and related plugin models - access plugins from views, templates etc.

All plugin points and plugins must reside in 'plugins.py' file in your Django app.

To register/create a plugin point:

To create a plugin point, you need to sub-class 'PluginPoint' which exists in Django plugins point module.

Example:

from djangoplugins.point import PluginPoint

class TestPluginPoint(PluginPoint):
    pass

Now, TestPluginPoint serves as a mount point for all its plugins. Now we have a point, we can start registering/creating plugins for it.

You can also define methods, attributes same as you would do for other classes. In turn, they will get inherited by each of its plugins.

Registering a plugin:

All individual plugins of this point need to subclass this so that they are registered as plugins for the plugin point, which means that subclassing plugin-point itself registers the plugin.

class TestPlugin(TestPluginPoint):
        name = 'test-plugin-1'
        title = 'Test Plugin 1'

Note: All plugins must define the name, title attributes.

Utilizing plugins:

As explained in the above section, each plugin is linked to the database record of 'Plugin' model in Django plugin. So the plugin model provides all database possibilities like filtering, sorting, searching.

* To get query set of all plugins related to plugin point, just import the plugin point and you can use 'get_plugins_qs' method.

TestPluginPoint.get_plugins_qs()

Note: This method return only enabled/active plugins.

If you need to sort or filter plugins, you should always access them via Django ORM.

TestPluginPoint.get_plugins_qs().order_by("title")

You can also access plugins in templates using 'get_plugins' template tag

{% load plugins %}

{% get_plugins your_app_name.plugins.TestPluginPoint as plugins %}

<ul>

    {% for plugin in plugins %}

        <li> {{ plugin.title }} </li>

    {% endfor %}

</ul>

To get the model instance of plugin point or plugin at any point of time, you can 'get_model' method

# Get model instance of a plugin point
testpluginpoint_obj = TestPluginPoint.get_model()

# Get model instance from plugin class.
testplugin_obj = TestPlugin.get_model()

# Get model instance by plugin name.
testplugin_obj = TestPluginPoint.get_model('test-plugin-1')

Note: 'get_model' method raises 'ObjectDoesNotExist' exception, if the object is not found.

For more details, you can refer Django-plugins official documentation here