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

78

79

80

81

82

""" 

    marvin.management 

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

 

    Configures the manager, and acts as an entry point for the manage command. 

 

""" 

# pylint: disable=invalid-name 

from . import create_app, db 

from .models import Stream, Entry, Movie 

 

from flask.ext.script import Manager 

from flask.ext.migrate import Migrate, MigrateCommand 

 

from os import path 

 

app = create_app() 

migrate = Migrate(app, db) 

manager = Manager(app) 

 

manager.add_command('db', MigrateCommand) 

 

@manager.command 

def runserver(): # pragma: no cover 

    """ Start a devserver on port 5000 """ 

    config_file = path.abspath(path.join(path.dirname(__file__), '..', 'dev_config.py')) 

    dev_app = create_app(config_file=config_file) 

    init_db() 

    dev_app.run() 

 

 

@manager.command 

def delete_streams_and_entries(wipe_movies=False): 

    """ Delete all streams and entries. 

 

    After deletion it will reset movie stream counts to 0. Can optionally also 

    delete all movies, if the --wipe_movies flag is used. """ 

 

    all_streams = Stream.query.all() 

    for stream in all_streams: 

        db.session.delete(stream) 

    all_entries = Entry.query.all() 

    for entry in all_entries: 

        db.session.delete(entry) 

 

    if wipe_movies: 

        all_movies = Movie.query.all() 

        for movie in all_movies: 

            db.session.delete(movie) 

    else: 

        # Just reset the stream counts 

        reset_movie_stream_counts() 

    db.session.commit() 

 

 

@manager.command 

def reset_movie_stream_counts(): 

    """ Recount the movie number_of_streams where it's > 0. """ 

    movies_with_stream_count = Movie.query.filter(Movie.number_of_streams > 0).all() 

    for movie in movies_with_stream_count: 

        movie.number_of_streams = len([s for s in movie.streams if s.public]) 

    db.session.commit() 

 

 

 

@manager.command 

def init_db(): 

    """ Create the database. Will not migrate if one already exists. """ 

    with app.test_request_context(): 

        db.create_all() 

 

 

def main(): # pragma: no cover 

    """ Runs the manager. 

 

    Target for setup.py entry point. 

    """ 

    manager.run() 

 

 

if __name__ == '__main__': # pragma: no cover 

    main()