-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Task: Content Spacing Correction #801
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6f0bd82
spacing in actions,events and testimonials corrected
abdullai-t af93d60
Merge branch 'development' of https://github.com/massenergize/api int…
abdullai-t 051b261
added function as a task
abdullai-t 8a2e505
folder organization
abdullai-t 6e22968
two tasks add
abdullai-t 4ea6714
sending report to task creator's email
abdullai-t 244c1c2
now using one task
abdullai-t 1ab64d5
touchups
abdullai-t 2fea32a
Updates from testing
BradHN1 37c213d
for models which don't have a community
BradHN1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
114 changes: 114 additions & 0 deletions
114
src/task_queue/database_tasks/contents_spacing_correction.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import csv | ||
import datetime | ||
from django.apps import apps | ||
from sentry_sdk import capture_message | ||
from _main_.utils.emailer.send_email import send_massenergize_email_with_attachments | ||
from api.utils.constants import DATA_DOWNLOAD_TEMPLATE | ||
from database import models as db_models | ||
from django.http import HttpResponse | ||
import re | ||
|
||
|
||
FEATURE_FLAG_KEY = "update-html-format-feature-flag" | ||
PATTERNS = ["<p><br></p>", "<p>.</p>", "<p> </p>", "<br />."] | ||
PATTERN = "|".join(re.escape(p) for p in PATTERNS) | ||
|
||
""" | ||
This File is used to fix spacing for both universal contents(contents not tied to a community) and content tied to a community. | ||
There are two parts to this: | ||
1. Generate a report of the contents that need to be fixed with the following information | ||
a. Community: the community the content belongs to | ||
b. Content type: whether content is an Action/Event/Testimonial etc. | ||
c. Item name: the name or title of the content | ||
d. Field name: the field name of the content to be corrected | ||
e. Count: the number of occurrences of spacing in the content. | ||
2. Fix the spacing in the database. | ||
""" | ||
|
||
def write_to_csv(data): | ||
response = HttpResponse(content_type="text/csv") | ||
writer = csv.DictWriter(response, fieldnames=["Community", "Content Type", "Item Name", "Field Name", "Count"]) | ||
writer.writeheader() | ||
for row in data: | ||
writer.writerow(row) | ||
return response.content | ||
|
||
|
||
def get_community(instance): | ||
if instance and hasattr(instance, "community"): | ||
return instance.community.name if instance.community else "" | ||
elif instance and hasattr(instance, "primary_community"): | ||
return instance.primary_community.name if instance.primary_community else "" | ||
|
||
|
||
def get_model_instances(model_name, app_label): | ||
model = apps.get_model(app_label=app_label, model_name=model_name) | ||
filter_args = {} if model_name == "PastEvent" else {"is_deleted": False} | ||
model_instances = model.objects.filter(**filter_args) | ||
return model_instances | ||
|
||
|
||
def auto_correct_spacing(instance, field_name, field_value): | ||
for pattern in PATTERNS: | ||
field_value = field_value.replace(pattern, "") | ||
setattr(instance, field_name, field_value) | ||
instance.save() | ||
|
||
|
||
|
||
def is_feature_enabled(instance): | ||
communities = db_models.Community.objects.filter(is_deleted=False) | ||
flag = db_models.FeatureFlag.objects.filter(key=FEATURE_FLAG_KEY).first() | ||
if not flag or not flag.enabled(): | ||
return False | ||
enabled_communities = flag.enabled_communities(communities) | ||
if hasattr(instance, "community"): | ||
if not instance.community or instance.community in enabled_communities: | ||
return True | ||
elif hasattr(instance, "primary_community"): | ||
if not instance.primary_community or instance.primary_community in enabled_communities: | ||
return True | ||
return False | ||
|
||
|
||
def process_spacing_data(task=None): | ||
try: | ||
data = [] | ||
models = apps.get_models() | ||
for model in models: | ||
app_label = model._meta.app_label | ||
for field in model._meta.fields: | ||
if field.__class__.__name__ == "TextField" and app_label in ["database"]: | ||
model_name = model.__name__ | ||
field_name = field.name | ||
model_instances = get_model_instances(model_name, app_label) | ||
for instance in model_instances: | ||
field_value = getattr(instance, field_name) | ||
if field_value: | ||
count = len(re.findall(PATTERN, field_value)) | ||
if count > 0: | ||
if is_feature_enabled(instance): | ||
auto_correct_spacing(instance, field_name, field_value) | ||
|
||
data.append({ | ||
"Community": get_community(instance), | ||
"Content Type": model_name, | ||
"Item Name": instance.name if hasattr(model, "name") else instance.title, | ||
"Field Name": field_name, | ||
"Count": count, | ||
}) | ||
|
||
if len(data) > 0: | ||
report = write_to_csv(data) | ||
temp_data = {'data_type': "Content Spacing", "name":task.creator.full_name if task.creator else "admin"} | ||
file_name = "Content-Spacing-Report-{}.csv".format(datetime.datetime.now().strftime("%Y-%m-%d")) | ||
send_massenergize_email_with_attachments(DATA_DOWNLOAD_TEMPLATE,temp_data,[task.creator.email], report, file_name) | ||
|
||
return True | ||
except Exception as e: | ||
print(str(e)) | ||
capture_message(str(e), level="error") | ||
BradHN1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return False | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
else return "N/A"
in case the model doesn't have a community
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed