ActivityPubService - TypeError `None` on migration
Heyhey everyone, hope all is good
I just wanted to share with you an error I got today within djangoldp/activities/services.py
Contexte
I was fixing a legacy error on risefor, using an M2M field instead of an FK, which was starting to generate issues
To do so i created a migration, i'll pass all the details but it's this operation that was generating the underlying issue
for email in ElectedOfficialContact.objects.all():
email.linkedActionFK = email.linkedAction.all().first() # Or whatever criterea you want
email.save()
Linked Model
class ElectedOfficialContact(Model):
author = models.ForeignKey(User, blank=True, null=True, on_delete=models.SET_NULL)
created_at = models.DateTimeField(auto_now_add=True)
subject = models.CharField(max_length=255,verbose_name=u"Sujet de l'email envoyé aux élu.e.s")
content = models.TextField(verbose_name=u"Contenu de l'email envoyé aux élu.e.s")
userName = models.TextField(verbose_name=u"Name of citizen")
userEmail = models.CharField(max_length=255, verbose_name=u"User email")
representativeEmails = models.CharField(max_length=1000, verbose_name=u"Adresses ciblés")
optin1 = models.BooleanField(default=True,verbose_name=u"Accept saving email for sending")
optin2 = models.BooleanField(default=False,blank=True, null=True,verbose_name=u"Sign up for Risefor Notifications")
linkedAction = models.ManyToManyField(ActionGroup,blank=True,related_name="actionEmailSentRelated",verbose_name=u"Action liée")
Important detail : in the production our `author` field is generally None
, as users aren't connected (another legacy issue, but it's another subject).
I'm pretty sure that's what was generating the error.
Issue
When Migrating, the 2 following errors came (2nd popped up when i fixed the first)
Number 1
ActivityPubService.send_update_activity(actor, obj, target) File "/var/www/risefor/env/lib/python3.8/site-packages/djangoldp/activities/services.py", line 483, in send_update_activity summary = str(obj['@id']) + " was updated" TypeError: 'NoneType' object is not subscriptable
"Fix"
Doing this worked for me, i don't know all the ins and outs so it's probably not a good fix, but still sharing it just in case
try:
summary = str(obj['@id']) + " was updated"
activity = cls.build_activity(actor, obj, activity_type='Update', summary=summary)
ActivityQueueService.send_activity(inbox,activity)
except TypeError as e:
print('error with type',e,actor,obj)
activity = cls.build_activity(actor, obj, activity_type='Update', summary='None, makes TypeError')
ActivityQueueService.send_activity(inbox,activity)
Number 2
Same error but on ActivityPubService.save_followers_for_targets(external_urlids, obj['@id']
line 599
"fix"
try:
# create Followers to update external resources of changes in future
ActivityPubService.save_followers_for_targets(external_urlids, obj['@id'])
except TypeError as e:
print('error with type',e,actor,obj)
ActivityPubService.save_followers_for_targets(external_urlids, 'None')
After making the changes, i re-migrated, and appart from my prints telling me there was an empty, all went well.
Here is an example of one of the prints, just in case
error with type 'NoneType' object is not subscriptable {'type': 'Service', 'name': 'Backlinks Service'} None
I figured it could be good to share this, in case you see it elsewhere