Skip to content

Commit

Permalink
#60 Mentions System
Browse files Browse the repository at this point in the history
Initial Mentions System Completed! Can Create, Update, & Remove Global & Platform Mentions. Still need to implement the post functionality & likely make this more class based. Not sure why I didn't start it that way 🙈

Also;
- Added "get_setting_category"

- Added ability for "get_setting_value" to find category when not provided.

- Lots of clean

- Probably a few other things I forgot
  • Loading branch information
VenomStyx committed Apr 24, 2022
1 parent d55def6 commit b84264e
Show file tree
Hide file tree
Showing 6 changed files with 423 additions and 75 deletions.
4 changes: 2 additions & 2 deletions Docs/Reading Encrypted Settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ If you ever find yourself in need of reading the encrypted settings, open up `Py
# Import settings & create settings object
from resources.config import settings_core
settings = settings_core()
# Read encrypted setting
decrypted_setting = settings.get_setting_value("CATEGORY","SETTING")
# Read encrypted setting. Category not required but faster.
decrypted_setting = settings.get_setting_value(category="CATEGORY", setting="SETTING_ID)
print(decrypted_setting)
```
113 changes: 81 additions & 32 deletions resources/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import os
import pathlib
import configparser
import ast


from resources.crypt import Crypt, Key
from resources.utility import string_to_list_of_dictionaries
Expand All @@ -38,10 +40,6 @@







# _____ _
# / __ \ |
# | / \/ | __ _ ___ ___ ___ ___
Expand All @@ -60,27 +58,16 @@ def __init__(self):
self.current_path = current_path
self.saved_path = str(self.current_path) + "saved/"



##### STORAGE
self.published_posts_file = cfg.get("storage","posts_file")
self.published_posts_file_location = "saved/" + self.published_posts_file
self.published_posts_file_location_full = current_path + self.published_posts_file



self.scheduled_posts_file = cfg.get("storage","scheduled_posts_file")
self.scheduled_posts_file_location = "saved/" + self.scheduled_posts_file
self.scheduled_posts_file_location_full = current_path + self.scheduled_posts_file # This needs to be changed for S3 support



self.uploaded_media_dir = cfg.get("storage","uploaded_media_dir")
self.full_uploaded_media_dir = self.saved_path + self.uploaded_media_dir




##### ENCRYPTION
self.key_location = cfg.get("encryption","hidden_key_location")
self.crypt = None
Expand All @@ -106,28 +93,29 @@ def __init__(self):
print('user needs to setup initial key')


##### VALID ENCRYPTION
## Valid Encryption Key
if self.crypt_setup:

##### ACCOUNTS
media_accounts_temp = cfg.get("accounts","media_accounts")
if media_accounts_temp != "None":
self.media_accounts = string_to_list_of_dictionaries(self.get_setting_value("accounts", "media_accounts"))
self.media_accounts = string_to_list_of_dictionaries(self.get_setting_value(category="accounts", setting="media_accounts"))
else:
self.media_accounts = None


##### S3 CREDENTIALS
self.s3_access = self.get_setting_value("accounts", "hidden_s3_access")
self.s3_secret = self.get_setting_value("accounts", "hidden_s3_secret")
self.s3_endpoint = self.get_setting_value("accounts", "hidden_s3_endpoint")
self.s3_bucket = self.get_setting_value("accounts", "hidden_s3_bucket")
self.s3_access = self.get_setting_value(category="accounts", setting="hidden_s3_access")
self.s3_secret = self.get_setting_value(category="accounts", setting="hidden_s3_secret")
self.s3_endpoint = self.get_setting_value(category="accounts", setting="hidden_s3_endpoint")
self.s3_bucket = self.get_setting_value(category="accounts", setting="hidden_s3_bucket")

##### S3 SETUP
self.storage = Storage(self.s3_access, self.s3_secret, self.s3_endpoint, self.s3_bucket)


##### Global Mention IDs
self.global_mention_ids = self.get_setting_value("posting", "global_mention_ids")
self.global_mention_ids = ast.literal_eval(self.get_setting_value(category="posting", setting="global_mention_ids"))

else:
self.media_accounts = None
Expand All @@ -141,7 +129,10 @@ def __init__(self):
self.no_posts_description = cfg.get("app","no_posts_description")
self.post_not_scheduled_for_reason_time_in_past = cfg.get("app","post_not_scheduled_for_reason_time_in_past")
self.value_redaction_message = cfg.get("app", "value_redaction_message")

self.new_gid_mention_platform_message = cfg.get("app", "new_gid_mention_platform_message")
self.new_gid_mention_platform_id_message = cfg.get("app", "new_gid_mention_platform_id_message")
self.new_global_id_message = cfg.get("app", "new_global_id_message")
self.global_mentions_updated_message = cfg.get("app", "global_mentions_updated_message")

##### PERFORMANCE
self.posts_cache_time = float(cfg.get("performance","posts_cache_time"))
Expand All @@ -152,12 +143,12 @@ def __init__(self):
self.supported_video_types = cfg.get("media","supported_video_types").split(",")
self.supported_audio_types = cfg.get("media","supported_audio_types").split(",")


##### POSTING
self.utc_timezones = cfg.get("posting","utc_timezones").split(",")
self.default_timezone = cfg.get("posting","default_timezone")



def reload_config(self):
cfg.read(configpath)

Expand All @@ -178,21 +169,59 @@ def write_encrypted_setting(self, category, setting, value):
print(e)
return None

##### GET ALL SETTINGS CATEGORIES
########## GET ALL SETTINGS CATEGORIES
#####
def get_all_setting_categories(self):
return cfg.sections()



########## GET SETTING CATEGORY
#####
def get_setting_category(self, setting):
"""
Returns the category of a setting
"""

## Initialize
cat = None

##### GET ALL SETTINGS IN CATEGORY
## Look through Categories
for category in self.get_all_setting_categories():

## Get options in Category
options = cfg.options(category)

## Look for match in category
if setting in cfg.options(category):
cat = category
break

## Return
return cat



########## GET ALL SETTINGS IN CATEGORY
#####
def get_all_settings_in_category(self,category):
return cfg.options(category)



##### GET SETTING VALUE
def get_setting_value(self,category,setting):
########## GET SETTING VALUE
#####
def get_setting_value(self,category=None,setting=None):

## If no setting provided, return None
if setting == None:
return None

## If no category is specified, find it
if category == None:
category = self.get_setting_category(setting)

## Get setting value
value = cfg.get(category,setting)

## Decrypt if encrypted
Expand All @@ -202,16 +231,35 @@ def get_setting_value(self,category,setting):
return value


##### SET SETTING VALUE
def set_setting_value(self,category,setting,value):

########## SET SETTING VALUE
#####
def set_setting_value(self,category=None,setting=None,value=None):

## If no setting provided, return None
if setting == None:
return None

## If no category is specified, find it
if category == None:
category = self.get_setting_category(setting)

## Encrypt if encrypted (soon)


## Set setting value
cfg.set(category,setting,value)

## Write to config file
with open(configpath, 'w') as configfile:
cfg.write(configfile)

## Reload config
self.reload_config()


##### CREATE NEW SETTING
########## CREATE NEW SETTING
#####
def create_new_setting(self,category,setting,value):
cfg.add_section(category)
cfg.set(category,setting,value)
Expand All @@ -221,7 +269,8 @@ def create_new_setting(self,category,setting,value):
self.reload_config()


##### CREATE NEW CATEGORY
########## CREATE NEW CATEGORY
#####
def create_new_category(self,category):
cfg.add_section(category)
with open(configpath, 'w') as configfile:
Expand Down
3 changes: 3 additions & 0 deletions settings_template.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,15 @@ display_name_no_posts_title = Warning | Title - No Posts Found
display_name_no_posts_description = Warning | Description - No Posts Found
display_name_post_not_scheduled_for_reason_time_in_past = Warning | Attempt to Schedule Post in the Past
display_name_value_redaction_message = Info | Hidden Value Redacted Message
display_name_global_mentions_updated_message = Info | Global Mentions Updated Message

## Values
no_posts_title = No Pending Posts
no_posts_description = No posts here yet!
post_not_scheduled_for_reason_time_in_past = WARNING. Your post could not be scheduled. Please pick a date in the future above.
value_redaction_message = REDACTED
global_mentions_updated_message = Global Mentions Updated. Please Restart the Application & Refresh the page


[storage]
## Display Names
Expand Down
145 changes: 145 additions & 0 deletions widgets/global_mentions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
# _____ _
# |_ _| | |
# | | _ __ ___ _ __ ___ _ __| |_ ___
# | || '_ ` _ \| '_ \ / _ \| '__| __/ __|
# _| || | | | | | |_) | (_) | | | |_\__ \
# \___/_| |_| |_| .__/ \___/|_| \__|___/
# | |
# |_|
# -----------------------------------------------------------------------



import streamlit as st
from resources.config import settings_core
from widgets import global_mentions_registration



# _ _ _ _ _
# | | | | (_) | | | |
# | | | | __ _ _ __ _ __ _| |__ | | ___ ___
# | | | |/ _` | '__| |/ _` | '_ \| |/ _ \/ __|
# \ \_/ / (_| | | | | (_| | |_) | | __/\__ \
# \___/ \__,_|_| |_|\__,_|_.__/|_|\___||___/
# -----------------------------------------------------------------------



settings = settings_core()



# ___ _ _ _ _
# / _ \ | (_) | | (_)
# / /_\ \_ __ _ __ | |_ ___ __ _| |_ _ ___ _ __
# | _ | '_ \| '_ \| | |/ __/ _` | __| |/ _ \| '_ \
# | | | | |_) | |_) | | | (_| (_| | |_| | (_) | | | |
# \_| |_/ .__/| .__/|_|_|\___\__,_|\__|_|\___/|_| |_|
# | | | |
# |_| |_|
# -----------------------------------------------------------------------



def app():

## Initialize
global_mentions = settings.global_mention_ids
global_mentions_dict = {}
global_mentions_values = list(global_mentions.values())
new_global_mention_platform = {}

## Title
with st.expander("Global Mention IDs"):

## Dividers
st.markdown(f"-----")

## Loop on Mentions
if len(global_mentions) > 0:

########## CURRENT GLOBAL MENTIONS
for index, gID in enumerate(global_mentions):

## Initialize
gID_mention_entries = global_mentions_values[index][1]
platform_mentions = []

####
gID_input = st.text_input(label="Global ID",value=gID)

##### MENTION ENTRIES
for index2, platform_data in enumerate(gID_mention_entries):

## Initialize Platform Data
gID_platform = platform_data[0]
gID_platform_ID = platform_data[1]

## Create Setting Layout
col1, col2 = st.columns(2)

## Platform
with col1:
gID_platform_data = st.text_input(label="Platform", value=gID_platform, key=f"{gID}_platform_{index2}")

## Platform ID
with col2:
gID_platform_data_id = st.text_input(label="Platform ID", value=gID_platform_ID, key=f"{gID}_platform_ID_{index2}")

## Add to Platforms List
platform_mentions.append([gID_platform_data, gID_platform_data_id])

## New Platform Mention Registration
if index2 == len(gID_mention_entries)-1:

## Create Setting Layout
col1, col2 = st.columns(2)

## Platform
with col1:
new_gID_platform_data = st.text_input(label="Platform", value=settings.new_gid_mention_platform_message, key=f"new_gid_mention_{gID}_platform_{index2}")

## Platform ID
with col2:
new_gID_platform_data_id = st.text_input(label="Platform ID", value=settings.new_gid_mention_platform_id_message, key=f"new_gid_mention_{gID}_platform_ID_{index2}")

## Not Default Values & Not Empty
if not new_gID_platform_data == settings.new_gid_mention_platform_message or not new_gID_platform_data == "" and not new_gID_platform_data_id == settings.new_gid_mention_platform_id_message or not new_gID_platform_data_id == "" :
platform_mentions.append([new_gID_platform_data, new_gID_platform_data_id])

## Dividers
st.markdown(f"-----")

## Get list of other platform mentions
global_mentions_dict[gID] = [gID_input,platform_mentions]



########## NEW GLOBAL MENTIONS
#####

## Global Mention Registration
global_mentions_registration_return = global_mentions_registration.app()
if global_mentions_registration_return:

## Initialization
global_mention_id = global_mentions_registration_return[0]
platform_mention_data = global_mentions_registration_return[1]

## Add to Global Mention IDs
global_mentions_dict[global_mention_id] = [global_mention_id,platform_mention_data]


########## RETURN
#####

## Global Mentions Valid
if len(global_mentions_dict) > 0:
return global_mentions_dict

## Global Mentions Invalid
else:
return None


0 comments on commit b84264e

Please sign in to comment.