Skip to content

Commit

Permalink
Create template.txt and template-txt-to-json.py
Browse files Browse the repository at this point in the history
  • Loading branch information
alexzhangs committed May 9, 2024
1 parent 7d06fc0 commit 70e3960
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
43 changes: 43 additions & 0 deletions shadowsocks_manager/notification/fixtures/template-txt-to-json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""
Description:
This script converts the contents of a template.txt file into a JSON string and prints it.
The template.txt file is read and its contents are stored in the 'content' variable.
Newline characters in the content are replaced with '\r\n'.
A dictionary is created to represent the JSON data, with the template details and other fields.
The dictionary is converted to a JSON string using the json.dumps() function.
The JSON string is printed to the console.
Each carriage return and the position of the template tags in the template.txt file matter.
Usage:
python template-txt-to-json.py > template.json
"""

import json

# Read the contents of template.py
with open('template.txt', 'r') as file:
content = file.read()

# Replace newline characters with \n
content = content.replace('\n', '\r\n')

# Create a dictionary for the JSON data
data = {
"model": "notification.template",
"pk": 1,
"fields": {
"type": "account_created",
"content": content,
"is_active": True,
"dt_created": "2019-05-12T15:37:42.356Z",
"dt_updated": "2019-06-18T20:21:16.078Z"
}
}

# Convert the dictionary to a JSON string
json_data = json.dumps(data, indent=4)

# Print the JSON string
print("\n".join(["[", json_data, "]"]))
25 changes: 25 additions & 0 deletions shadowsocks_manager/notification/fixtures/template.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Subject: CREDENTIAL: VPN Account (Shadowsocks)
To: {{ account.email }}
Dear {{ account.first_name }},

Your VPN account is setup as below.

* Port: {{ account.username }}
* Password: {{ account.password }}

It's available on {{ node_accounts|length }} node(s):
{% for obj in node_accounts %}
Node #{{ forloop.counter }}:
------
{% ifchanged %}
* VPN Server: {% if obj.node.record.fqdn %}{{ obj.node.record.fqdn }}{% else %}{{ obj.node.public_ip }}{% endif %}
* Location: {{ obj.node.location }}
* Encrypt: {{ obj.node.ssmanager.encrypt }}{% if obj.node.ssmanager.is_v2ray_enabled %}
* V2Ray: Required
* Plugin: v2ray-plugin
* Plugin Options: tls;host={{ obj.node.record.fqdn }}
* Mode: Websocket (HTTPS){% endif %}
{% else %}
This node shares the same settings with above.{%endifchanged%}{% endfor %}
{% if sender.get_full_name %}
{{ sender.get_full_name }}{% endif %}

0 comments on commit 70e3960

Please sign in to comment.