Merge branch 'ticket-679' into OPW-Moderation-Update
Conflicts: mediagoblin/auth/tools.py mediagoblin/auth/views.py mediagoblin/db/migration_tools.py mediagoblin/db/migrations.py mediagoblin/db/models.py mediagoblin/decorators.py mediagoblin/user_pages/views.py
This commit is contained in:
@@ -32,17 +32,18 @@ def dbupdate_parse_setup(subparser):
|
||||
|
||||
|
||||
class DatabaseData(object):
|
||||
def __init__(self, name, models, migrations):
|
||||
def __init__(self, name, models, foundations, migrations):
|
||||
self.name = name
|
||||
self.models = models
|
||||
self.foundations = foundations
|
||||
self.migrations = migrations
|
||||
|
||||
def make_migration_manager(self, session):
|
||||
return MigrationManager(
|
||||
self.name, self.models, self.migrations, session)
|
||||
self.name, self.models, self.foundations, self.migrations, session)
|
||||
|
||||
|
||||
def gather_database_data(media_types, plugins):
|
||||
def gather_database_data(plugins):
|
||||
"""
|
||||
Gather all database data relevant to the extensions we have
|
||||
installed so we can do migrations and table initialization.
|
||||
@@ -54,17 +55,11 @@ def gather_database_data(media_types, plugins):
|
||||
# Add main first
|
||||
from mediagoblin.db.models import MODELS as MAIN_MODELS
|
||||
from mediagoblin.db.migrations import MIGRATIONS as MAIN_MIGRATIONS
|
||||
from mediagoblin.db.models import FOUNDATIONS as MAIN_FOUNDATIONS
|
||||
|
||||
managed_dbdata.append(
|
||||
DatabaseData(
|
||||
u'__main__', MAIN_MODELS, MAIN_MIGRATIONS))
|
||||
|
||||
# Then get all registered media managers (eventually, plugins)
|
||||
for media_type in media_types:
|
||||
models = import_component('%s.models:MODELS' % media_type)
|
||||
migrations = import_component('%s.migrations:MIGRATIONS' % media_type)
|
||||
managed_dbdata.append(
|
||||
DatabaseData(media_type, models, migrations))
|
||||
u'__main__', MAIN_MODELS, MAIN_FOUNDATIONS, MAIN_MIGRATIONS))
|
||||
|
||||
for plugin in plugins:
|
||||
try:
|
||||
@@ -90,13 +85,26 @@ forgotten to add it? ({1})'.format(plugin, exc))
|
||||
|
||||
migrations = {}
|
||||
except AttributeError as exc:
|
||||
_log.debug('Cloud not find MIGRATIONS in {0}.migrations, have you \
|
||||
_log.debug('Could not find MIGRATIONS in {0}.migrations, have you \
|
||||
forgotten to add it? ({1})'.format(plugin, exc))
|
||||
migrations = {}
|
||||
|
||||
try:
|
||||
foundations = import_component('{0}.models:FOUNDATIONS'.format(plugin))
|
||||
except ImportError as exc:
|
||||
_log.debug('No foundations found for {0}: {1}'.format(
|
||||
plugin,
|
||||
exc))
|
||||
|
||||
foundations = []
|
||||
except AttributeError as exc:
|
||||
_log.debug('Could not find FOUNDATIONS in {0}.models, have you \
|
||||
forgotten to add it? ({1})'.format(plugin, exc))
|
||||
foundations = {}
|
||||
|
||||
if models:
|
||||
managed_dbdata.append(
|
||||
DatabaseData(plugin, models, migrations))
|
||||
DatabaseData(plugin, models, foundations, migrations))
|
||||
|
||||
|
||||
return managed_dbdata
|
||||
@@ -110,13 +118,24 @@ def run_dbupdate(app_config, global_config):
|
||||
in the future, plugins)
|
||||
"""
|
||||
|
||||
# Gather information from all media managers / projects
|
||||
dbdatas = gather_database_data(
|
||||
app_config['media_types'],
|
||||
global_config.get('plugins', {}).keys())
|
||||
|
||||
# Set up the database
|
||||
db = setup_connection_and_db_from_config(app_config, migrations=True)
|
||||
#Run the migrations
|
||||
run_all_migrations(db, app_config, global_config)
|
||||
|
||||
|
||||
def run_all_migrations(db, app_config, global_config):
|
||||
"""
|
||||
Initializes or migrates a database that already has a
|
||||
connection setup and also initializes or migrates all
|
||||
extensions based on the config files.
|
||||
|
||||
It can be used to initialize an in-memory database for
|
||||
testing.
|
||||
"""
|
||||
# Gather information from all media managers / projects
|
||||
dbdatas = gather_database_data(
|
||||
global_config.get('plugins', {}).keys())
|
||||
|
||||
Session = sessionmaker(bind=db.engine)
|
||||
|
||||
|
||||
@@ -63,7 +63,7 @@ def _import_media(db, args):
|
||||
# TODO: Add import of queue files
|
||||
queue_cache = BasicFileStorage(args._cache_path['queue'])
|
||||
|
||||
for entry in db.MediaEntry.find():
|
||||
for entry in db.MediaEntry.query.filter_by():
|
||||
for name, path in entry.media_files.items():
|
||||
_log.info('Importing: {0} - {1}'.format(
|
||||
entry.title.encode('ascii', 'replace'),
|
||||
@@ -204,7 +204,7 @@ def _export_media(db, args):
|
||||
# TODO: Add export of queue files
|
||||
queue_cache = BasicFileStorage(args._cache_path['queue'])
|
||||
|
||||
for entry in db.MediaEntry.find():
|
||||
for entry in db.MediaEntry.query.filter_by():
|
||||
for name, path in entry.media_files.items():
|
||||
_log.info(u'Exporting {0} - {1}'.format(
|
||||
entry.title,
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from mediagoblin.gmg_commands import util as commands_util
|
||||
from mediagoblin.auth import lib as auth_lib
|
||||
from mediagoblin import auth
|
||||
from mediagoblin import mg_globals
|
||||
|
||||
def adduser_parser_setup(subparser):
|
||||
@@ -40,9 +40,9 @@ def adduser(args):
|
||||
|
||||
db = mg_globals.database
|
||||
users_with_username = \
|
||||
db.User.find({
|
||||
'username': args.username.lower(),
|
||||
}).count()
|
||||
db.User.query.filter_by(
|
||||
username=args.username.lower()
|
||||
).count()
|
||||
|
||||
if users_with_username:
|
||||
print u'Sorry, a user with that name already exists.'
|
||||
@@ -52,7 +52,7 @@ def adduser(args):
|
||||
entry = db.User()
|
||||
entry.username = unicode(args.username.lower())
|
||||
entry.email = unicode(args.email)
|
||||
entry.pw_hash = auth_lib.bcrypt_gen_password_hash(args.password)
|
||||
entry.pw_hash = auth.gen_password_hash(args.password)
|
||||
entry.status = u'active'
|
||||
entry.email_verified = True
|
||||
default_privileges = [
|
||||
@@ -78,7 +78,8 @@ def makeadmin(args):
|
||||
|
||||
db = mg_globals.database
|
||||
|
||||
user = db.User.one({'username': unicode(args.username.lower())})
|
||||
user = db.User.query.filter_by(
|
||||
username=unicode(args.username.lower())).one()
|
||||
if user:
|
||||
user.is_admin = True
|
||||
user.all_privileges.append(
|
||||
@@ -105,9 +106,10 @@ def changepw(args):
|
||||
|
||||
db = mg_globals.database
|
||||
|
||||
user = db.User.one({'username': unicode(args.username.lower())})
|
||||
user = db.User.query.filter_by(
|
||||
username=unicode(args.username.lower())).one()
|
||||
if user:
|
||||
user.pw_hash = auth_lib.bcrypt_gen_password_hash(args.password)
|
||||
user.pw_hash = auth.gen_password_hash(args.password)
|
||||
user.save()
|
||||
print 'Password successfully changed'
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user