1# -*- coding: UTF-8 -*-
2
3__revision__ = '$Id$'
4
5# Copyright © 2009 Piotr Ożarowski
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published byp
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU Library General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program; if not, write to the Free Software
19# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
20
21# You may use and distribute this software under the terms of the
22# GNU General Public License, version 2 or later
23
24import logging
25
26from sqlalchemy.sql import delete, select
27
28from db.tables import movies as movies_table
29from db.tables import movie_tag as movie_tag_table
30from db.tables import movie_lang as movie_lang_table
31from gutils import question
32from plugins.extensions import GriffithExtensionBase as Base
33from sql import update_whereclause
34from quick_filter import change_filter_update_whereclause
35
36log = logging.getLogger('Griffith')
37
38class GriffithExtension(Base):
39    name = 'Remover'
40    description = _('Removes all currently filtered movies')
41    author = 'Piotr Ożarowski'
42    email = 'piotr@griffith.cc'
43    version = 0.2
44    api = 1
45    enabled = True # TODO: disable it by default
46
47    toolbar_icon = 'ge_remover.png'
48
49    def toolbar_icon_clicked(self, widget, movie):
50        if question(_('Are you sure you want to remove %d movies?') % self.app.total):
51            session = self.db.Session()
52
53            # first: remove all dependend data (associated tags, languages, ...)
54            query = select([movies_table.c.movie_id])
55            # add conditions from simple filter
56            change_filter_update_whereclause(self.app, query)
57            # add conditions from adv filter
58            query = update_whereclause(query, self.app._search_conditions)
59            query = query.where(movies_table.c.loaned==False) # don't delete loaned movies
60            log.debug(query)
61            movie_ids = []
62            for movie_entry in session.execute(query):
63                movie_ids.append(movie_entry.movie_id)
64                # tags
65                query_movie_tags = delete(movie_tag_table)
66                query_movie_tags = query_movie_tags.where(movie_tag_table.c.movie_id==movie_entry.movie_id)
67                log.debug(query_movie_tags)
68                session.execute(query_movie_tags)
69                # languages
70                query_movie_lang = delete(movie_lang_table)
71                query_movie_lang = query_movie_lang.where(movie_lang_table.c.movie_id==movie_entry.movie_id)
72                log.debug(query_movie_lang)
73                session.execute(query_movie_lang)
74                # TODO: removing posters if no longer used by another movie?
75
76            # second: remove the movie entries
77            if len(movie_ids):
78                query = delete(movies_table)
79                # use the collected movie ids because other conditions are not true anymore
80                # (f.e. tags are already deleted)
81                query = query.where(movies_table.c.movie_id.in_(movie_ids))
82
83                log.debug(query)
84                session.execute(query)
85            session.commit()
86
87            self.app.populate_treeview()
88