Importing blocks from csv

main
Bob Mottram 2023-02-10 18:42:58 +00:00
parent 5c637349f2
commit 489082a100
5 changed files with 34 additions and 41 deletions

View File

@ -1462,17 +1462,10 @@ def get_cw_list_variable(list_name: str) -> str:
return 'list' + list_name.replace(' ', '').replace("'", '')
def import_blocks(base_dir: str, nickname: str, domain: str,
filename: str) -> bool:
def import_blocking_file(base_dir: str, nickname: str, domain: str,
lines: []) -> bool:
"""Imports blocked domains for a given account
"""
lines = []
try:
with open(filename, 'r', encoding='utf-8') as fp_blocks:
lines = fp_blocks.read().splitlines()
except OSError:
print('EX: unable to import blocked instances from file ' +
filename)
if not lines:
return False
if len(lines) < 2:
@ -1571,7 +1564,7 @@ def import_blocks(base_dir: str, nickname: str, domain: str,
return True
def export_blocks(base_dir: str, nickname: str, domain: str) -> str:
def export_blocking_file(base_dir: str, nickname: str, domain: str) -> str:
"""exports account level blocks in a csv format
"""
account_directory = acct_dir(base_dir, nickname, domain)

View File

@ -1705,7 +1705,8 @@ def extract_text_fields_in_post(post_bytes, boundary: str, debug: bool,
fields_with_semicolon_allowed = (
'message', 'bio', 'autoCW', 'password', 'passwordconfirm',
'instanceDescription', 'instanceDescriptionShort',
'subject', 'location', 'imageDescription'
'subject', 'location', 'imageDescription', 'importBlocks',
'importFollows', 'importTheme'
)
if debug:
if 'password' not in message_fields:

View File

@ -147,8 +147,8 @@ from media import replace_twitter
from media import attach_media
from media import path_is_video
from media import path_is_audio
from blocking import import_blocks
from blocking import export_blocks
from blocking import import_blocking_file
from blocking import export_blocking_file
from blocking import add_account_blocks
from blocking import get_cw_list_variable
from blocking import load_cw_lists
@ -6177,7 +6177,6 @@ class PubServer(BaseHTTPRequestHandler):
'instanceLogo',
'left_col_image', 'right_col_image',
'import_follows',
'import_blocks',
'import_theme'
)
profile_media_types_uploaded = {}
@ -6226,10 +6225,6 @@ class PubServer(BaseHTTPRequestHandler):
filename_base = \
acct_dir(base_dir, nickname, domain) + \
'/import_following.csv'
elif m_type == 'import_blocks':
filename_base = \
acct_dir(base_dir, nickname, domain) + \
'/import_blocks.csv'
else:
filename_base = \
acct_dir(base_dir, nickname, domain) + \
@ -6254,25 +6249,6 @@ class PubServer(BaseHTTPRequestHandler):
nickname)
continue
if m_type == 'import_blocks':
if os.path.isfile(filename_base):
blocks_import_succeeded = False
if import_blocks(base_dir, nickname, domain,
filename):
print(nickname + ' imported blocks csv')
blocks_import_succeeded = True
try:
os.remove(filename_base)
except OSError:
print('EX: ' +
'unable to remove imported blocks file ' +
filename_base)
if blocks_import_succeeded:
continue
print('WARN: failed to import blocks from csv for ' +
nickname)
continue
if m_type == 'import_theme':
if nickname == admin_nickname or \
is_artist(base_dir, nickname):
@ -6446,6 +6422,16 @@ class PubServer(BaseHTTPRequestHandler):
actor_skills_ctr:
actor_changed = True
if fields.get('importBlocks'):
blocks_str = fields['importBlocks']
while blocks_str.startswith('\n'):
blocks_str = blocks_str[1:]
blocks_lines = blocks_str.split('\n')
if import_blocking_file(base_dir, nickname, domain,
blocks_lines):
print('blocks imported for ' + nickname)
else:
print('blocks not imported for ' + nickname)
# change password
if fields.get('password') and \
fields.get('passwordconfirm'):
@ -8409,7 +8395,7 @@ class PubServer(BaseHTTPRequestHandler):
filename = base_dir + '/exports/' + filename
nickname = get_nickname_from_actor(path)
if nickname:
blocks_str = export_blocks(base_dir, nickname, domain)
blocks_str = export_blocking_file(base_dir, nickname, domain)
if blocks_str:
msg = blocks_str.encode('utf-8')
msglen = len(msg)

View File

@ -5995,7 +5995,17 @@ def _test_extract_text_fields_from_post():
'116202748023898664511855843036\r\nContent-Disposition: ' + \
'form-data; name="attachpic"; filename=""\r\nContent-Type: ' + \
'application/octet-stream\r\n\r\n\r\n----------------------' + \
'-------116202748023898664511855843036--\r\n'
'-------116202748023898664511855843036--\r\n' + \
'Content-Disposition: form-data; name="importBlocks"; ' + \
'filename="wildebeest_suspend.csv"\r\nContent-Type: ' + \
'text/csv\r\n\r\n#domain,#severity,#reject_media,#reject_reports,' + \
'#public_comment,#obfuscate\nbgp.social,suspend,false,false,' + \
'"Wildebeest",false\ncesko.social,suspend,false,false,' + \
'"Wildebeest",false\ncloudflare.social,suspend,false,false,' + \
'"Wildebeest",false\ndogfood.social,suspend,false,false,' + \
'"Wildebeest",false\ndomo.cafe,suspend,false,false,"Wildebeest",' + \
'false\nemaw.social,suspend,false,false\n\r\n ' + \
'-----------------------------116202748023898664511855843036--\r\n'
debug = False
fields = extract_text_fields_in_post(None, boundary, debug, form_data)
assert fields['submitPost'] == 'Submit'
@ -6006,6 +6016,9 @@ def _test_extract_text_fields_from_post():
assert fields['location'] == ''
assert fields['imageDescription'] == ''
assert fields['message'] == 'This is a ; test'
if not fields['importBlocks'][1:].startswith('#domain,#severity,'):
print(fields['importBlocks'])
assert fields['importBlocks'][1:].startswith('#domain,#severity,')
def _test_speaker_replace_link():

View File

@ -2048,8 +2048,8 @@ def _html_edit_profile_filtering(base_dir: str, nickname: str, domain: str,
edit_profile_form += \
' <label class="labels">' + \
translate['Import Blocks'] + '</label>\n'
edit_profile_form += ' <input type="file" id="import_blocks" '
edit_profile_form += 'name="import_blocks" '
edit_profile_form += ' <input type="file" id="importBlocks" '
edit_profile_form += 'name="importBlocks" '
edit_profile_form += 'accept="' + BLOCKFILE_FORMATS + '">\n'
edit_profile_form += \
' <label class="labels">' + \