def get_queryset(self): MarketAccount = get_model('social', 'marketaccount') BannedUser = get_model('social', 'banneduser') accounts = MarketAccount.objects.all().values_list('handle', flat=True) banned_users = BannedUser.objects.all().values_list('handle', flat=True) account_min_date = '2014-01-13 00:00:00' return super(SocialPostManager, self).get_queryset().exclude(handle__in=accounts)\ .exclude(deleted=True).exclude(content__contains='RT ').exclude(entry_allowed=False)\ .exclude(handle__in=banned_users).filter(Q(user_joined__isnull=True)|Q(user_joined__lte=account_min_date))
def ban_user(self): post_pk = self.request.GET['post_pk'] tweet = get_model('social', 'socialpost').everything.get(pk=post_pk) hellban = get_model('social', 'banneduser')(handle=tweet.handle) try: hellban.save() except IntegrityError: return "Already banned" return "OK"
def handle(self, *args, **kwargs): """ Import for the first stored search term. """ terms = get_model('social', 'searchterm').objects.filter(active=True) for term in terms: for account in self.accounts: self.stdout.write("\nImporting %s posts on %s for account %s" % (term.term, account.type, account.handle)) api = SocialSearchFacade(account) search = api.search(term.term, count=kwargs.get('post_count')) for post in search: obj = get_model('social', 'socialpost')( account=account, content=post.content, created_at=post.created_at, followers=post.followers, handle=post.handle, image_url=post.image_url, post_url=post.post_url, uid=post.uid, user_joined=post.user_joined, profile_image=post.profile_image, post_source=post.post_source, raw_object=pickle.dumps(post._obj), search_term=term, ) try: post = get_model( 'social', 'socialpost').everything.get(uid=obj.uid) except ObjectDoesNotExist: obj.save() self.stdout.write("Added %s (%d %s)" % (obj.uid, obj.id, obj.handle)) entry_count = obj.entry_count if entry_count > settings.MAX_ENTRIES: self.disable( obj, reason='Already entered max times (%d)' % entry_count) continue else: self.stdout.write("Post already exists %s (%d %s)" % (post.uid, post.id, post.handle))
def get_queryset(self): queryset = get_model('social', 'socialpost').objects.all() user = self.request.QUERY_PARAMS.get('user', None) if user is not None: try: # If we have a user then we need to look up what accounts they are associated # with and then filter on all those (it's M2M) tracked_term = get_model('social', 'trackedterms').objects.get(user__username=user) queryset = queryset.filter(search_term__in=tracked_term.terms.values_list('pk', flat=True)) except get_model('social', 'trackedterms').DoesNotExist: # If we can't find the user just carry on pass return queryset
def entry_count(self): """ Return how many times this handle has entered - we're only counting when they tweeted with an image - otherwise we'll exclude just tagged tweets - this isn't perfect - they might tweet something else and attach an image but should be ok """ return get_model('social','socialpost').everything.filter(handle=self.handle).exclude(image_url=None).exclude(uid=self.uid).count()
def handle(self, *args, **kwargs): """ Import for the first stored search term. """ terms = get_model('social', 'searchterm').objects.filter(active=True) for term in terms: for account in self.accounts: self.stdout.write("\nImporting %s posts on %s for account %s" % (term.term, account.type, account.handle)) api = SocialSearchFacade(account) search = api.search(term.term, count=kwargs.get('post_count')) for post in search: obj = get_model('social', 'socialpost')( account=account, content=post.content, created_at=post.created_at, followers=post.followers, handle=post.handle, image_url=post.image_url, post_url=post.post_url, uid=post.uid, user_joined=post.user_joined, profile_image=post.profile_image, post_source=post.post_source, raw_object=pickle.dumps(post._obj), search_term=term, ) try: post = get_model('social', 'socialpost').everything.get(uid=obj.uid) except ObjectDoesNotExist: obj.save() self.stdout.write("Added %s (%d %s)" % (obj.uid, obj.id, obj.handle)) entry_count = obj.entry_count if entry_count > settings.MAX_ENTRIES: self.disable(obj, reason='Already entered max times (%d)' % entry_count) continue else: self.stdout.write("Post already exists %s (%d %s)" % (post.uid, post.id, post.handle))
def entry_count(self): """ Return how many times this handle has entered - we're only counting when they tweeted with an image - otherwise we'll exclude just tagged tweets - this isn't perfect - they might tweet something else and attach an image but should be ok """ return get_model( 'social', 'socialpost').everything.filter(handle=self.handle).exclude( image_url=None).exclude(uid=self.uid).count()
class PaginatedImagePostFeedView(generics.ListAPIView): queryset = get_model('social', 'socialpost').objects.all() serializer_class = PostSerializer pagination_serializer_class = PaginatedPostSerializer filter_backends = (HasImageFilterBackend, OldSchoolRetweet) def get_queryset(self): queryset = get_model('social', 'socialpost').objects.all() user = self.request.QUERY_PARAMS.get('user', None) if user is not None: try: # If we have a user then we need to look up what accounts they are associated # with and then filter on all those (it's M2M) tracked_term = get_model('social', 'trackedterms').objects.get(user__username=user) queryset = queryset.filter(search_term__in=tracked_term.terms.values_list('pk', flat=True)) except get_model('social', 'trackedterms').DoesNotExist: # If we can't find the user just carry on pass return queryset
def send_tweet(self): tweet_pk = self.request.GET['tweet_pk'] msg = self.request.GET['msg'] tweet = get_model('social', 'socialpost').objects.get(pk=tweet_pk) # Reverse the quoting and get the unicode back msg = urllib.unquote(msg) try: api = twitter.Api( consumer_key=tweet.account.consumer_key, consumer_secret=tweet.account.consumer_secret, access_token_key=tweet.account.access_token_key, access_token_secret=tweet.account.access_token_secret, ) # If we have an included media file then attach and send that # otherwise we post a regular Update instead - that is we're # not going by the message content! if tweet.photoshop: status = api.PostMedia(u'{!s}'.format(msg), tweet.photoshop.file.name, in_reply_to_status_id=tweet.uid) else: status = api.PostUpdate(u'{!s}'.format(msg), in_reply_to_status_id=tweet.uid) # Update the tweet itself now tweet.tweeted = True tweet.tweet_id = status.id tweet.sent_tweet = msg tweet.tweeted_by = self.request.user tweet.tweeted_at = datetime.now() tweet.save() except twitter.TwitterError: status = None return status
""") messages.short_description = 'Post control' def save_model(self, request, obj, form, change): obj.save() def get_actions(self, request): actions = super(SocialAdmin, self).get_actions(request) if 'delete_selected' in actions: del actions['delete_selected'] return actions def get_queryset(self, request): if request.user.is_superuser: return self.model.everything.get_query_set() return super(SocialAdmin, self).get_queryset(request) class MessageAdmin(BaseAdmin): list_display = ('account', 'type', 'copy') list_filter = ('account', 'type') admin.site.register(get_model('social', 'socialpost'), SocialAdmin) admin.site.register(get_model('social', 'searchterm'), BaseAdmin) admin.site.register(get_model('social', 'banneduser'), BaseAdmin) admin.site.register(get_model('social', 'message'), MessageAdmin) admin.site.register(get_model('social', 'marketaccount'), BaseAdmin) admin.site.register(get_model('social', 'trackedterms'), BaseAdmin)
class MarketAccountViewSet(viewsets.ModelViewSet): queryset = get_model('social', 'marketaccount').objects.all() serializer_class = MarketAccountSerializer
class Meta: model = get_model('social', 'message')
def __init__(self): super(Command, self).__init__() self.accounts = get_model('social', 'marketaccount').objects.all()
class Meta: model = get_model('social', 'socialpost') fields = ('image_url', 'post_url', 'handle', 'content', 'post_source', 'created_at')
class Meta: model = get_model('social', 'marketaccount')
class MessageViewSet(viewsets.ModelViewSet): queryset = get_model('social', 'message').objects.all() serializer_class = MessageSerializer filter_fields = ('type', 'account',)
messages.short_description = 'Post control' def save_model(self, request, obj, form, change): obj.save() def get_actions(self, request): actions = super(SocialAdmin, self).get_actions(request) if 'delete_selected' in actions: del actions['delete_selected'] return actions def get_queryset(self, request): if request.user.is_superuser: return self.model.everything.get_query_set() return super(SocialAdmin, self).get_queryset(request) class MessageAdmin(BaseAdmin): list_display = ('account', 'type', 'copy') list_filter = ('account', 'type') admin.site.register(get_model('social', 'socialpost'), SocialAdmin) admin.site.register(get_model('social', 'searchterm'), BaseAdmin) admin.site.register(get_model('social', 'banneduser'), BaseAdmin) admin.site.register(get_model('social', 'message'), MessageAdmin) admin.site.register(get_model('social', 'marketaccount'), BaseAdmin) admin.site.register(get_model('social', 'trackedterms'), BaseAdmin)