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

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

""" 

    marvin.models 

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

 

    Here we define the models we use in marvin. If this module grows too large, 

    make it a package and split it up into smaller pieces. 

 

""" 

from . import db 

from .fields import JSONField 

from .security import generate_pw_hash 

 

from datetime import datetime 

from flask import url_for, current_app 

from flask.ext.wtf import Form 

from flask.ext.principal import Permission, UserNeed 

from itsdangerous import constant_time_compare, URLSafeSerializer 

from sqlalchemy_defaults import Column 

from sqlalchemy_utils import EmailType, JSONType 

from time import time 

from wtforms_alchemy import model_form_factory 

from wtforms.fields import TextField 

from wtforms.validators import Length 

 

 

ModelForm = model_form_factory(Form) 

 

 

class Movie(db.Model): 

    """ Movies are the first thing the user will search for. 

 

    Through a movie the user can find streams related to this movie. 

    Most metadata should be fetched automatically from IMDb/TMDB. 

    """ 

    __lazy_options__ = {} 

 

    #: Identifies the movie uniquely. Do not make assumptions about the nature of this field 

    #: as it might change without notice. Is completely unrelated to other IDs found elsewhere 

    #: for the same movie, like on IMDb or similar sites. 

    id = Column(db.Integer, primary_key=True) 

    #: Namespaced identification of some resource, like a movies ID om IMDb, or it's ID on 

    #: YouTube/Vimeo, or just a URI if we don't know the site already. Format like "imdb:tt01" 

    #: or "youtube:Fq00mCqBMY8". 

    external_id = Column(db.String(200), unique=True, index=True) 

    #: The title of the movie. Note that this field is *not sufficient* to uniquely identify a 

    #: movie. Always use IDs if you need to do that. 

    title = Column(db.String(100), index=True) 

    #: What kind of movie is this? E.g. actual movie, episode, clip found on internet? 

    category = Column(db.String(20), default='movie') 

    #: Time added to database 

    datetime_added = Column(db.DateTime, auto_now=True) 

    #: Year the movies was first shown 

    year = Column(db.Integer, min=1880, max=2050) 

    #: Small cover art, 300px-ish 

    cover_img = Column(db.String(100), nullable=True) 

    #: An aggregate of number of streams available 

    number_of_streams = Column(db.Integer, default=0, nullable=False, min=0) 

    # Movie duration, in seconds 

    duration_in_s = Column(db.Integer, min=0) 

    # IMDb rating 

    imdb_rating = Column(db.Float, default=0.0, max=10.0, min=0.0, nullable=False) 

    # Number of votes on IMDb 

    number_of_imdb_votes = Column(db.Integer, default=0, min=0, nullable=False) 

    # Score from metacritic 

    metascore = Column(db.Integer, default=0, min=0, max=100, nullable=False) 

    # A measurement of how relevant this movie is, used for search ranking purposes 

    relevancy = Column(db.Float, min=0.0, default=0, nullable=False) 

 

 

    def __init__(self, **kwargs): 

        """ Create new movie object. 

 

        :param kwargs: Set object properties directly from the constructor. 

        """ 

        self.__dict__.update(kwargs) 

 

 

    def to_json(self, include_streams=True): 

        """ A dict representation of the movie that can be used for serialization. """ 

        movie = { 

            'href': url_for('moviedetailview', movie_id=self.id, _external=True), 

            'external_id': self.external_id, 

            'title': self.title, 

            'category': self.category, 

            'datetime_added': self.datetime_added, 

            'year': self.year, 

            'cover_img': self.cover_img, 

            'number_of_streams': self.number_of_streams, 

            'imdb_rating': self.imdb_rating, 

            'imdb_votes': self.number_of_imdb_votes, 

            'metascore': self.metascore, 

            'duration_in_s': self.duration_in_s, 

            '_links': { 

                'createStream': url_for('createstreamview', movie_id=self.id, _external=True), 

            }, 

        } 

        if include_streams: 

            streams = [s for s in self.streams if s.public or Permission(UserNeed(s.creator_id))] 

            movie['streams'] = [stream.to_json(include_movie=False) for stream in streams] 

        return movie 

 

 

    def update_relevancy(self): 

        """ Calculate a new relevancy rating for the movie. 

 

        The IMDb rating is weighted the most, contributing a potential 200 points out of a 325 max, 

        while the metascore ranking can contribute another 100 points, 25 points for number of 

        votes, and then the score is discounted by a factor of .99 for each year since it's relase. 

        """ 

        imdb_ranking = 20*self.imdb_rating 

        metascore_ranking = self.metascore 

        imdb_votes_ranking = min(self.number_of_imdb_votes, 25000) / 1000 

        current_year = datetime.now().year 

        years_since_release = current_year - (self.year or 1900) 

        age_discount = 0.99**(years_since_release) 

        self.relevancy = (imdb_ranking + imdb_votes_ranking + metascore_ranking)*age_discount 

 

 

class MovieForm(ModelForm): 

    """ The form used to validate new movie objects. """ 

 

    class Meta(object): 

        model = Movie 

 

 

class Stream(db.Model): 

    """ A collection of related, timecoded entries that accompanies a movie. 

 

    Entries in a stream will usually have some common theme, like annoucing new 

    actors that enter the screen, or providing references for topics mentioned 

    in a movie. 

    """ 

    __lazy_options__ = {} 

 

    #: Unique identifier for this stream. Do not make assumptions about it's format, subject to change. 

    id = Column(db.Integer, primary_key=True) 

    #: A user chosen name for the stream. Users can change this at their own discretion, do not assume to 

    #: be constant. 

    name = Column(db.String(30), nullable=False) 

    #: Short description of the stream 

    description = Column(db.String(140), nullable=False, default='') 

    #: Foreign key to a movie 

    movie_id = Column(db.Integer, db.ForeignKey('movie.id'), nullable=False) 

    #: The movie this stream is associated to. 

    movie = db.relationship('Movie', backref=db.backref('streams', lazy='dynamic')) 

    #: Foreign key to the user that created the stream 

    creator_id = Column(db.Integer, db.ForeignKey('user.id'), nullable=False) 

    #: The user that created the stream 

    creator = db.relationship('User', backref=db.backref('created_streams', lazy='dynamic')) 

    #: Whether the stream is visible public. Must be set explicitly to True by the user when he/she 

    #: considers the stream done. 

    public = Column(db.Boolean, default=False, nullable=False) 

 

 

    def __init__(self, movie=None, creator=None, **kwargs): 

        """ Create new stream. 

 

        :param movie: The movie this stream should be associated to. 

        :param creator: The user that created the stream. 

        :param kwargs: Set object properties from constructor. 

        """ 

        self.movie = movie 

        self.creator = creator 

        self.__dict__.update(kwargs) 

 

 

    def to_json(self, include_movie=True): 

        """ Get a dict representation of the stream suitable for serialization. """ 

        stream = { 

            'href': url_for('streamdetailview', stream_id=self.id, _external=True), 

            'name': self.name, 

            'published': self.public, 

            'description': self.description, 

            'author': { 

                'username': self.creator.username, 

                'href': url_for('userdetailview', user_id=self.creator.id, _external=True), 

            }, 

            '_links': { 

                'createEntry': url_for('createentryview', stream_id=self.id, _external=True), 

                'entries': url_for('streamentryview', stream_id=self.id, _external=True), 

                'publish': url_for('publishstreamview', stream_id=self.id, _external=True), 

                'unpublish': url_for('unpublishstreamview', stream_id=self.id, _external=True), 

            } 

        } 

        if include_movie: 

            stream['movie'] = { 

                'href': url_for('moviedetailview', movie_id=self.movie_id, _external=True), 

                'title': self.movie.title, 

            } 

        return stream 

 

 

class StreamForm(ModelForm): 

    """ A form used to validate new streams. """ 

 

    class Meta(object): 

        model = Stream 

        # explicitly define which fields should be considered 

        only = ( 

            'name', 

            'description', 

        ) 

 

 

class Entry(db.Model): 

    """ User-created content that appears at a given time in the movie. """ 

    __lazy_options__ = {} 

 

    #: Unique identifier 

    id = Column(db.Integer, primary_key=True) 

    #: The time this entry should appear, in ms since the beginning of the stream 

    entry_point_in_ms = Column(db.Integer, min=0, nullable=False) 

    #: The title of the entry 

    title = Column(db.String(30), nullable=False) 

    #: The type of content, e.g. 'text', 'wiki', 'imdb:actor', etc. 

    content_type = Column(db.String(20), nullable=False) 

    #: The content of the entry, as a JSON data structure 

    content = Column(JSONType, 

        info={ 

            'form_field_class': JSONField, 

            #'validators': JSONValidator(), 

        }, 

    ) 

    #: Foreign key to a stream 

    stream_id = Column(db.Integer, 

        db.ForeignKey('stream.id'), 

        nullable=False, 

    ) 

    #: The stream this entry belongs to 

    stream = db.relationship('Stream', backref=db.backref('entries', lazy='dynamic', cascade='all, delete')) 

 

 

    def __init__(self, stream=None, **kwargs): 

        """ Create new entry. 

 

        :param stream: The stream this entry should be associated to. 

        :param kwargs: Properties of the stream that can be set from the constructor. 

        """ 

        self.stream = stream 

        self.__dict__.update(kwargs) 

 

 

    def to_json(self): 

        """ Get a dict representation of the entry suitable for serialization. """ 

        return { 

            'href': url_for('entrydetailview', entry_id=self.id, _external=True), 

            'entry_point_in_ms': self.entry_point_in_ms, 

            'content_type': self.content_type, 

            'content': self.content, 

            'title': self.title, 

            'stream': { 

                'href': url_for('streamdetailview', stream_id=self.stream_id, _external=True), 

                'name': self.stream.name, 

            }, 

        } 

 

 

class EntryForm(ModelForm): 

    """ Form used to validate new entries. """ 

 

    class Meta(object): 

        model = Entry 

        # explicitly declare which fields to consider in the form 

        only = ( 

            'entry_point_in_ms', 

            'content_type', 

            'content', 

            'title', 

        ) 

 

 

class User(db.Model): 

    """ A user of the app. """ 

    __lazy_options__ = {} 

 

    #: Unique identifier for this user 

    id = Column(db.Integer, primary_key=True) 

    #: The users chosen username 

    username = Column(db.String(20), index=True, unique=True) 

    #: The user's email address 

    email = Column(EmailType, nullable=False, unique=True) 

    #: A string in the format method$salt$hash, where method for now will be scrypt:N:p:r, 

    #: with N, p and r can be chosen to be arbitrary strong on a given machine. 

    password_hash = Column(db.String(250)) 

    #: Date and time of signup 

    user_created_datetime = Column(db.DateTime, auto_now=True) 

 

 

    def __init__(self, **kwargs): 

        """ Create a new user. 

 

        :param kwargs: All properties can be set directory from the constructor. 

        """ 

        if 'password' in kwargs: 

            password = kwargs.pop('password') 

            self.password_hash = generate_pw_hash(password) 

        self.__dict__.update(kwargs) 

 

 

    def __nonzero__(self): 

        """ Same as self.is_authenticated(). """ 

        return self.is_authenticated() 

 

 

    def __bool__(self): 

        """ Same as self.is_authenticated(). """ 

        return self.is_authenticated() 

 

 

    def __str__(self): 

        return self.username 

 

 

    def is_authenticated(self): 

        """ Used to tell the difference between authenticated users and anonymous users. """ 

        # pylint: disable=no-self-use 

        return True 

 

 

    def to_json(self, include_personal_data=False): 

        """ A dict representation of the user. 

 

        :param include_personal_data: Whether to include sensitive data such as email. 

        """ 

        streams = [s for s in self.created_streams if include_personal_data or s.public] 

        data = { 

            'username': self.username, 

            'href': url_for('userdetailview', user_id=self.id), 

            'streams': [{ 

                'href': url_for('streamdetailview', stream_id=s.id, _external=True), 

                'name': s.name, 

                'published': s.public, 

                'movie': { 

                    'href': url_for('moviedetailview', movie_id=s.movie.id, _external=True), 

                    'title': s.movie.title, 

                }, 

                } for s in streams], 

        } 

 

        if include_personal_data: 

            data['email'] = self.email 

            data['signup_date'] = self.user_created_datetime 

 

        return data 

 

 

    def get_auth_token(self): 

        """ Get a auth token the user can use to authenticate agains the service. """ 

        # The data keys should be as short as possible to keep the token short 

        data = { 

            # The user's id 

            'i': self.id, 

            # Time of issue 

            't': time(), 

            # Last characters of user's hashed password, makes sure the key is automatically 

            # expired if the user changes password 

            'p': self.password_hash[-10:], 

        } 

        serializer = URLSafeSerializer(current_app.config['SECRET_KEY']) 

        return serializer.dumps(data) 

 

 

    def verify_auth_data(self, auth_data): 

        """ Verify that an auth_data is valid for this user. 

 

        In pracice this means checking that the password in the given data is still valid 

        and has not expired yet. 

        """ 

        return constant_time_compare(auth_data['p'].encode('ascii'), self.password_hash[-10:].encode('ascii')) 

 

 

class AnonymousUser(object): 

    """ Represents an anonymous user. """ 

 

    def __nonzero__(self): 

        """ Same as self.is_authenticated(). """ 

        return self.is_authenticated() 

 

 

    def __bool__(self): # python 3 

        """ Same as self.is_authenticated(). """ 

        return self.is_authenticated() 

 

 

    def __str__(self): 

        return 'Anonymous' 

 

 

    def is_authenticated(self): 

        """ Used to tell anonymous users apart from authenticated users. Always returns False. """ 

        # pylint: disable=no-self-use 

        return False 

 

 

class UserForm(ModelForm): 

    """ Form used to validate new user creation. """ 

 

    class Meta(object): 

        model = User 

        only = ( 

            'username', 

            'email', 

        ) 

 

    password = TextField(validators=[Length(min=6, max=1024)]) 

 

 

class UserLoginForm(Form): 

    """ Form used to validate user logins. """ 

 

    identifier = TextField() 

    password = TextField(validators=[Length(min=6, max=1024)])