예제 #1
0
 def post(self, request, **kwargs):
     if len(
             Subscription._default_manager.filter(
                 Course_id=kwargs.get('pk'), User_id=request.user.id)) == 0:
         new_sub = Subscription(Course=self.get_object(), User=request.user)
         new_sub.save()
     return super().get(request)
예제 #2
0
def create_subscription(user, course):

    salt = sha_constructor(str(random.random())).hexdigest()[:5]
    username = user.username
    coursename = course.name 
    
    if isinstance(username, unicode):
        username = username.encode('utf-8')
    
    if isinstance(coursename, unicode):
        coursename = coursename.encode('utf-8')
    
    activation_key = sha_constructor(salt+username+coursename).hexdigest()

    subscription = Subscription.objects.filter(user=user, course=course)
    
    if subscription:
        subscription = subscription[0]
        if subscription.activation_key != 'DEACTIVATED':
            return subscription
        else:
            subscription.activation_key = activation_key
            subscription.save()
    else:
        subscription = Subscription(user=user, course=course, activation_key=activation_key)
        subscription.save()    

    context = dict(activation_key=activation_key, 
                   course=course,
                   domain=settings.DOMAIN,
                   expiration_days=settings.SUBSCRIPTION_ACTIVATION_DAYS)

    subject = render_to_string('subscription/activation_email_subject.txt', context)
    subject = ''.join(subject.splitlines())
    message = render_to_string('subscription/activation_email.txt', context)
    user.email_user(subject, message, settings.DEFAULT_FROM_EMAIL)

    return subscription
예제 #3
0
 def test_subscription(self):
     user = CustomUser(password="******",
                       first_name="john",
                       last_name="doe",
                       email="*****@*****.**",
                       language='1234',
                       region='NA')
     user.save()
     sub = Subscription(user_id=user.id, name="netflux", api_provider_id=8)
     sub2 = Subscription(user_id=user.id, name="prime", api_provider_id=9)
     sub3 = Subscription(user_id=user.id, name="Banana", api_provider_id=10)
     sub.save()
     sub2.save()
     sub3.save()
     self.assertEqual(sub.user.first_name, "john")
     self.assertEqual(sub.name, "netflux")
     self.assertEqual(sub.user_id, user.id)
     self.assertEqual(sub.api_provider_id, 8)
     self.assertEqual(sub2.name, "prime")
     self.assertEqual(sub2.user_id, user.id)
     self.assertEqual(sub2.api_provider_id, 9)
     self.assertEqual(sub3.name, "Banana")
     self.assertEqual(sub3.user_id, user.id)
     self.assertEqual(sub3.api_provider_id, 10)
     self.assertEqual(len(user.subscriptions.all()), 3)
 def test_director_recommendations(self):
   user = CustomUser(password="******", first_name="john", last_name="doe",
                     email="*****@*****.**", language='1234', region='NA')
   user.save()
   netflix = Subscription(user_id=user.id, name="netflix", api_provider_id=8)
   netflix.save()
   amazon_prime = Subscription(
       user_id=user.id, name="Amazon Prime", api_provider_id=9)
   amazon_prime.save()
   disney_plus = Subscription(
       user_id=user.id, name="Disney+", api_provider_id=337)
   disney_plus.save()
   itunes = Subscription(user_id=user.id, name="Itunes", api_provider_id=2)
   itunes.save()
   thumb = Thumb(title='The Great Escape', api_movie_id=2,
                 api_director_id=341, up=True, user=user)
   thumb.save()
   thumb2 = Thumb(title='James Bond', api_movie_id=3,
                  api_director_id=341, up=True, user=user)
   thumb2.save()
   thumb3 = Thumb(title='The Red House', api_movie_id=4,
                  api_director_id=489, up=True, user=user)
   thumb3.save()
   thumb4 = Thumb(title='Gladiator', api_movie_id=5,
                  api_director_id=489, up=True, user=user)
   thumb4.save()
   thumb5 = Thumb(title='House', api_movie_id=6,
                  api_director_id=341, up=True, user=user)
   thumb5.save()
   self.assertEqual(views.recommendations_by_director(user), 341)
 def test_recommendations(self):
   user = CustomUser(password="******", first_name="john", last_name="doe",
                   email="*****@*****.**", language='en-US', region='US')
   user.save()
   netflix = Subscription(user_id=user.id, name="netflix", api_provider_id=8)
   netflix.save()
   amazon_prime = Subscription(user_id=user.id, name="Amazon Prime", api_provider_id=9)
   amazon_prime.save()
   disney_plus = Subscription(user_id=user.id, name="Disney+", api_provider_id=337)
   disney_plus.save()
   itunes = Subscription(user_id=user.id, name="Itunes", api_provider_id=2)
   itunes.save()
   c = Client()
   response = c.get(f"/v1/movies?user={user.id}")
   self.assertEqual(len(response.json()['results']), 20)
   self.assertEqual(type(response.json()['results'][0]['title']), str)
   self.assertEqual(type(response.json()['results'][0]['title']), str)
   self.assertEqual(type(response.json()['results'][0]['genre_ids']), list)
   self.assertEqual(type(response.json()['results'][0]['genre_ids'][0]), int)
   self.assertEqual(type(response.json()['results'][0]['id']), int)
   self.assertEqual(type(response.json()['results'][0]['genre_ids'][0]), int)
   self.assertEqual(type(response.json()['results'][0]['poster_path']), str)
   self.assertEqual(type(response.json()['results'][0]['backdrop_path']), str)
   self.assertEqual(type(response.json()['results'][0]['release_date']), str)
   self.assertEqual(type(response.json()['results'][0]['vote_average']), float)
 def test_services_by_user(self):
   user = CustomUser(password="******", first_name="john", last_name="doe",
                     email="*****@*****.**", language='1234', region='NA')
   user.save()
   netflix = Subscription(user_id=user.id, name="netflix", api_provider_id=8)
   netflix.save()
   amazon_prime = Subscription(
       user_id=user.id, name="Amazon Prime", api_provider_id=9)
   amazon_prime.save()
   disney_plus = Subscription(
       user_id=user.id, name="Disney+", api_provider_id=337)
   disney_plus.save()
   itunes = Subscription(user_id=user.id, name="Itunes", api_provider_id=2)
   itunes.save()
   self.assertEqual(views.user_subscriptions(user), '2|8|9|337')