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

83

84

85

86

87

88

89

90

91

92

""" 

    marvin.views.entries 

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

 

    CRUD endpoints for stream entries. 

 

""" 

# pylint: disable=no-self-use 

 

from .. import db 

from ..models import Entry, EntryForm, Stream 

from ..permissions import login_required 

 

from flask.ext.restful import Resource 

from flask.ext.principal import Permission, UserNeed 

 

class EntryDetailView(Resource): 

    """ RUD interface to entries. """ 

 

    def get(self, entry_id): 

        """ Get the entry with the given ID. """ 

        entry = Entry.query.get(entry_id) 

        return { 

            'entry': entry.to_json(), 

        } 

 

 

    @login_required 

    def put(self, entry_id): 

        """ Update the entry with the given ID. """ 

        entry = Entry.query.get_or_404(entry_id) 

        put_permission = Permission(UserNeed(entry.stream.creator_id)) 

        if put_permission.can(): 

            form = EntryForm(obj=entry) 

            if form.validate_on_submit(): 

                form.populate_obj(entry) 

                return { 

                    'msg': 'Entry updated.', 

                    'entry': entry.to_json(), 

                } 

            return { 

                'msg': 'Some attributes did not pass validation.', 

                'errors': form.errors, 

            }, 400 

        else: 

            return { 

                'msg': "Only the stream creator can edit it's entries.", 

            }, 403 

 

 

    @login_required 

    def delete(self, entry_id): 

        """ Delete the entry with the given ID. """ 

        entry = Entry.query.get(entry_id) 

        delete_permission = Permission(UserNeed(entry.stream.creator_id)) 

        if delete_permission.can(): 

            db.session.delete(entry) 

            return {'msg': 'Entry deleted.'} 

        else: 

            return { 

                'msg': 'Only the stream creator can delete entries.', 

            }, 403 

 

 

class CreateEntryView(Resource): 

    """ Create interface to entries. """ 

 

    @login_required 

    def post(self, stream_id): 

        """ Create new entry. """ 

        stream = Stream.query.get_or_404(stream_id) 

        add_entry_to_stream_permission = Permission(UserNeed(stream.creator_id)) 

        if add_entry_to_stream_permission.can(): 

            form = EntryForm() 

            if form.validate_on_submit(): 

                entry = Entry() 

                form.populate_obj(entry) 

                entry.stream = stream 

                db.session.add(entry) 

                db.session.commit() 

                return { 

                    'msg': 'Entry created.', 

                    'entry': entry.to_json(), 

                }, 201 

            return { 

                'msg': 'Some attributes did not pass validation.', 

                'errors': form.errors, 

            }, 400 

        else: 

            return { 

                'msg': 'Only the creator can add entries to streams', 

            }, 403