Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

""" 

    marvin.views.movies 

    ~~~~~~~~~~~~~~~~~~~ 

 

    Endpoints related to movies. 

 

""" 

# pylint: disable=no-self-use 

 

from ..models import Movie 

 

from flask import request 

from flask.ext.restful import Resource 

from logging import getLogger 

 

_logger = getLogger('marvin.views.movies') 

 

 

class MovieDetailView(Resource): 

    """ Read interface to movies. """ 

 

    def get(self, movie_id): 

        """ Get the movie with the given ID. """ 

        movie = Movie.query.get_or_404(movie_id) 

        return { 

            'movie': movie.to_json(), 

        } 

 

 

class AllMoviesView(Resource): 

    """ R interface to all movies. 

 

    Creation is in general not done manually by users, but rather automatically through search 

    results and the likes. Only case where you can explicitly create a movie is by entering a url, 

    so that we can look up the necessary parameters ourselves. This will be implemented later. 

    """ 

 

 

    def get(self): 

        """ Get a list of id -> movie title pairs of all movies registered. """ 

        # Import the task here since it will cause circular imports if it's done on the top 

        from marvin.tasks import external_search 

 

        search_query = request.args.get('q') 

        limit = 15 

 

        # Return results from our own db 

        if search_query: 

            search_words = search_query.split() 

            final_search_query = '%'.join(search_words) 

            movie_query = (Movie.query 

                .filter(Movie.title.ilike('%' + final_search_query + '%')) 

                .order_by(Movie.relevancy.desc()) 

                .limit(limit)) 

            _logger.info("Got search query for '%s'", search_query) 

            movies = movie_query.all() 

64            if movies: 

                _logger.info("Query for '%s' returned %d results", search_query, len(movies)) 

                # Good, we found something, return that, and look for more asynchronously 

                external_search.delay(search_query) 

            else: 

                # Crap, database is empty, let's look for some more stuff synchronously, so that we don't 

                # give any empty responses 

                _logger.info("No movies found locally for query '%s', searching external resources...", search_query) 

                external_search(search_query) 

                movies = movie_query.all() 

                _logger.info("Synchronous search for '%s' resulted in %d new movies", search_query, len(movies)) 

        else: 

            movies = (Movie.query 

                .filter(Movie.number_of_streams >= 1) 

                .order_by(Movie.relevancy.desc()) 

                .limit(limit) 

                .all()) 

 

        return { 

            'movies': [movie.to_json(include_streams=False) for movie in movies], 

        }