def find_missing_short_reviews(self): short_reviews = ShortReview.objects.all() for short_review in short_reviews: try: activity = UserActivity.objects.get(activity_type = UserActivity.TYPE_SHORT_REVIEW, short_review = short_review) except UserActivity.DoesNotExist: film = Film.objects.get(pk=short_review.object.pk) activity = UserActivity(user = short_review.user, activity_type = UserActivity.TYPE_SHORT_REVIEW, short_review = short_review, film=film, created_at=short_review.created_at) activity.save()
def find_missing_short_reviews(self): short_reviews = ShortReview.objects.all() for short_review in short_reviews: try: activity = UserActivity.objects.get( activity_type=UserActivity.TYPE_SHORT_REVIEW, short_review=short_review) except UserActivity.DoesNotExist: film = Film.objects.get(pk=short_review.object.pk) activity = UserActivity( user=short_review.user, activity_type=UserActivity.TYPE_SHORT_REVIEW, short_review=short_review, film=film, created_at=short_review.created_at) activity.save()
def test_latest_checkins(self): """ Test latest_checkins widget """ # create 10 checkins activites self.initialize() for x in xrange(0, 10): act = UserActivity() act.activity_type = UserActivity.TYPE_CHECKIN act.user = self.u1 act.save() act = latest_checkins() act = act['activities'] self.failUnlessEqual(len(act), 4)
def film_rating_upgrade(self): users = User.objects.all() for user in users: ratings = Rating.objects.filter(user=user, type=1, last_rated__gte=datetime.datetime.now()-datetime.timedelta(hours=24)).exclude(rating__isnull = True) if len(ratings) != 0: rated_objects_html = "" for rating in ratings: rated_object_html = "<a href=\"/film/" + rating.parent.permalink +"\">" + unicode(rating.film) + " (" + str(rating.rating) + ")</a>," rated_objects_html = rated_objects_html + rated_object_html activity = UserActivity(user = user, activity_type = UserActivity.TYPE_RATING, rate_type=UserActivity.TYPE_FILM, rated_objects_count=len(ratings), rated_objects_html=rated_objects_html) activity.save() for rating in ratings: activity.rated_objects.add(rating)
def save_activity(self): """ Save new activity. There is no need to update it """ from film20.useractivity.models import UserActivity txt = self.from_user.username + " started_to_follow " + self.to_user.username act = UserActivity(user=self.from_user, LANG="pl", content=txt, username=self.from_user.username, activity_type=UserActivity.TYPE_FOLLOW) act.save() act = UserActivity(user=self.from_user, LANG="en", content=txt, username=self.from_user.username, activity_type=UserActivity.TYPE_FOLLOW) act.save()
def notify(self): from film20.useractivity.models import UserActivity profile = self.user.get_profile() tz = profile.timezone_id and pytz.timezone( profile.timezone_id) or pytz.utc data = { 'checkin': self, 'user': self.user, 'screening': self.screening, 'time': self.screening and self.screening.get_local_time(tz), 'film': self.film or self.screening and self.screening.film, 'channel': self.screening and self.screening.channel, 'link': self.screening and self.screening.get_absolute_url() or self.film and self.film.get_absolute_url(), 'hashtags': self.film and UserActivity.get_hashtags(self.film), 'url': self.get_absolute_url(), } if self.film: data['picture'] = self.film.get_absolute_image_url() status = self.status == self.ACTIVE and "check_in" or "check_in_cancel" send_notice([self.user], "useractivity_" + status, data) followers = set(self.user.followers.followers()) send_notice(followers, "showtimes_following_" + status, data) if self.screening: screening_users = User.objects.filter( screeningcheckin__screening=self.screening).exclude( id=self.user_id) send_notice(screening_users, "showtimes_screening_" + status, data) if self.screening.channel.type == TYPE_CINEMA: # TODO - select only user near this cinema near_cinema_users = set( User.objects.filter( selected_channels=self.screening.channel).exclude( id=self.user_id)) logger.info("near users: %r", near_cinema_users) near_followers = near_cinema_users.intersection(followers) logger.info("near followers: %r", near_followers) send_notice(near_cinema_users, "showtimes_near_cinema_" + status, data) send_notice(near_followers, "showtimes_following_near_cinema_" + status, data)
def film_rating_upgrade(self): users = User.objects.all() for user in users: ratings = Rating.objects.filter( user=user, type=1, last_rated__gte=datetime.datetime.now() - datetime.timedelta(hours=24)).exclude(rating__isnull=True) if len(ratings) != 0: rated_objects_html = "" for rating in ratings: rated_object_html = "<a href=\"/film/" + rating.parent.permalink + "\">" + unicode( rating.film) + " (" + str(rating.rating) + ")</a>," rated_objects_html = rated_objects_html + rated_object_html activity = UserActivity(user=user, activity_type=UserActivity.TYPE_RATING, rate_type=UserActivity.TYPE_FILM, rated_objects_count=len(ratings), rated_objects_html=rated_objects_html) activity.save() for rating in ratings: activity.rated_objects.add(rating)
def notify(self): from film20.useractivity.models import UserActivity profile = self.user.get_profile() tz = profile.timezone_id and pytz.timezone(profile.timezone_id) or pytz.utc data = { 'checkin':self, 'user':self.user, 'screening':self.screening, 'time':self.screening and self.screening.get_local_time(tz), 'film':self.film or self.screening and self.screening.film, 'channel':self.screening and self.screening.channel, 'link': self.screening and self.screening.get_absolute_url() or self.film and self.film.get_absolute_url(), 'hashtags': self.film and UserActivity.get_hashtags(self.film), 'url': self.get_absolute_url(), } if self.film: data['picture'] = self.film.get_absolute_image_url() status = self.status == self.ACTIVE and "check_in" or "check_in_cancel" send_notice([self.user], "useractivity_" + status, data) followers = set(self.user.followers.followers()) send_notice(followers, "showtimes_following_" + status, data) if self.screening: screening_users = User.objects.filter(screeningcheckin__screening=self.screening).exclude(id=self.user_id) send_notice(screening_users, "showtimes_screening_" + status, data) if self.screening.channel.type == TYPE_CINEMA: # TODO - select only user near this cinema near_cinema_users = set(User.objects.filter(selected_channels=self.screening.channel).exclude(id=self.user_id)) logger.info("near users: %r", near_cinema_users) near_followers = near_cinema_users.intersection(followers) logger.info("near followers: %r", near_followers) send_notice(near_cinema_users, "showtimes_near_cinema_" + status, data) send_notice(near_followers, "showtimes_following_near_cinema_" + status, data)
def save_activity(self, status=None, *args, **kwargs): """ Creates or updates an activity related to this article """ from film20.useractivity.models import UserActivity act = None # Checking if activity already exists for the given article, # if so update activity try: act = UserActivity.objects.get(post = self, user = self.user) # otherwise, create a new one except UserActivity.DoesNotExist: act = UserActivity() act.user = self.user act.activity_type = UserActivity.TYPE_POST act.post = self act.title = self.get_title() if self.lead: act.content = self.lead else: act.content = self.body rf = self.related_film.all() # special case: one film related with article - article is a review if len(rf) == 1: act.film_title = rf[0].get_title() act.film = rf[0] act.film_permalink = rf[0].permalink else: act.film = None act.film_title = None act.film_permalink = None act.username = self.user.username act.spoilers = self.spoilers act.status = self.status act.permalink = self.get_absolute_url() # legacy if self.featured_note or self.is_published: act.featured = True else: act.featured = False if self.publish is not None: act.created_at = self.publish act.save()
def test_show_article_ok(self): """ Test show_article - article exist! """ ua = UserActivity() ua.activity_type = UserActivity.TYPE_POST ua.status = UserActivity.PUBLIC_STATUS ua.title = "Lorem ipsum" ua.content = "lalala" ua.user = self.u1 ua.username = self.u1.username ua.post = Post() ua.post.title = ua.title ua.post.user = self.u1 ua.post.body = ua.content ua.post.permalink = 'testowy-artykul' ua.post.save() ua.save() response = self.client.get(ua.get_absolute_url()) print ua.get_absolute_url() self.failUnlessEqual(response.status_code, 200) self.assertEqual(response.context['activity'].content, "lalala")
def initialize(self): self.clean_data() # set up users self.u1= User.objects.create_user('michuk', '*****@*****.**', 'secret') self.u1.save() # set up film self.film = Film() self.film.id = 233 self.film.title = "Battlefield Earth II" self.film.type = Object.TYPE_FILM self.film.permalink = "battlefirld-earth-ii" self.film.release_year = 2010 self.film.save(saved_by=2) self.film.save_tags("tag1", LANG="pl", saved_by=2) # set up film self.film1 = Film() self.film1.id = 236 self.film1.title = "Battlefield Earth III" self.film1.type = Object.TYPE_FILM self.film1.permalink = "battlefirld-earth-iii" self.film1.release_year = 2011 self.film1.save(saved_by=2) self.film1.save_tags("tag2", LANG="pl", saved_by=2) # set up activities sr = UserActivity(user=self.u1, film=self.film, object=self.film, content="Lorem", activity_type=UserActivity.TYPE_SHORT_REVIEW, featured=True) sr.save() sr1 = UserActivity() sr1.user = self.u1 sr1.film = self.film1 sr1.object = self.film1 sr1.content = "ipsum" sr1.activity_type = UserActivity.TYPE_SHORT_REVIEW sr1.featured = True sr1.save() rating1 = UserActivity() rating1.activity_type = UserActivity.TYPE_RATING rating1.user = self.u1 rating1.object = self.film rating1.featured = True rating1.save() rating2 = UserActivity() rating2.activity_type = UserActivity.TYPE_RATING rating2.user = self.u1 rating2.object = self.film rating2.featured = True rating2.save()
def photo_post_save( sender, instance, created, *args, **kw ): # resize self image if created: image = Image.open( instance.image.path ) image.thumbnail( POSTER_DIMENSION, Image.ANTIALIAS ) if image.mode != "RGB": image = image.convert( "RGB" ) image.save( instance.image.path, "JPEG" ) if isinstance( instance.content_object, Film ) or \ isinstance( instance.content_object, Person ): # if accepted, save this photo as main if instance.moderation_status == ModeratedPhoto.STATUS_ACCEPTED: filename = "%s.jpg" % instance.content_object.permalink[:80] from film20.useractivity.models import UserActivity lang = getattr( instance, 'LANG', None if instance.in_all_languages else settings.LANGUAGE_CODE ) ua = UserActivity( user=instance.user, username=instance.user.username, activity_type = UserActivity.TYPE_POSTER, LANG = lang or settings.LANGUAGE_CODE, ) poster = Poster.objects.create( object=instance.content_object, image=instance.image, is_main=instance.is_main, LANG=lang ) # in movie hires image is default ... if isinstance( instance.content_object, Film ): #instance.content_object.hires_image.save( filename, instance.image ) ua.film=instance.content_object ua.film_title=instance.content_object.get_title() ua.film_permalink=instance.content_object.permalink ua.content = poster.image else: #instance.content_object.hires_image.save( filename, instance.image ) # Save: # person name in object_title # person permalink in object_slug ua.person = instance.content_object ua.object_title = str( instance.content_object ) ua.object_slug = instance.content_object.permalink ua.content = poster.image ua.save() # if moderated send notification ... if instance.moderation_status != ModeratedPhoto.STATUS_UNKNOWN \ and instance.user != instance.moderation_status_by: if notification: notification.send( [ instance.user ], "photo_moderated", { "item": instance, "status": instance.moderation_status } ) else: raise NotImplementedError
def save_activity(self): """ Save new activity """ from film20.useractivity.models import UserActivity try: # Checking if activity already exists for given externallink, if so update activity act = UserActivity.objects.get(link=self, user=self.user) act.url = self.url act.url_kind = self.url_kind act.video_thumb = getattr(self, "video_thumb", None) act.title = self.get_title() act.content = getattr(self, "excerpt", None) act.film = getattr(self, "film", None) act.film_title = self.film.get_title() act.film_permalink = self.film.permalink act.status = self.status act.permalink = self.get_absolute_url() act.save() except UserActivity.DoesNotExist: # Activity does not exist - create a new one act = UserActivity() act.user = self.user act.username = self.user.username act.activity_type = UserActivity.TYPE_LINK act.link = self act.url = self.url act.url_kind = self.url_kind act.video_thumb = getattr(self, "video_thumb", None) act.title = self.get_title() act.content = getattr(self, "excerpt", None) act.film = getattr(self, "film", None) act.film_title = self.film.get_title() act.film_permalink = self.film.permalink act.status = self.status act.permalink = self.get_absolute_url() act.save()
def save_activity(self, status=None, *args, **kwargs): """ Creates or updates an activity related to this article """ from film20.useractivity.models import UserActivity act = None # Checking if activity already exists for the given article, # if so update activity try: act = UserActivity.objects.get(post=self, user=self.user) # otherwise, create a new one except UserActivity.DoesNotExist: act = UserActivity() act.user = self.user act.activity_type = UserActivity.TYPE_POST act.post = self act.title = self.get_title() if self.lead: act.content = self.lead else: act.content = self.body rf = self.related_film.all() # special case: one film related with article - article is a review if len(rf) == 1: act.film_title = rf[0].get_title() act.film = rf[0] act.film_permalink = rf[0].permalink else: act.film = None act.film_title = None act.film_permalink = None act.username = self.user.username act.spoilers = self.spoilers act.status = self.status act.permalink = self.get_absolute_url() # legacy if self.featured_note or self.is_published: act.featured = True else: act.featured = False if self.publish is not None: act.created_at = self.publish act.save()
def save_activity(self): """ Save new activity, if activity exists updates it """ from film20.useractivity.models import UserActivity if not self.content_object: raise Exception("ThreadedComment.save_activity: content_object" "missing for new commment") try: # Checking if activity already exists for given note, if so update activity act = UserActivity.objects.get(activity_type = UserActivity.TYPE_COMMENT, comment = self, user = self.user) act.content = self.comment act.title = self.content_object.get_comment_title() act.username = self.user.username act.is_first_post = self.is_first_post act.status = self.status if settings.ENSURE_OLD_STYLE_PERMALINKS: act.permalink = self.content_object.get_absolute_url_old_style() + "#" + str(self.id) else: act.permalink = self.content_object.get_absolute_url() + "#" + str(self.id) act.slug = self.content_object.get_slug() + "#" + str(self.id) act.save() except UserActivity.DoesNotExist: # Activity does not exist - create a new one act = UserActivity() act.user = self.user act.username = self.user.username act.activity_type = UserActivity.TYPE_COMMENT act.comment = self # title for object related with comment act.title = self.content_object.get_comment_title() act.content = self.comment act.is_first_post = self.is_first_post act.status = self.status if settings.ENSURE_OLD_STYLE_PERMALINKS: act.permalink = self.content_object.get_absolute_url_old_style() + "#" + str(self.id) else: act.permalink = self.content_object.get_absolute_url() + "#" + str(self.id) act.slug = self.content_object.get_slug() + "#" + str(self.id) act.save()
def initialize(self): self.clean_data() # set up users self.u1 = User.objects.create_user('michuk', '*****@*****.**', 'secret') self.u1.save() # set up film self.film = Film() self.film.id = 233 self.film.title = "Battlefield Earth II" self.film.type = Object.TYPE_FILM self.film.permalink = "battlefirld-earth-ii" self.film.release_year = 2010 self.film.save(saved_by=2) self.film.save_tags("tag1", LANG="pl", saved_by=2) # set up film self.film1 = Film() self.film1.id = 236 self.film1.title = "Battlefield Earth III" self.film1.type = Object.TYPE_FILM self.film1.permalink = "battlefirld-earth-iii" self.film1.release_year = 2011 self.film1.save(saved_by=2) self.film1.save_tags("tag2", LANG="pl", saved_by=2) # set up activities sr = UserActivity(user=self.u1, film=self.film, object=self.film, content="Lorem", activity_type=UserActivity.TYPE_SHORT_REVIEW, featured=True) sr.save() sr1 = UserActivity() sr1.user = self.u1 sr1.film = self.film1 sr1.object = self.film1 sr1.content = "ipsum" sr1.activity_type = UserActivity.TYPE_SHORT_REVIEW sr1.featured = True sr1.save() rating1 = UserActivity() rating1.activity_type = UserActivity.TYPE_RATING rating1.user = self.u1 rating1.object = self.film rating1.featured = True rating1.save() rating2 = UserActivity() rating2.activity_type = UserActivity.TYPE_RATING rating2.user = self.u1 rating2.object = self.film rating2.featured = True rating2.save()
def save_activity(self): """ Save new activity, if activity exists updates it """ from film20.useractivity.models import UserActivity if not self.content_object: raise Exception("ThreadedComment.save_activity: content_object" "missing for new commment") try: # Checking if activity already exists for given note, if so update activity act = UserActivity.objects.get(activity_type = UserActivity.TYPE_COMMENT, comment = self, user = self.user) act.content = self.comment act.title = self.content_object.get_comment_title() act.username = self.user.username act.is_first_post = self.is_first_post act.status = self.status if settings.ENSURE_OLD_STYLE_PERMALINKS: act.permalink = self.content_object.get_absolute_url_old_style() + "#" + str(self.id) else: act.permalink = self.content_object.get_absolute_url() + "#" + str(self.id) act.slug = self.content_object.get_slug() + "#" + str(self.id) act.number_of_comments = self.number_of_comments act.save() except UserActivity.DoesNotExist: # Activity does not exist - create a new one act = UserActivity() act.user = self.user act.username = self.user.username act.activity_type = UserActivity.TYPE_COMMENT act.comment = self # title for object related with comment act.title = self.content_object.get_comment_title() act.content = self.comment act.is_first_post = self.is_first_post act.status = self.status if settings.ENSURE_OLD_STYLE_PERMALINKS: act.permalink = self.content_object.get_absolute_url_old_style() + "#" + str(self.id) else: act.permalink = self.content_object.get_absolute_url() + "#" + str(self.id) act.slug = self.content_object.get_slug() + "#" + str(self.id) act.number_of_comments = self.number_of_comments act.save()
def photo_post_save( sender, instance, created, *args, **kw ): # resize self image if created: image = Image.open( instance.image.path ) image.thumbnail( POSTER_DIMENSION, Image.ANTIALIAS ) if image.mode != "RGB": image = image.convert( "RGB" ) image.save( instance.image.path, "JPEG" ) if isinstance( instance.content_object, Film ) or \ isinstance( instance.content_object, Person ): # if accepted, save this photo as main if instance.moderation_status == ModeratedPhoto.STATUS_ACCEPTED: filename = "%s.jpg" % instance.content_object.permalink[:80] from film20.useractivity.models import UserActivity ua = UserActivity( user=instance.user, username=instance.user.username, activity_type = UserActivity.TYPE_POSTER, ) # in movie hires image is default ... if isinstance( instance.content_object, Film ): instance.content_object.hires_image.save( filename, instance.image ) ua.film=instance.content_object ua.film_title=instance.content_object.get_title() ua.film_permalink=instance.content_object.permalink ua.content = instance.content_object.hires_image else: instance.content_object.image.save( filename, instance.image ) # Save: # person name in object_title # person permalink in object_slug ua.person = instance.content_object ua.object_title = str( instance.content_object ) ua.object_slug = instance.content_object.permalink ua.content = instance.content_object.image ua.save() # if moderated send notification ... if instance.moderation_status != ModeratedPhoto.STATUS_UNKNOWN \ and instance.user != instance.moderation_status_by: if notification: notification.send( [ instance.user ], "photo_moderated", { "item": instance, "status": instance.moderation_status } ) else: raise NotImplementedError
def test_show_article_ok(self): """ Test show_article - article exist! """ ua = UserActivity() ua.activity_type = UserActivity.TYPE_POST ua.status = UserActivity.PUBLIC_STATUS ua.title = "Lorem ipsum" ua.content = "lalala" ua.user = self.u1 ua.username = self.u1.username ua.post = Post() ua.post.title = ua.title ua.post.user = self.u1 ua.post.body = ua.content ua.post.permalink = "testowy-artykul" ua.post.save() ua.save() response = self.client.get(ua.get_absolute_url()) print ua.get_absolute_url() self.failUnlessEqual(response.status_code, 200) self.assertEqual(response.context["activity"].content, "lalala")