def forwards(self, orm): message = "Adding denormalized tags field to threads" num_questions = orm.Question.objects.count() for question in ProgressBar(orm.Question.objects.iterator(), num_questions, message): thread = question.thread thread.tagnames = question.tagnames thread.save() if orm.Question.objects.exclude( tagnames=models.F('thread__tagnames')).exists(): raise ValueError( "There are Thread instances for which data doesn't match Question!" )
def forwards(self, orm): message = "Adding view counts to threads" num_questions = orm.Question.objects.count() for question in ProgressBar(orm.Question.objects.iterator(), num_questions, message): thread = question.thread thread.view_count = question.view_count thread.save() if orm.Question.objects.exclude( view_count=models.F('thread__view_count')).exists(): raise ValueError( "There are Thread instances for which data doesn't match Question!" )
def add_legacy_links(self): questions = models.Post.objects.filter(post_type='question') count = questions.count() message = 'Adding links to old forum' template = """\n\n{quote}This thread was imported from the previous forum. For your reference, the original is [available here|%s]{quote}""" for question in ProgressBar(questions.iterator(), count, message): thread_id = question.old_question_id jive_url = self.jive_url old_url = '%s/thread.jspa?threadID=%s' % (jive_url, thread_id) question.text += template % old_url question.save() transaction.commit() transaction.commit()
def forwards(self, orm): message = "Adding problem counts to threads" num_exercises = orm.Exercise.objects.count() for exercise in ProgressBar(orm.Exercise.objects.iterator(), num_exercises, message): thread = exercise.thread thread.problem_count = exercise.problem_count thread.save() if orm.Exercise.objects.exclude( problem_count=models.F('thread__problem_count')).exists(): raise ValueError( "There are Thread instances for which data doesn't match Exercise!" )
def forwards(self, orm): "Write your forwards methods here." print "Converting question to threads:" num_questions = orm.Question.objects.count() for question in ProgressBar(orm.Question.objects.iterator(), num_questions): thread = orm.Thread.objects.create(favourite_count=question.favourite_count) question.thread = thread question.save() if orm.Question.objects.filter(thread__isnull=True).exists(): raise ValueError('There are Thread-less questions after this migration has been applied!') if orm.Question.objects.exclude(favourite_count=models.F('thread__favourite_count')).exists(): raise ValueError("There are Thread instances for which data doesn't match Question!")
def forwards(self, orm): "Write your forwards methods here." message = 'Creating personal group for the users' users = orm['auth.User'].objects.all() for user in ProgressBar(users.iterator(), users.count(), message): group_name = format_group_name(user) group_tag = orm['askbot.Tag'](name=group_name, created_by=user) group_tag.save() group_profile = orm['askbot.GroupProfile'](group_tag=group_tag, is_open=False) group_profile.save() membership = orm['askbot.GroupMembership'](group=group_tag, user=user) membership.save()
def forwards(self, orm): # ContentType for Post model should be created no later than in migration 0092 ct_post = orm['contenttypes.ContentType'].objects.get(app_label='askbot', model='post') message = "Connecting award objects to posts" num_awards = orm.Award.objects.count() for aw in ProgressBar(orm.Award.objects.iterator(), num_awards, message): ct = aw.content_type if ct.app_label == 'askbot' and ct.model in ('question', 'answer', 'comment'): aw.content_type = ct_post try: aw.object_id = orm.Post.objects.get(**{'self_%s__id' % str(ct.model): aw.object_id}).id except orm.Post.DoesNotExist: continue aw.save() ### message = "Connecting repute objects to posts" num_reputes = orm.Repute.objects.count() for rp in ProgressBar(orm.Repute.objects.iterator(), num_reputes, message): if rp.question: rp.question_post = orm.Post.objects.get(self_question__id=rp.question.id) rp.save()
def forwards(self, orm): message = "Adding followers to threads" num_exercises = orm.Exercise.objects.count() for exercise in ProgressBar(orm.Exercise.objects.iterator(), num_exercises, message): exercise.thread.followed_by.clear( ) # just in case someone reversed this migration exercise.thread.followed_by.add( *list(exercise.followed_by.iterator())) if exercise.followed_by.count( ) != exercise.thread.followed_by.count(): raise ValueError( "There are Thread instances for which data doesn't match Exercise!" )
def handle(self, *args, **kwargs): help = 'expires RapidResponder badges' expire_date = timezone.now() - datetime.timedelta( askbot_settings.RAPID_RESPONDER_BADGE_EXPIRES) awards = Award.objects.filter(badge__slug=badges.RapidResponder.key, awarded_at__lte=expire_date) expired_count = 0 for award in ProgressBar(awards.iterator(), awards.count(), 'Expiring RapidResponder Badges'): award.delete() expired_count += 1 print('Expired %d RapidResponder badges' % expired_count)
def populate_askbot_roles(apps, schema_editor): Role = apps.get_model('askbot', 'Role') UserProfile = apps.get_model('askbot', 'UserProfile') profiles = UserProfile.objects.filter(status__in=('m', 'd')) count = profiles.count() message = 'Assigning roles {} to the admins and the moderators' message = message.format(', '.join(MOD_ROLES + ADMIN_ROLES)) for profile in ProgressBar(profiles.iterator(), count, message): user = profile.auth_user_ptr if profile.status == 'd': for role in ADMIN_ROLES + MOD_ROLES: Role.objects.create(user=user, role=role) else: for role in MOD_ROLES: Role.objects.create(user=user, role=role)
def calculate_localized_reps(apps, schema_editor): User = apps.get_model('auth', 'User') Profile = apps.get_model('askbot', 'LocalizedUserProfile') Repute = apps.get_model('askbot', 'Repute') users = User.objects message = 'Calculating localized reputations' print('') for user in ProgressBar(users.iterator(), users.count(), message): reps = user.repute_set.values('language_code') reps = reps.annotate( negative=Sum('negative'), positive=Sum('positive') ) for rep in reps: lang = rep['language_code'] profile, junk = Profile.objects.get_or_create( auth_user=user, language_code=lang) profile.reputation = max(0, rep['positive'] + rep['negative']) profile.save() #recalculate the total reputation aggregate = Profile.objects.filter( auth_user=user ).aggregate(reputation=Sum('reputation')) aggregate_rep = aggregate['reputation'] or 0 #dict might have 'None' value new_rep = const.MIN_REPUTATION + aggregate_rep old_rep = user.askbot_profile.reputation #compensate if new rep is less than old if new_rep < old_rep: comp = Repute() comp.user = user comp.positive = old_rep - new_rep comp.reputation_type = 10 with translation.override(user.askbot_profile.primary_language): comp.comment = _('Compensated by admin during recalculation of karma') comp.save() new_rep = old_rep #update total reputation if new_rep != old_rep: user.askbot_profile.reputation = new_rep user.askbot_profile.save() print('old %d new %d' % (old_rep, new_rep))
def forwards(self, orm): "Write your forwards methods here." # Note: Don't use "from appname.models import ModelName". # Use orm.ModelName to refer to models in this application, # and orm['appname.ModelName'] for models in other applications. threads = orm['askbot.Thread'].objects.exclude(accepted_answer=None) count = threads.count() message = 'Applying accepted answer data to answer posts' now = datetime.datetime.now() admin = self.get_first_admin(orm) for thread in ProgressBar(threads.iterator(), count, message): answer = thread.accepted_answer answer.endorsed = True answer.endorsed_at = thread.answer_accepted_at answer.endorsed_by = admin #may be none answer.save()
def transfer_messages(self): """transfers some messages from SE to ASKBOT """ messages = se.Message.objects.all() for m in ProgressBar(messages.iterator(), messages.count()): if m.is_read: continue if m.user is None: continue if m.user.id == -1: continue u = X.get_user(m.user) text = X.get_message_text(m) if text: u.message_set.create(message=text, )
def _collect_missing_badges(self): self._missing_badges = {} badges = se.Badge.objects.all() message = 'Collecting missing badges' for se_b in ProgressBar(badges.iterator(), badges.count(), message): name = X.get_badge_name(se_b.name) try: #todo: query badge from askbot.models.badges #using case-insensitive name matching askbot.badges.get_badge(name=name) except KeyError: #todo: if absent - print error message #and drop it self._missing_badges[name] = 0 if len(se_b.description) > 300: print 'Warning truncated description for badge %d' % se_b.id sys.stdout.flush()
def forwards(self, orm): "Write your forwards methods here." # Note: Don't use "from appname.models import ModelName". # Use orm.ModelName to refer to models in this application, # and orm['appname.ModelName'] for models in other applications. threads = orm['askbot.Thread'].objects.all() count = threads.count() message = 'Applying language code to tags:' for thread in ProgressBar(threads.iterator(), count, message): tags = thread.tags.all() #load each tag by name + language code for tag in tags: #maybe fix language code of thread, unlikely if thread.language_code == '': thread.language_code = django_settings.LANGUAGE_CODE thread.save() #if tg has no language code, we just copy it from thread if tag.language_code == '': tag.language_code = thread.language_code tag.save() elif tag.language_code != thread.language_code: try: new_tag = orm['askbot.Tag'].objects.get( name=tag.name, language_code=thread.language_code ) if tags.filter(id=new_tag.id).exists(): continue new_tag.used_count += 1 except orm['askbot.Tag'].DoesNotExist: new_tag = orm['askbot.Tag']() new_tag.name = tag.name new_tag.language_code = thread.language_code new_tag.created_by = thread.last_activity_by new_tag.used_count = 1 new_tag.status = 1 new_tag.save() new_tag.suggested_by.add(thread.last_activity_by) if tag.used_count > 1: tag.used_count -= 1 tag.save() thread.tags.remove(tag) thread.tags.add(new_tag)
def handle_noargs(self, **options): acts = Activity.objects.all() deleted_count = 0 message = "Searching for context-less activity objects:" for act in ProgressBar(acts.iterator(), acts.count(), message): try: if act.object_id != None and act.content_object == None: act.delete() deleted_count += 1 except: #this can happen if we have a stale content type act.delete() if deleted_count: print "%d activity objects deleted" % deleted_count else: print "None found"
def handle_noargs(self, **options): """deletes old sessions""" quiet = options.get('quiet', False) expired_sessions = Session.objects.filter( expire_date__lt=timezone.now()) count = expired_sessions.count() expired_sessions = expired_sessions.iterator() if not quiet: message = 'There are %d expired sessions' % count expired_sessions = ProgressBar(expired_sessions, count, message) deleted_count = 0 for session in expired_sessions: session.delete() deleted_count += 1 if deleted_count % ITEMS_PER_TRANSACTION == 0: transaction.commit() transaction.commit()
def transfer_comments(self): comments = se.PostComment.objects.all() for se_c in ProgressBar(comments.iterator(), comments.count()): if se_c.deletion_date: print 'Warning deleted comment %d dropped' % se_c.id sys.stdout.flush() continue se_post = se_c.post askbot_post = X.get_post(se_post) if askbot_post is None: continue se_author = se_c.user if se_author is None: continue comment = askbot_post.add_comment(comment=se_c.text, added_at=se_c.creation_date, user=USER[se_author.id]) COMMENT[se_c.id] = comment
def import_users(self, user_soup): """import users from jive to askbot""" message = 'Importing users:' for user in ProgressBar(iter(user_soup), len(user_soup), message): username = user.find('Username').text real_name = user.find('Name').text try: email = EmailField().clean(user.find('Email').text) except ValidationError: email = '*****@*****.**' % self.bad_email_count self.bad_email_count += 1 joined_timestamp = parse_date(user.find('CreationDate').text) user = models.User(username=username, email=email, real_name=real_name, date_joined=joined_timestamp) user.save() transaction.commit()
def handle_noargs(self, *args, **kwargs): now = timezone.now() awarded_count = 0 users = User.objects.all() count = users.count() message = 'Awarding badges for each user' for user in ProgressBar(users.iterator(), count, message): try: # get last vote vote = Vote.objects.filter(user=user).order_by('-id')[0] except IndexError: # user did not vote continue else: cd = badges.CivicDuty() awarded = cd.consider_award(actor=user, context_object=vote.voted_post, timestamp=now) awarded_count += int(awarded) print('Awarded %d badges' % awarded_count)
def transfer_comment_votes(self): votes = se.Comment2Vote.objects.all() for v in ProgressBar(votes.iterator(), votes.count()): vote_type = v.vote_type.name if vote_type not in ('UpMod', 'Offensive'): continue if v.user is None: continue p = X.get_post(v.post_comment) #could also check deletion date on the Comment2Vote object #instead of making get_post return None on KeyError inside if p is None: #may be a deleted post continue u = X.get_user(v.user) m = X.vote_actions[vote_type] vote_method = getattr(askbot.User, m) vote_method(u, p, timestamp=v.creation_date, force=True) transaction.commit()
def promote_company_replies(self, domain): admin = turn_first_company_user_to_admin(domain) if admin is None: print "Note: did not find any users with email matching %s" % domain return message = 'Promoting company replies to accepted answers:' threads = models.Thread.objects.all() count = threads.count() for thread in ProgressBar(threads.iterator(), count, message): answer = thread_get_answer_from_company(thread, domain) if answer == None: comment = thread_find_first_comment_from_company(thread, domain) if comment: admin.repost_comment_as_answer(comment) answer = comment if answer: admin.accept_best_answer(answer=answer, force=True) transaction.commit() transaction.commit()
def handle(self, *args, **kwargs): """reads the tags file, parses it, then applies tags to questions by matching them with the question titles and content """ translation.activate(django_settings.LANGUAGE_CODE) if kwargs['tags_file'] is None: raise CommandError('parameter --tags-file is required') try: tags_input = open(kwargs['tags_file']).read() except IOError: raise CommandError('file "%s" not found' % kwargs['tags_file']) tags_list = map(lambda v: v.strip(), tags_input.split('\n')) multiword_tags = list() for tag in tags_list: if ' ' in tag: multiword_tags.append(tag) if len(multiword_tags): message = 'multiword tags tags not allowed, have: %s' % ', '.join( multiword_tags) raise CommandError(message) threads = Thread.objects.all() count = threads.count() message = 'Applying tags to questions' user = User.objects.all().order_by('-id')[0] now = timezone.now() for thread in ProgressBar(threads.iterator(), count, message): thread.apply_hinted_tags(tags_list, user=user, timestamp=now, silent=True)
def forwards(self, orm): orm.Post.objects.all().delete() # in case there are some leftovers after this migration failed before if 'test' not in sys.argv: # Don't confuse users print TERM_RED_BOLD, "!!! Remember to not remove the old content types for Exercise, Problem and Comment models until further notice from migration 0101!", TERM_RESET post_id = max( # 'or 0' protects against None orm.Exercise.objects.aggregate(max_id=models.Max('id'))['max_id'] or 0, orm.Problem.objects.aggregate(max_id=models.Max('id'))['max_id'] or 0, orm.Comment.objects.aggregate(max_id=models.Max('id'))['max_id'] or 0, ) if 'test' not in sys.argv: # Don't confuse users print TERM_GREEN, '[DEBUG] Initial Post.id ==', post_id + 1, TERM_RESET message = "Converting Exercises -> Posts" num_exercises = orm.Exercise.objects.count() for q in ProgressBar(orm.Exercise.objects.iterator(), num_exercises, message): post_id += 1 orm.Post.objects.create( id=post_id, post_type='exercise', old_problem_id=None, old_exercise_id=q.id, old_comment_id=None, self_problem=None, self_exercise=q, self_comment=None, thread=q.thread, parent=None, author=q.author, added_at=q.added_at, deleted=q.deleted, deleted_at=q.deleted_at, deleted_by=q.deleted_by, wiki=q.wiki, wikified_at=q.wikified_at, locked=q.locked, locked_by=q.locked_by, locked_at=q.locked_at, score=q.score, vote_up_count=q.vote_up_count, vote_down_count=q.vote_down_count, comment_count=q.comment_count, offensive_flag_count=q.offensive_flag_count, last_edited_at=q.last_edited_at, last_edited_by=q.last_edited_by, html=q.html, text=q.text, summary=q.summary, is_anonymous=q.is_anonymous, ) message = "Problems -> Posts" num_problems = orm.Problem.objects.count() for ans in ProgressBar(orm.Problem.objects.iterator(), num_problems, message): post_id += 1 orm.Post.objects.create( id=post_id, post_type='problem', old_problem_id=ans.id, old_exercise_id=None, old_comment_id=None, self_problem=ans, self_exercise=None, self_comment=None, thread=ans.exercise.thread, parent=None, author=ans.author, added_at=ans.added_at, deleted=ans.deleted, deleted_at=ans.deleted_at, deleted_by=ans.deleted_by, wiki=ans.wiki, wikified_at=ans.wikified_at, locked=ans.locked, locked_by=ans.locked_by, locked_at=ans.locked_at, score=ans.score, vote_up_count=ans.vote_up_count, vote_down_count=ans.vote_down_count, comment_count=ans.comment_count, offensive_flag_count=ans.offensive_flag_count, last_edited_at=ans.last_edited_at, last_edited_by=ans.last_edited_by, html=ans.html, text=ans.text, summary=ans.summary, is_anonymous=ans.is_anonymous, ) message = "Comments -> Posts" num_comments = orm.Comment.objects.count() for cm in ProgressBar(orm.Comment.objects.iterator(), num_comments, message): # Workaround for a strange issue with: http://south.aeracode.org/docs/generics.html # No need to investigate that as this is as simple as the "proper" way if (cm.content_type.app_label, cm.content_type.model) == ('askbot', 'exercise'): content_object = orm.Exercise.objects.get(id=cm.object_id) thread = orm.Thread.objects.get(id=content_object.thread.id) # convert from Thread to orm.Thread - a subtle difference parent = orm.Post.objects.get(self_exercise=content_object) elif (cm.content_type.app_label, cm.content_type.model) == ('askbot', 'problem'): content_object = orm.Problem.objects.get(id=cm.object_id) thread = orm.Thread.objects.get(id=content_object.exercise.thread.id) # convert from Thread to orm.Thread - a subtle difference parent = orm.Post.objects.get(self_problem=content_object) else: raise ValueError('comment.content_object is neither exercise nor problem!') post_id += 1 orm.Post.objects.create( id=post_id, post_type='comment', old_problem_id=None, old_exercise_id=None, old_comment_id=cm.id, self_problem=None, self_exercise=None, self_comment=cm, thread=thread, parent=parent, author=cm.user, added_at=cm.added_at, deleted=False, deleted_at=None, deleted_by=None, wiki=False, wikified_at=None, locked=False, locked_by=None, locked_at=None, score=cm.score, vote_up_count=cm.score, vote_down_count=0, comment_count=0, offensive_flag_count=cm.offensive_flag_count, last_edited_at=None, last_edited_by=None, html=cm.html, text=cm.comment, summary='', is_anonymous=False, ) if orm.Post.objects.exists(): # Check Post.id sequence/auto-increment to make sure new Post-s don't start from id=1 if db.backend_name == 'mysql': # Docs: # - http://stackoverflow.com/exercises/933565/get-auto-increment-value-with-mysql-query # - autoincrement = db.execute("SELECT auto_increment FROM information_schema.tables WHERE table_name='askbot_post' AND table_schema=DATABASE()")[0][0] elif db.backend_name == 'sqlite3': # Docs: # - http://www.sqlite.org/autoinc.html # - http://www.sqlite.org/c3ref/last_insert_rowid.html # - http://www.sqlite.org/faq.html#q1 # - http://stackoverflow.com/exercises/531109/how-to-return-the-value-of-auto-increment-column-in-sqlite-with-vb6 autoincrement = db.execute("SELECT last_insert_rowid() FROM askbot_post")[0][0] + 1 elif db.backend_name == 'postgres': # Docs: # - http://lindsaar.net/2008/11/2/how-to-reset-a-sequence-with-postgresql # - http://www.postgresql.org/docs/8.3/static/functions-sequence.html # Note that SELECT SETVAL(...) returns the value being set autoincrement = db.execute("SELECT SETVAL('askbot_post_id_seq', %s)", params=[post_id + 1])[0][0] else: autoincrement = -1 print TERM_RED_BOLD, ("You are using `%s` database backend which is not officially supported. " "Therefore after migrations are applied you should make sure that autoincrement/sequence value for " "table `askbot_post` is set to %d") % (db.backend_name, post_id + 1), TERM_RESET if autoincrement != -1: if autoincrement != (post_id + 1): raise ValueError('Auto_increment for askbot_post table should be %d but is %d!' % (post_id + 1, autoincrement)) print TERM_GREEN, '[DEBUG] %s: auto-increment/sequence-value for askbot_post table is OK (%d)' % ( db.backend_name, autoincrement), TERM_RESET # Verify that numbers match sum_all = orm.Exercise.objects.count() + orm.Problem.objects.count() + orm.Comment.objects.count() if sum_all != orm.Post.objects.count(): raise ValueError('sum(#Q,#C,#A) != #Post !')
def transfer_users(self): se_users = se.User.objects.all() for se_u in ProgressBar(se_users.iterator(), se_users.count()): #if se_u.id == -1:#skip the Community user # continue u = askbot.User() u_type = se_u.user_type.name if u_type == 'Administrator': u.set_status('d') elif u_type == 'Moderator': u.set_status('m') elif u_type not in ('Unregistered', 'Registered'): raise Exception('unknown user type %s' % u_type) if se_u.password_id is not None: pw = se.Password.objects.get(id=se_u.password_id) u.password = '******' % (pw.salt, pw.password) else: u.set_unusable_password() #if user is not registered, no association record created #we do not allow posting by users who are not authenticated #probably they'll just have to "recover" their account by email if u_type != 'Unregistered': try: assert (se_u.open_id) #everybody must have open_id u_openid = askbot_openid.UserAssociation() u_openid.openid_url = se_u.open_id u.save() u_openid.user = u u_openid.last_used_timestamp = se_u.last_login_date u_openid.save() except AssertionError: print u'User %s (id=%d) does not have openid' % \ (unidecode(se_u.display_name), se_u.id) sys.stdout.flush() except IntegrityError: print "Warning: have duplicate openid: %s" % se_u.open_id sys.stdout.flush() if se_u.open_id is None and se_u.email is None: print 'Warning: SE user %d is not recoverable (no email or openid)' sys.stdout.flush() u.reputation = 1 #se_u.reputation, it's actually re-computed u.last_seen = se_u.last_access_date u.email = X.get_email(se_u.email) u.location = X.blankable(se_u.location) u.date_of_birth = se_u.birthday #dattime -> date u.website = X.blankable(se_u.website_url) u.about = X.blankable(se_u.about_me) if se_u.last_login_date is None: u.last_login = se_u.creation_date else: u.last_login = se_u.last_login_date u.date_joined = se_u.creation_date u.is_active = True #todo: this may not be the case u.username = X.get_screen_name(se_u) u.real_name = X.blankable(se_u.real_name) (gold, silver, bronze) = X.parse_badge_summary(se_u.badge_summary) u.gold = gold u.silver = silver u.bronze = bronze #todo: we don't have these fields #views - number of profile views? #has_replies #has_message #opt_in_recruit #last_login_ip #open_id_alt - ?? #preferences_raw - not clear how to use #display_name_cleaned - lowercased, srtipped name #timed_penalty_date #phone #don't know how to handle these - there was no usage example #password_id #guid #ignored #last_email_date - this translates directly to EmailFeedSetting.reported_at #save the data try: other = askbot.User.objects.get(username=u.username) print 'alert - have a second user with name %s' % u.username sys.sdtout.flush() except askbot.User.DoesNotExist: pass u.save() form = EditUserEmailFeedsForm() form.reset() if se_u.opt_in_email == True: #set up daily subscription on "own" items form.initial['individually_selected'] = 'd' form.initial['asked_by_me'] = 'd' form.initial['answered_by_me'] = 'd' # form.save(user=u, save_unbound=True) USER[se_u.id] = u
def handle_noargs(self, **options): message = "Rebuilding thread summary cache" count = Thread.objects.count() for thread in ProgressBar(Thread.objects.iterator(), count, message): thread.update_summary_html()
def forwards(self, orm): message = "Connecting votes to posts" num_votes = orm.Vote.objects.count() for v in ProgressBar(orm.Vote.objects.iterator(), num_votes, message): try: if (v.content_type.app_label, v.content_type.model) == ('askbot', 'question'): v.voted_post = orm.Post.objects.get( self_question__id=v.object_id) elif (v.content_type.app_label, v.content_type.model) == ('askbot', 'answer'): v.voted_post = orm.Post.objects.get( self_answer__id=v.object_id) elif (v.content_type.app_label, v.content_type.model) == ('askbot', 'comment'): v.voted_post = orm.Post.objects.get( self_comment__id=v.object_id) else: raise ValueError('Unknown vote subject!') v.save() except orm.Post.DoesNotExist: print TERM_RED_BOLD, 'Post of type=%s, id=%s does not exist!!!' % ( v.content_type.model, v.object_id) v.delete() ### # ContentType for Post model might not yet be present in the database # (if migrations are applied in a row then contenttypes update is not called between them) ct_post, c = orm['contenttypes.ContentType'].objects.get_or_create( app_label='askbot', model='post', defaults={'name': 'post'}) abandoned_activities = [] message = "Connecting activity objects to posts" num_activities = orm.Activity.objects.count() for a in ProgressBar(orm.Activity.objects.iterator(), num_activities, message): # test if content_object for this activity exists - there might be a bunch of "abandoned" activities # # NOTE that if activity.content_object is gone then we cannot reliably recover it from activity.question # - the latter is just a thread to which the activity is related! It might occasionally be the actual post # the activity is related to, but it's not the general rule. model_signature = '.'.join( [a.content_type.app_label, a.content_type.model]) if not orm[model_signature].objects.filter( id=a.object_id).exists(): abandoned_activities.append(a) continue save = False ct = a.content_type if ct.app_label == 'askbot' and ct.model in ('question', 'answer', 'comment'): a.content_type = ct_post a.object_id = orm.Post.objects.get( **{ 'self_%s__id' % str(ct.model): a.object_id }).id save = True if a.question: a.question_post = orm.Post.objects.get( self_question__id=a.question.id) save = True if save: a.save() if abandoned_activities: # Remove "abandoned" activities abandoned_activities_lst = [ (a.id, '.'.join([a.content_type.app_label, a.content_type.model]), a.object_id) for a in abandoned_activities ] print TERM_RED_BOLD, "!!! Abandoned activities num=%d, total num=%d:" % ( len(abandoned_activities), orm.Activity.objects.count()), TERM_RESET print TERM_GREEN, abandoned_activities_lst, TERM_RESET for a in abandoned_activities: a.delete()
def handle(self, *args, **kwargs): users = User.objects.all() count = users.count() message = 'Ading users to global and personal groups' for user in ProgressBar(users.iterator(), count, message): user.join_default_groups()
def forwards(self, orm): "Write your forwards methods here." profiles = orm['askbot.GroupProfile'].objects.all() items = profiles.iterator() count = profiles.count() message = 'Transfering group information from Tag to Group model' for profile in ProgressBar(items, count, message): group_tag = profile.group_tag group_name = group_tag.name.replace('-', ' ') group = orm['askbot.Group']() group.name = group_name group.logo_url = profile.logo_url group.moderate_email = profile.moderate_email group.is_open = profile.is_open group.preapproved_emails = profile.preapproved_emails group.preapproved_email_domains = profile.preapproved_email_domains try: #see if such group is already there auth_group = orm['auth.Group'].objects.get(name=group_name) group.group_ptr = auth_group except orm['auth.Group'].DoesNotExist: pass group.save() #update thread groups thread_groups = orm['askbot.ThreadToGroup'].objects thread_groups = thread_groups.filter(tag=group_tag) thread_groups.update(group=group) #update post groups post_groups = orm['askbot.PostToGroup'].objects post_groups = post_groups.filter(tag=group_tag) post_groups.update(group=group) #update user groups memberships = group_tag.user_memberships.all() for membership in memberships: membership.user.groups.add(group) db_engine_name = askbot.get_database_engine_name() if 'postgresql_psycopg2' in db_engine_name: from django.db import connection cursor = connection.cursor() cursor.execute( 'DROP TRIGGER IF EXISTS group_membership_tsv_update_trigger ' 'ON askbot_groupmembership' ) message = 'Deleting old group information' items = profiles.iterator() for profile in ProgressBar(items, count, message): group_tag = profile.group_tag group_tag.user_memberships.all().delete() profile.delete() #for postgresql setup new user full text search if 'postgresql_psycopg2' in db_engine_name: script_path = os.path.join( askbot.get_install_directory(), 'search', 'postgresql', 'user_profile_search_08312012.plsql' ) setup_full_text_search(script_path)
def import_threads(self, threads, tag_name): message = 'Importing threads for %s' % tag_name for thread in ProgressBar(iter(threads), len(threads), message): self.import_thread(thread, tag_name) transaction.commit()