Skip to content
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

Revert "feat: add beehiiv integration" #238

Merged
merged 1 commit into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,6 @@ Before running the application, ensure you have the following:
EMAIL_HOST = 'smtp.gmail.com' # Example using Gmail
EMAIL_HOST_USER = 'enter your email'
EMAIL_HOST_PASSWORD = 'enter password' #for Gmail, generate app password

# Beehiiv Integration
BEEHIIV_ENABLED=True
BEEHIIV_API_KEY=your_beehiiv_api_key # Get this from Beehiiv dashboard
BEEHIIV_PUBLICATION_ID=your_publication_id # Get this from Beehiiv dashboard
BEEHIIV_SYNC_BATCH_SIZE=50
```

5. Environment Switching: Use this script (switch-env.sh) to easily switch between environments:
Expand Down Expand Up @@ -213,7 +207,7 @@ Before running the application, ensure you have the following:
python3 manage.py migrate # To migrate the changes in Database
python3 manage.py runserver # To run the API server
redis-server # to start redis-server
celery -A core worker -Q default,beehiiv -l info # to run celery with beehiiv queue
celery -A core worker -l info # to run celery
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

Incomplete removal of Beehiiv integration detected

The Celery worker command change in README.md is correct, but there are remaining Beehiiv references that need to be removed:

  • server/apps/newsletter/views.py:
    • Remove the import from .tasks import sync_subscriber_to_beehiiv
    • Remove the task call sync_subscriber_to_beehiiv.delay(subscriber.id)
    • Remove the related log message
🔗 Analysis chain

Verify Celery worker configuration.

The Celery worker command has been simplified by removing the -Q default,beehiiv queue configuration, which aligns with the removal of Beehiiv integration.

Run this script to ensure no remaining Beehiiv-related Celery tasks exist:

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for any remaining Beehiiv-related Celery tasks
# that might need the beehiiv queue.

# Search for any remaining Beehiiv-related task definitions
rg -i "beehiiv.*task" 

# Search for any remaining queue configurations
rg -i "queue.*beehiiv"

Length of output: 48


Script:

#!/bin/bash
# Broader search for any Beehiiv references
echo "=== Searching for any Beehiiv references ==="
rg -i "beehiiv"

# Look for Celery configuration files and task definitions
echo -e "\n=== Checking Celery configurations ==="
fd -e py -e ini -e conf -e cfg | grep -i "celery"

# Search for queue configurations in Python files
echo -e "\n=== Checking queue configurations ==="
rg "queue.*=" "\.py$"

Length of output: 770


Script:

#!/bin/bash
# Check the content of newsletter views and tasks
echo "=== Newsletter Views Content ==="
cat server/apps/newsletter/views.py

echo -e "\n=== Celery Configuration Files ==="
echo "--- core/celery.py ---"
cat server/core/celery.py
echo -e "\n--- core/config/celery_config.py ---"
cat server/core/config/celery_config.py

Length of output: 3677

celery -A core beat -l info # to run celery beat
```

Expand All @@ -235,7 +229,6 @@ Before running the application, ensure you have the following:
| GET | articles/<uuid:pk> | Retrieve an article |
| PATCH | articles/<uuid:pk> | Update an article |
| DELETE | articles/<uuid:pk> | Delete an article |
| POST | newsletter/subscribe/ | Subscribe to newsletter |

7. Client Testing: `http://localhost:4321`

Expand Down
9 changes: 1 addition & 8 deletions server/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,7 @@ CLOUDINARY_CLOUD_NAME=xxxx
CLOUDINARY_API_KEY=123456789
CLOUDINARY_API_SECRET=xxxxxxxxxxxxx


# Beehiiv Configuration
BEEHIIV_ENABLED=False
BEEHIIV_API_KEY=xxxxxxxxxxxxx
BEEHIIV_PUBLICATION_ID=xxxxxxxxxxxxx
BEEHIIV_SYNC_BATCH_SIZE=50

# Django smtp
EMAIL_HOST = 'smtp.gmail.com' # Example using Gmail
EMAIL_HOST_USER = 'enter your email'
EMAIL_HOST_PASSWORD = 'enter password' #for Gmail, generate app password
EMAIL_HOST_PASSWORD = 'enter password' #for Gmail, generate app password
155 changes: 0 additions & 155 deletions server/apps/newsletter/beehiiv.py

This file was deleted.

This file was deleted.

3 changes: 0 additions & 3 deletions server/apps/newsletter/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ class Subscriber(BaseModel):
email = models.EmailField(unique=True)
is_active = models.BooleanField(default=True)
subscribed_at = models.DateTimeField(auto_now_add=True)
beehiiv_id = models.CharField(max_length=255, blank=True, null=True)
synced_to_beehiiv = models.BooleanField(default=False)
sync_error = models.TextField(blank=True, null=True)

def __str__(self):
return self.email
Expand Down
66 changes: 4 additions & 62 deletions server/apps/newsletter/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
from .models import Newsletter, Subscriber
from django.utils import timezone
from django.utils.html import format_html
import logging
from .beehiiv import BeehiivClient

logger = logging.getLogger(__name__)

@shared_task
def send_newsletter_via_email():
Expand All @@ -20,9 +16,10 @@ def send_newsletter_via_email():

for subscriber in subscribers:
try:

unsubscribe_link = format_html(
'{}/newsletter/unsubscribe/{}/',
settings.SITE_URL,
settings.SITE_URL, # Ensure this is set in your settings, e.g., 'http://127.0.0.1:8000'
subscriber.email
)

Expand All @@ -37,7 +34,7 @@ def send_newsletter_via_email():
)

except Exception as e:
logger.error(f"Error sending email to {subscriber.email}: {e}")
print(f"Error sending email to {subscriber.email}: {e}")

# Mark newsletter as sent
newsletter.is_sent = True
Expand All @@ -46,59 +43,4 @@ def send_newsletter_via_email():

# Return a success message
subscriber_count = Subscriber.objects.filter(is_active=True).count()
logger.info(f'Newsletter sent to {subscriber_count} subscribers')

@shared_task(bind=True, max_retries=3)
def sync_subscriber_to_beehiiv(self, subscriber_id):
try:
subscriber = Subscriber.objects.get(id=subscriber_id)
except Subscriber.DoesNotExist:
logger.error(f"Subscriber with id {subscriber_id} does not exist.")
return

if subscriber.synced_to_beehiiv:
return

try:
client = BeehiivClient()
response = client.create_subscriber(subscriber.email)

subscriber.beehiiv_id = response.get('id')
subscriber.synced_to_beehiiv = True
subscriber.sync_error = None
subscriber.save()

logger.info(f"Successfully synced subscriber {subscriber.email} to Beehiiv")

except Exception as e:
logger.error(f"Failed to sync subscriber {subscriber_id} to Beehiiv: {str(e)}")
if 'subscriber' in locals():
subscriber.sync_error = str(e)
subscriber.save()
retry_countdown = 60 * (2 ** self.request.retries)
raise self.retry(exc=e, countdown=retry_countdown) from e

@shared_task
def bulk_sync_subscribers_to_beehiiv():
unsynced = Subscriber.objects.filter(
is_active=True,
synced_to_beehiiv=False
)[:settings.BEEHIIV_SYNC_BATCH_SIZE]

for subscriber in unsynced:
sync_subscriber_to_beehiiv.delay(subscriber.id)

return len(unsynced)

@shared_task
def retry_failed_beehiiv_syncs():
failed_syncs = Subscriber.objects.filter(
is_active=True,
synced_to_beehiiv=False,
sync_error__isnull=False
)

for subscriber in failed_syncs:
sync_subscriber_to_beehiiv.delay(subscriber.id)

return len(failed_syncs)
print(f'Newsletter sent to {subscriber_count} subscribers')
11 changes: 9 additions & 2 deletions server/apps/research/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,20 @@
router.register(r'articles', ArticleViewSet, basename='article')

urlpatterns = [
path('', RedirectView.as_view(url='/admin/', permanent=False)),
path('', RedirectView.as_view(url='/admin/', permanent=False)), # Redirect root to admin

# custom redirect URL old slug to new slug
*redirects_urlpatterns,

path('api/', include(router.urls)),
re_path(r'^api/articles/(?P<identifier>[-\w0-9a-fA-F]+)/$', ArticleViewSet.as_view({'get': 'retrieve_by_identifier'}), name='article-detail-by-identifier'),

# Custom URL for retrieving articles by slug or UUID
re_path(r'^api/articles/(?P<identifier>[-\w0-9a-fA-F]+)/$', ArticleViewSet.as_view({'get': 'retrieve_by_identifier'}), name='article-detail-by-identifier'),

# Custom URL for retrieving articles by category
re_path(r'^api/articles/category/(?P<category>[-\w]+)/$', ArticleViewSet.as_view({'get': 'retrieve_by_category'}), name='article-list-by-category'),
# upload tinyMCE images to cloudinary
path('tinymce/upload/', tinymce_upload_image, name='tinymce_upload'),
#rss feed
path('research/rss/', LatestArticlesFeed(), name='rss_feed'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
18 changes: 3 additions & 15 deletions server/core/celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,17 @@
app.conf.beat_schedule = {
'publish-scheduled-articles-every-minute': {
'task': 'apps.research.tasks.publish_scheduled_articles',
'schedule': crontab(minute='*/1'),
'schedule': crontab(minute='*/1'), # Runs every minute
},

'send-scheduled-newsletter': {
'task': 'apps.newsletter.tasks.send_newsletter_via_email',
'schedule': crontab(minute='*/1'),
},

'sync-subscribers-to-beehiiv': {
'task': 'apps.newsletter.tasks.bulk_sync_subscribers_to_beehiiv',
'schedule': crontab(minute=0, hour='*/1'),
'options': {'queue': 'beehiiv'}
},

'retry-failed-beehiiv-syncs': {
'task': 'apps.newsletter.tasks.retry_failed_beehiiv_syncs',
'schedule': crontab(minute=30, hour='*/6'),
'options': {'queue': 'beehiiv'}
'schedule': crontab(minute='*/1'), # Runs every minute
},
}

@app.task(bind=True)
def debug_task(self):
"""Debug task to test the celery worker."""
if settings.DEBUG:
print(f'Request: {self.request!r}')
print(f'Request: {self.request!r}')
Loading
Loading