/django-toollib

Django common module, including sending email, rendering template, pagination, verification code etc.

Primary LanguagePythonOtherNOASSERTION

Django-toollib is a common module building on top of django, include sending email, rendering template, pagination, verification code etc.

If you have any question, please contact dev-web-sys@funshion.com

Github link: https://github.com/cjie888/django-toollib

Sending Email

You should change the email configuration in setting.py file first.

EMAIL_HOST = "mail.XXXXX.com"
EMAIL_HOST_USER = "email_user"
EMAIL_HOST_PASSWORD = "email_password"
EMAIL_PORT = 25
EMAIL_USE_TLS = False
EMAIL_FROM = "email_from"
  • Sending normal email

Programing interface:
send_mail(subject, body, to, cc, use_thread=True) 
Parameters:
subject - the email subject
body  - the email body
to - the recipients (a list)
cc - copy to(a list)
use_thread - whether to start a thread to send email 


A simple example demonstrating the use of the programmatic interface:
from toollib.email import send_mail
send_mail("subject_not_use_thread", "body_not_use_thread", ["xxx@domain.com"], [], False)
* Sending html template email

Programing interface:
 
send_html_template_email(subject, template_name, data, to, cc, use_thread=True)
Parameters:
template_name - the email template
data  - the data render the template(a dictionary)
the parameters of subject, to, cc, use_thread is the same as sending noraml email.


A simple example demonstrating the use of the programmatic interface:
from toollib.email import send_html_template_email
send_html_template_email("template_subject_not_use_thread", 'email.html', {'username':'test'}, ["xxx@domain.com"], [], False)

Rendering template and json

  • render json
    It is a decorator which wrapping the json or data to http response.
  • render template

Programing interface:
 
render_template(template, request, **kwargs)
Parameters:
template - the template name
request - the http request
kwargs - the template parameter

A simple example demonstrating the use of the programmatic interface:
from toollib.render import render_template
response = render_template('email.html', None, username = 'render_test')

Pagination


Programing interface:
 
get_page(query_set, page_no, page_size)
Parameters:
query_set - the query set
page_no  - the page no (int)
page_size - the page size of every page


A simple example demonstrating the use of the programmatic interface:
from toollib.page import get_page
query_set = MyModel.objects.all()
page = get_page(query_set, 2, 3)
### Get Client(user) IP Address
A simple example demonstrating the use of the programmatic interface:
from toollib.client import get_client_ip
clinet_ip = get_client_ip(request)
If you use nginx as a proxy, you should add configuration in nginx.conf.
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
If you use haproxy as a proxy, you should add configuration in haproxy.cfg.
option forwardfor
### [Verification Code](code.md)