本文整理汇总了Python中django.db.migrations.state.StateApps类的典型用法代码示例。如果您正苦于以下问题:Python StateApps类的具体用法?Python StateApps怎么用?Python StateApps使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了StateApps类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: setUpBeforeMigration
def setUpBeforeMigration(self, apps: StateApps) -> None:
Reaction = apps.get_model('zerver', 'Reaction')
RealmEmoji = apps.get_model('zerver', 'RealmEmoji')
Message = apps.get_model('zerver', 'Message')
Recipient = apps.get_model('zerver', 'Recipient')
sender = self.example_user('iago')
realm = sender.realm
sending_client = make_client(name="test suite")
stream_name = 'Denmark'
stream = get_stream(stream_name, realm)
subject = 'foo'
def send_fake_message(message_content: str, stream: ModelBase) -> ModelBase:
recipient = Recipient.objects.get(type_id=stream.id, type=2)
return Message.objects.create(sender = sender,
recipient = recipient,
subject = subject,
content = message_content,
pub_date = timezone_now(),
sending_client = sending_client)
message = send_fake_message('Test 1', stream)
# Create reactions for all the realm emoji's on the message we faked.
for realm_emoji in RealmEmoji.objects.all():
reaction = Reaction(user_profile=sender, message=message,
emoji_name=realm_emoji.name, emoji_code=realm_emoji.name,
reaction_type='realm_emoji')
reaction.save()
realm_emoji_reactions_count = Reaction.objects.filter(reaction_type='realm_emoji').count()
self.assertEqual(realm_emoji_reactions_count, 1)
示例2: set_initial_value_of_is_private_flag
def set_initial_value_of_is_private_flag(
apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
UserMessage = apps.get_model("zerver", "UserMessage")
Message = apps.get_model("zerver", "Message")
if not Message.objects.exists():
return
i = 0
# Total is only used for the progress bar
total = Message.objects.filter(recipient__type__in=[1, 3]).count()
processed = 0
print("\nStart setting initial value for is_private flag...")
sys.stdout.flush()
while True:
range_end = i + 10000
# Can't use [Recipient.PERSONAL, Recipient.HUDDLE] in migration files
message_ids = list(Message.objects.filter(recipient__type__in=[1, 3],
id__gt=i,
id__lte=range_end).values_list("id", flat=True).order_by("id"))
count = UserMessage.objects.filter(message_id__in=message_ids).update(flags=F('flags').bitor(UserMessage.flags.is_private))
if count == 0 and range_end >= Message.objects.last().id:
break
i = range_end
processed += len(message_ids)
if total != 0:
percent = round((processed / total) * 100, 2)
else:
percent = 100.00
print("Processed %s/%s %s%%" % (processed, total, percent))
sys.stdout.flush()
示例3: add_domain_to_realm_alias_if_needed
def add_domain_to_realm_alias_if_needed(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
Realm = apps.get_model('zerver', 'Realm')
RealmAlias = apps.get_model('zerver', 'RealmAlias')
for realm in Realm.objects.all():
# if realm.domain already exists in RealmAlias, assume it is correct
if not RealmAlias.objects.filter(domain=realm.domain).exists():
RealmAlias.objects.create(realm=realm, domain=realm.domain)
示例4: convert_muted_topics
def convert_muted_topics(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
stream_query = '''
SELECT
zerver_stream.name,
zerver_stream.realm_id,
zerver_stream.id,
zerver_recipient.id
FROM
zerver_stream
INNER JOIN zerver_recipient ON (
zerver_recipient.type_id = zerver_stream.id AND
zerver_recipient.type = 2
)
'''
stream_dict = {}
with connection.cursor() as cursor:
cursor.execute(stream_query)
rows = cursor.fetchall()
for (stream_name, realm_id, stream_id, recipient_id) in rows:
stream_name = stream_name.lower()
stream_dict[(stream_name, realm_id)] = (stream_id, recipient_id)
UserProfile = apps.get_model("zerver", "UserProfile")
MutedTopic = apps.get_model("zerver", "MutedTopic")
new_objs = []
user_query = UserProfile.objects.values(
'id',
'realm_id',
'muted_topics'
)
for row in user_query:
user_profile_id = row['id']
realm_id = row['realm_id']
muted_topics = row['muted_topics']
tups = ujson.loads(muted_topics)
for (stream_name, topic_name) in tups:
stream_name = stream_name.lower()
val = stream_dict.get((stream_name, realm_id))
if val is not None:
stream_id, recipient_id = val
muted_topic = MutedTopic(
user_profile_id=user_profile_id,
stream_id=stream_id,
recipient_id=recipient_id,
topic_name=topic_name,
)
new_objs.append(muted_topic)
with connection.cursor() as cursor:
cursor.execute('DELETE from zerver_mutedtopic')
MutedTopic.objects.bulk_create(new_objs)
示例5: delete_messages_sent_to_stream_stat
def delete_messages_sent_to_stream_stat(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
UserCount = apps.get_model('analytics', 'UserCount')
StreamCount = apps.get_model('analytics', 'StreamCount')
RealmCount = apps.get_model('analytics', 'RealmCount')
InstallationCount = apps.get_model('analytics', 'InstallationCount')
FillState = apps.get_model('analytics', 'FillState')
property = 'messages_sent_to_stream:is_bot'
UserCount.objects.filter(property=property).delete()
StreamCount.objects.filter(property=property).delete()
RealmCount.objects.filter(property=property).delete()
InstallationCount.objects.filter(property=property).delete()
FillState.objects.filter(property=property).delete()
示例6: clear_message_sent_by_message_type_values
def clear_message_sent_by_message_type_values(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
UserCount = apps.get_model('analytics', 'UserCount')
StreamCount = apps.get_model('analytics', 'StreamCount')
RealmCount = apps.get_model('analytics', 'RealmCount')
InstallationCount = apps.get_model('analytics', 'InstallationCount')
FillState = apps.get_model('analytics', 'FillState')
property = 'messages_sent:message_type:day'
UserCount.objects.filter(property=property).delete()
StreamCount.objects.filter(property=property).delete()
RealmCount.objects.filter(property=property).delete()
InstallationCount.objects.filter(property=property).delete()
FillState.objects.filter(property=property).delete()
示例7: backfill_first_message_id
def backfill_first_message_id(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
Stream = apps.get_model('zerver', 'Stream')
Message = apps.get_model('zerver', 'Message')
for stream in Stream.objects.all():
first_message = Message.objects.filter(
recipient__type_id=stream.id,
recipient__type=2).first()
if first_message is None:
# No need to change anything if the outcome is the default of None
continue
stream.first_message_id = first_message.id
stream.save()
示例8: populate_is_zephyr
def populate_is_zephyr(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
Realm = apps.get_model("zerver", "Realm")
Stream = apps.get_model("zerver", "Stream")
realms = Realm.objects.filter(
string_id='zephyr',
)
for realm in realms:
Stream.objects.filter(
realm_id=realm.id
).update(
is_in_zephyr_realm=True
)
示例9: backfill_user_activations_and_deactivations
def backfill_user_activations_and_deactivations(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
migration_time = timezone_now()
RealmAuditLog = apps.get_model('zerver', 'RealmAuditLog')
UserProfile = apps.get_model('zerver', 'UserProfile')
for user in UserProfile.objects.all():
RealmAuditLog.objects.create(realm=user.realm, modified_user=user,
event_type='user_created', event_time=user.date_joined,
backfilled=False)
for user in UserProfile.objects.filter(is_active=False):
RealmAuditLog.objects.create(realm=user.realm, modified_user=user,
event_type='user_deactivated', event_time=migration_time,
backfilled=True)
示例10: set_realm_for_existing_scheduledemails
def set_realm_for_existing_scheduledemails(
apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
scheduledemail_model = apps.get_model("zerver", "ScheduledEmail")
preregistrationuser_model = apps.get_model("zerver", "PreregistrationUser")
for scheduledemail in scheduledemail_model.objects.all():
if scheduledemail.type == 3: # ScheduledEmail.INVITATION_REMINDER
# Don't think this can be None, but just be safe
prereg = preregistrationuser_model.objects.filter(email=scheduledemail.address).first()
if prereg is not None:
scheduledemail.realm = prereg.realm
else:
scheduledemail.realm = scheduledemail.user.realm
scheduledemail.save(update_fields=['realm'])
# Shouldn't be needed, but just in case
scheduledemail_model.objects.filter(realm=None).delete()
示例11: set_initial_value_for_signup_notifications_stream
def set_initial_value_for_signup_notifications_stream(
apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
realm_model = apps.get_model("zerver", "Realm")
realms = realm_model.objects.exclude(notifications_stream__isnull=True)
for realm in realms:
realm.signup_notifications_stream = realm.notifications_stream
realm.save(update_fields=["signup_notifications_stream"])
示例12: render_all_stream_descriptions
def render_all_stream_descriptions(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
Stream = apps.get_model('zerver', 'Stream')
all_streams = Stream.objects.exclude(description='')
for stream in all_streams:
stream.rendered_description = bugdown_convert(stream.description,
no_previews=True)
stream.save(update_fields=["rendered_description"])
示例13: reset_is_private_flag
def reset_is_private_flag(
apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
UserMessage = apps.get_model("zerver", "UserMessage")
UserProfile = apps.get_model("zerver", "UserProfile")
user_profile_ids = UserProfile.objects.all().order_by("id").values_list("id", flat=True)
# We only need to do this because previous migration
# zerver/migrations/0100_usermessage_remove_is_me_message.py
# didn't clean the field after removing it.
i = 0
total = len(user_profile_ids)
print("Setting default values for the new flag...")
sys.stdout.flush()
for user_id in user_profile_ids:
while True:
# Ideally, we'd just do a single database query per user.
# Unfortunately, Django doesn't use the fancy new index on
# is_private that we just generated if we do that,
# resulting in a very slow migration that could take hours
# on a large server. We address this issue by doing a bit
# of hackery to generate the SQL just right (with an
# `ORDER BY` clause that forces using the new index).
flag_set_objects = UserMessage.objects.filter(user_profile__id = user_id).extra(
where=["flags & 2048 != 0"]).order_by("message_id")[0:1000]
user_message_ids = flag_set_objects.values_list("id", flat=True)
count = UserMessage.objects.filter(id__in=user_message_ids).update(
flags=F('flags').bitand(~UserMessage.flags.is_private))
if count < 1000:
break
i += 1
if (i % 50 == 0 or i == total):
percent = round((i / total) * 100, 2)
print("Processed %s/%s %s%%" % (i, total, percent))
sys.stdout.flush()
示例14: reversal
def reversal(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
# Ensures that migration can be re-run in case of a failure.
RealmEmoji = apps.get_model('zerver', 'RealmEmoji')
for realm_emoji in RealmEmoji.objects.all():
corrupt_file_name = realm_emoji.file_name
correct_file_name = get_emoji_file_name(corrupt_file_name, realm_emoji.name)
realm_emoji.file_name = correct_file_name
realm_emoji.save(update_fields=['file_name'])
示例15: set_initial_value_for_bot_creation_policy
def set_initial_value_for_bot_creation_policy(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
Realm = apps.get_model("zerver", "Realm")
for realm in Realm.objects.all():
if realm.create_generic_bot_by_admins_only:
realm.bot_creation_policy = 2 # BOT_CREATION_LIMIT_GENERIC_BOTS
else:
realm.bot_creation_policy = 1 # BOT_CREATION_EVERYONE
realm.save(update_fields=["bot_creation_policy"])