def notify_subscribers(mentions): # type: (List[str]) -> None recipients = list( instance.topic.subscribers.exclude(pk__in=list( set([instance.user.pk] + [ x.pk for x in MentionsService. get_mentioned_users_with_notification_enabled( mentions, 'new_forum_post_mention') ])))) if recipients: push_notification( recipients, instance.user, 'new_forum_reply', { 'user': instance.user.userprofile.get_display_name(), 'user_url': settings.BASE_URL + reverse_url( 'user_page', kwargs={'username': instance.user}), 'post_url': build_notification_url( settings.BASE_URL + instance.get_absolute_url(), instance.user), 'topic_url': build_notification_url( settings.BASE_URL + instance.topic.get_absolute_url(), instance.user), 'topic_name': instance.topic.name, 'unsubscribe_url': build_notification_url( settings.BASE_URL + reverse_url('pybb:delete_subscription', args=[instance.topic.id]), instance.user) })
def group_post_save(sender, instance, created, **kwargs): if created and instance.creator is not None: instance.members.add(instance.creator) if instance.moderated: instance.moderators.add(instance.creator) if instance.public: followers = [ x.user for x in ToggleProperty.objects.toggleproperties_for_object( "follow", UserProfile.objects.get(user__pk=instance.creator.pk).user) ] push_notification( followers, instance.creator, 'new_public_group_created', { 'creator': instance.creator.userprofile.get_display_name(), 'group_name': instance.name, 'url': settings.BASE_URL + reverse_url('group_detail', args=(instance.pk, )), }) add_story(instance.creator, verb='VERB_CREATED_PUBLIC_GROUP', action_object=instance)
def notify_mentioned(mentions): # type: (List[str]) -> None for username in mentions: user = get_object_or_None(User, username=username) if user is None: try: profile = get_object_or_None(UserProfile, real_name=username) if profile: user = profile.user except MultipleObjectsReturned: user = None if user: push_notification( [user], instance.user, 'new_forum_post_mention', { 'url': build_notification_url( settings.BASE_URL + instance.get_absolute_url(), instance.user), 'user': instance.user.userprofile.get_display_name(), 'user_url': settings.BASE_URL + reverse_url( 'user_page', kwargs={'username': instance.user}), 'post': instance.topic.name, })
def save_to_file(request): query = request.GET.get('query') FILE = 'STXnext/json_books.json' URL = f'https://www.googleapis.com/books/v1/volumes?q={query}' books = get_from_google_api(URL) with open(FILE, 'w') as f: f.write(json.dumps(books, indent=4)) messages.success(request, f"books with query{query} added to file ", fail_silently=True) return redirect(reverse_url('home'))
def forum_topic_post_save(sender, instance, created, **kwargs): if created and hasattr(instance.forum, 'group'): group = instance.forum.group if instance.on_moderation: recipients = group.moderators.all() else: recipients = group.members.all() recipients = [x for x in recipients if x != instance.user] push_notification( recipients, instance.user, 'new_topic_in_group', { 'user_url': build_notification_url(settings.BASE_URL + reverse_url( 'user_page', kwargs={'username': instance.user})), 'user': instance.user.userprofile.get_display_name(), 'url': build_notification_url( settings.BASE_URL + instance.get_absolute_url(), instance.user), 'group_url': build_notification_url( settings.BASE_URL + reverse_url('group_detail', kwargs={'pk': group.pk}), instance.user), 'group_name': group.name, 'topic_title': instance.name, }, ) cache_key = make_template_fragment_key( 'home_page_latest_from_forums', (instance.user.pk, instance.user.userprofile.language)) cache.delete(cache_key)
def forum_topic_pre_save(sender, instance, **kwargs): if not hasattr(instance.forum, 'group'): return try: topic = sender.objects.get(pk=instance.pk) except sender.DoesNotExist: pass else: if topic.on_moderation == True and instance.on_moderation == False: # This topic is being approved group = instance.forum.group push_notification( [x for x in group.members.all() if x != instance.user], instance.user, 'new_topic_in_group', { 'user_url': build_notification_url(settings.BASE_URL + reverse_url( 'user_page', kwargs={'username': instance.user})), 'user': instance.user.userprofile.get_display_name(), 'url': build_notification_url( settings.BASE_URL + instance.get_absolute_url(), instance.user), 'group_url': build_notification_url( reverse_url('group_detail', kwargs={'pk': group.pk}), instance.user), 'group_name': group.name, 'topic_title': instance.name, }, )
def nested_comment_post_save(sender, instance, created, **kwargs): if created: mentions = MentionsService.get_mentions(instance.text) if hasattr(instance.content_object, "updated"): # This will trigger the auto_now fields in the content_object # We do it only if created, because the content_object needs to # only be updated if the number of comments changes. save_kwargs = {} if issubclass(type(instance.content_object), SafeDeleteModel): save_kwargs['keep_deleted'] = True instance.content_object.save(**save_kwargs) if instance.pending_moderation: CommentNotificationsService( instance).send_moderation_required_notification() else: CommentNotificationsService(instance).send_notifications() else: mentions = cache.get( "user.%d.comment_pre_save_mentions" % instance.author.pk, []) if not instance.pending_moderation: for username in mentions: user = get_object_or_None(User, username=username) if not user: try: profile = get_object_or_None(UserProfile, real_name=username) if profile: user = profile.user except MultipleObjectsReturned: user = None if user: push_notification( [user], instance.author, 'new_comment_mention', { 'url': build_notification_url( settings.BASE_URL + instance.get_absolute_url(), instance.author), 'user': instance.author.userprofile.get_display_name(), 'user_url': settings.BASE_URL + reverse_url( 'user_page', kwargs={'username': instance.author}), })
def home_page(request): context = {} FILE = 'STXnext/json_books.json' ids = Book.objects.order_by('id') if len(ids) > 0: id_first = ids[0].id last = ids.reverse()[0].id context['count_books'] = [id for id in range(id_first, last + 1)] # flush database if there is more than 200 records if last - id_first > 200: Book.objects.all().delete() Author.objects.all().delete() messages.warning(request, "More than 200 records. Database reset") return redirect(reverse_url('home')) not_empty = os.stat(FILE).st_size != 0 context['not_empty'] = not_empty return render(request, 'STXnext/home_page.html', context)
def group_members_changed(sender, instance, **kwargs): action = kwargs['action'] pk_set = kwargs['pk_set'] group_sync_map = { 'IOTD Submitters': ['iotd_submitters', 'content_moderators', 'iotd_staff'], 'IOTD Reviewers': ['iotd_reviewers', 'content_moderators', 'iotd_staff'], 'IOTD Judges': ['iotd_judges', 'content_moderators', 'iotd_staff'], } if instance.name in list(group_sync_map.keys()): for django_group in group_sync_map[instance.name]: DjangoGroup.objects.get_or_create(name=django_group) django_groups = DjangoGroup.objects.filter( name__in=group_sync_map[instance.name]) try: iotd_staff_group = Group.objects.get(name='IOTD Staff') except Group.DoesNotExist: iotd_staff_group = None if action == 'post_add': users = [ profile.user for profile in UserProfile.objects.filter(user__pk__in=pk_set) ] instance.save() # trigger date_updated update if instance.public: for pk in pk_set: user = UserProfile.objects.get(user__pk=pk).user if user != instance.owner: followers = [ x.user for x in ToggleProperty.objects. toggleproperties_for_object("follow", user) ] push_notification( followers + [instance.owner], user, 'user_joined_public_group', { 'user': user.userprofile.get_display_name(), 'user_url': build_notification_url( settings.BASE_URL + reverse_url( 'user_page', kwargs={'username': user.username}), user), 'group_name': instance.name, 'url': build_notification_url( settings.BASE_URL + reverse_url( 'group_detail', args=(instance.pk, )), user), }) add_story(user, verb='VERB_JOINED_GROUP', action_object=instance) if instance.autosubmission: images = Image.objects_including_wip.filter(user__pk__in=pk_set) for image in images: instance.images.add(image) # Sync IOTD AstroBin groups with django groups if instance.name in list(group_sync_map.keys()): for django_group in django_groups: django_group.user_set.add(*list(users)) if iotd_staff_group: for user in users: iotd_staff_group.members.add(user) elif action == 'post_remove': users = [ profile.user for profile in UserProfile.objects.filter(user__pk__in=pk_set) ] images = Image.objects_including_wip.filter(user__pk__in=pk_set) for image in images: instance.images.remove(image) if instance.forum and not instance.public: topics = Topic.objects.filter(forum=instance.forum) for topic in topics: topic.subscribers.remove(*User.objects.filter( pk__in=kwargs['pk_set'])) # Sync IOTD AstroBin groups with django groups if instance.name in list(group_sync_map.keys()): all_members = [] all_members_chain = chain([ x.members.all() for x in Group.objects \ .filter(name__in=list(group_sync_map.keys())) \ .exclude(name=instance.name) ]) for chain_item in all_members_chain: all_members += chain_item for user in [x for x in users if x not in all_members]: for django_group in django_groups: django_group.user_set.remove(user) if iotd_staff_group: iotd_staff_group.members.remove(user) elif action == 'pre_clear': # Sync IOTD AstroBin groups with django groups users = instance.members.all() if instance.name in list(group_sync_map.keys()): all_members = [] all_members_chain = chain([ x.members.all() for x in Group.objects \ .filter(name__in=list(group_sync_map.keys())) \ .exclude(name=instance.name) ]) for chain_item in all_members_chain: all_members += chain_item for user in [x for x in users if x not in all_members]: for django_group in django_groups: django_group.user_set.remove(user) if iotd_staff_group: iotd_staff_group.members.remove(user) elif action == 'post_clear': instance.images.clear()
def toggleproperty_post_save(sender, instance, created, **kwargs): if hasattr(instance.content_object, "updated"): # This will trigger the auto_now fields in the content_object kwargs = {} if issubclass(type(instance.content_object), SafeDeleteModel): kwargs['keep_deleted'] = True instance.content_object.save(**kwargs) if created: verb = None if instance.property_type in ("like", "bookmark"): if instance.content_type == ContentType.objects.get_for_model( Image): image = instance.content_type.get_object_for_this_type( id=instance.object_id) Image.all_objects.filter(pk=instance.content_object.pk).update( updated=timezone.now()) if image.is_wip: return if instance.property_type == "like": verb = 'VERB_LIKED_IMAGE' elif instance.property_type == "bookmark": verb = 'VERB_BOOKMARKED_IMAGE' else: return push_notification( [instance.content_object.user], instance.user, 'new_' + instance.property_type, { 'url': build_notification_url( settings.BASE_URL + instance.content_object.get_absolute_url(), instance.user), 'title': instance.content_object.title, 'user': instance.user.userprofile.get_display_name(), 'user_url': settings.BASE_URL + reverse_url( 'user_page', kwargs={'username': instance.user.username}), }) elif instance.content_type == ContentType.objects.get_for_model( NestedComment): push_notification( [instance.content_object.author], instance.user, 'new_comment_like', { 'url': build_notification_url( settings.BASE_URL + instance.content_object.get_absolute_url(), instance.user), 'user': instance.user.userprofile.get_display_name(), 'user_url': settings.BASE_URL + reverse_url( 'user_page', kwargs={'username': instance.user.username}), 'comment': instance.content_object.text }) UserProfile.all_objects.filter( user=instance.content_object.author).update( updated=timezone.now()) elif instance.content_type == ContentType.objects.get_for_model( Post): push_notification( [instance.content_object.user], instance.user, 'new_forum_post_like', { 'url': build_notification_url( settings.BASE_URL + instance.content_object.get_absolute_url(), instance.user), 'user': instance.user.userprofile.get_display_name(), 'user_url': settings.BASE_URL + reverse_url( 'user_page', kwargs={'username': instance.user.username}), 'post': instance.content_object.topic.name }) UserProfile.all_objects.filter( user=instance.content_object.user).update( updated=timezone.now()) if verb is not None: add_story(instance.user, verb=verb, action_object=instance.content_object) elif instance.property_type == "follow": user_ct = ContentType.objects.get_for_model(User) if instance.content_type == user_ct: followed_user = user_ct.get_object_for_this_type( pk=instance.object_id) push_notification( [followed_user], instance.user, 'new_follower', { 'object': instance.user.userprofile.get_display_name(), 'object_url': build_notification_url( settings.BASE_URL + reverse_url( 'user_page', kwargs={'username': instance.user.username}), instance.user), })
def image_post_save(sender, instance, created, **kwargs): # type: (object, Image, bool, object) -> None if created: instance.user.userprofile.premium_counter += 1 instance.user.userprofile.save(keep_deleted=True) if not instance.is_wip: if not instance.skip_notifications: push_notification_for_new_image.apply_async(args=( instance.user.pk, instance.pk, )) if instance.moderator_decision == 1: add_story(instance.user, verb='VERB_UPLOADED_IMAGE', action_object=instance) if Image.all_objects.filter(user=instance.user).count() == 1: push_notification( [instance.user], None, 'congratulations_for_your_first_image', { 'BASE_URL': settings.BASE_URL, 'PREMIUM_MAX_IMAGES_FREE': settings.PREMIUM_MAX_IMAGES_FREE, 'url': reverse_url('image_detail', args=(instance.get_id(), )) }) mentions = MentionsService.get_mentions(instance.description_bbcode) else: mentions = cache.get("image.%d.image_pre_save_mentions" % instance.pk, []) for username in mentions: user = get_object_or_None(User, username=username) if not user: try: profile = get_object_or_None(UserProfile, real_name=username) if profile: user = profile.user except MultipleObjectsReturned: user = None if user and user != instance.user: thumb = instance.thumbnail_raw('gallery', None, sync=True) push_notification( [user], instance.user, 'new_image_description_mention', { 'image': instance, 'image_thumbnail': thumb.url if thumb else None, 'url': build_notification_url( settings.BASE_URL + instance.get_absolute_url(), instance.user), 'user': instance.user.userprofile.get_display_name(), 'user_url': settings.BASE_URL + reverse_url( 'user_page', kwargs={'username': instance.user}), }) if not instance.uploader_in_progress: groups = instance.user.joined_group_set.filter(autosubmission=True) for group in groups: if instance.is_wip: group.images.remove(instance) else: group.images.add(instance) if instance.user.userprofile.updated < datetime.datetime.now( ) - datetime.timedelta(minutes=5): instance.user.save() try: instance.user.userprofile.save(keep_deleted=True) except UserProfile.DoesNotExist: pass UserService(instance.user).clear_gallery_image_list_cache() if instance.user.userprofile.auto_submit_to_iotd_tp_process: IotdService.submit_to_iotd_tp_process(instance.user, instance, False)
def get_absolute_url(self): return reverse_url("ripper:article_detail", args=(self.url_no_schema, ))