Comments (10)
I'd be very interested in any developments with this enhancement. I've already built out my rest API with DRF and was having a hard time integrating it with stream. Looking forward to any updates! Thanks!
from stream-django.
I solved it by doing the following:
property tag that returns the serializer class
@property
def activity_object_serializer_class(self):
from .serializers import FooSerializer
return FooSerializer
Then used this to serialize the enriched activities. Supports nesting.
@staticmethod
def get_serialized_object_or_str(obj):
if hasattr(obj, 'activity_object_serializer_class'):
obj = obj.activity_object_serializer_class(obj).data
else:
obj = str(obj) # Could also raise exception here
return obj
def serialize_activities(self, activities):
for activity in activities:
for a in activity['activities']:
a['object'] = self.get_serialized_object_or_str(a['object'])
# The actor is always a auth.User in our case
a['actor'] = UserSerializer(a['actor']).data
return activities
Don't know if you can use any of this but i thought i should post it anyway.
Thank you for a great service!
from stream-django.
@RubenSchmidt Hey does it work for aggregated feed?
from stream-django.
@X17 I am currently only using it for aggregated feed. I am actually not sure if it works properly with the other feeds.
from stream-django.
Is there any news on this issue?
from stream-django.
Take a look at my PR: #67
from stream-django.
bumping this. I'm currently trying to power inapp notifications with getstream and django-rest-framework. Any example of this would be very appreciated. Also if I can help in anyway please let me know!
from stream-django.
Sorry, never got round to finishing this up but I did some experiments, maybe it will help?:
https://github.com/pterk/django_twitter/blob/master/stream_twitter/serializers.py
https://github.com/pterk/django_twitter/blob/master/stream_twitter/views.py#L141
from stream-django.
I've made an so question and minimal drf api to try to solve this https://stackoverflow.com/questions/56435643/django-rest-framework-getstream-notifications
https://github.com/morenoh149/django-rest-framework-getstream
if anyone has ideas please let me know @pterk @aarcro @RubenSchmidt @tbarbugli
from stream-django.
I solved it by doing the following:
property tag that returns the serializer class
@property def activity_object_serializer_class(self): from .serializers import FooSerializer return FooSerializerThen used this to serialize the enriched activities. Supports nesting.
@staticmethod def get_serialized_object_or_str(obj): if hasattr(obj, 'activity_object_serializer_class'): obj = obj.activity_object_serializer_class(obj).data else: obj = str(obj) # Could also raise exception here return obj def serialize_activities(self, activities): for activity in activities: for a in activity['activities']: a['object'] = self.get_serialized_object_or_str(a['object']) # The actor is always a auth.User in our case a['actor'] = UserSerializer(a['actor']).data return activitiesDon't know if you can use any of this but i thought i should post it anyway.
Thank you for a great service!
Your code helped me get started but I was on course to dig into flat feeds. I think how stream's code is built it works differently for flat versus aggregated. Specifically it seems in terms of how I think for aggregated it gives you "activities" whereas for flat feeds I just see EnrichedActivity with an activity_data member variable. Feel free to point out if that's not right. Wouldn't be surprised at all if you had to change your code if you still use stream since that was 2016.
I tried looking through:
For if they had given us a way to access the activity data directly to be able to serialize it and I was unable to identify if an accessor had been provided in stream_django. I don't like it but I ended up having to access the member variable directly with the activity data. Even though Tom said he thought we would have issues with DRF I know exactly what I still want to do there but the issue is getting the right data out of stream's classes.
I had their EnrichedActivity(collections.MutableMapping) object in the list returned by:
enriched_activities = enricher.enrich_activities(activities)
and did:
for activity in activities:
activity = activity.activity_data
and pulled the data back out that I wanted to serialize. If they could dig through a better solution for us for flat feeds that would be ideal. Or just resolve it in general. I'd rather not leave it the way it is. I tried using the __get__
methods but I think those are for specific keys and not entire activity items. Would need a method in stream_django that returns the activity item inside EnrichedActivity. Feel that could be the best bet so I can build my result list and serialize what I want but could need more discussion. @tbarbugli @pterk
PS. I don't want to be forced to use something like:
class ActivitySerializer(serializers.Serializer):
id = serializers.UUIDField()
foreign_id = serializers.CharField()
verb = serializers.CharField()
time = serializers.DateTimeField()
as the way that I have to serialize my objects. I should be able to have full control on how to serialize my objects irrelevant to how stream has "activities". If I want to add other layers to my serialization steps it should be extendable and allowed via stream. Right now I'm taking fields out of the activity data itself and choosing to return serialized data in the fashion I desire. Maybe I'm alone here but we'll see if others agree. Certainly seems how you build good code, especially in the Django world: loose coupling.
from stream-django.
Related Issues (20)
- Django 1.11 and Python 3+ HOT 1
- Support for Django 3.1? HOT 2
- TypeError: can only concatenate str (not "bytes") to str HOT 3
- Django 3.2 LTS support ? HOT 7
- Django 4 support HOT 2
- Cannot remove package HOT 1
- signature is invalid exception HOT 1
- Trouble removing activiity from user feed so that it is also removed from the timeline of those following. HOT 1
- i get errors when installing stream_django on windows HOT 8
- Where is the dynamic frontend? HOT 2
- Create activity optionally based on instance attribute values
- Create Reference function is inconsistent across different Stream clients
- I want to use stream, but... HOT 1
- Is there a plan to add reactions? HOT 3
- Out of nowhere Import Error HOT 1
- Crypto library HOT 2
- Support for Django v3.0 and up? HOT 4
- How is 'unfollowing' supposed to be handled? HOT 12
- Enricher doesn't work on models with inheritance?
- Problem adding reactions in models.py HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from stream-django.