def import_users(f): def convert_username(name): """Convert the username to a naxos-db compliant format""" return HTMLParser().unescape(name.replace(' ', '_'))[:30] s = fix_json(f) users = json.loads(s) n = len(users) new_users = {} # Here will be stored usernames & passwords for i, user in enumerate(users): print("Creating users... {}/{}".format(i + 1, n), end=" " * 20 + "\r") user['login'] = convert_username(user['login']) # Skip if pk is taken (user likely exists already) m = ForumUser.objects.filter(pk=user['userid']).first() if m: continue password = keygen() # generate password new_users[user['login']] = password # Store username & password # Create users u = ForumUser.objects.create( pk=int(user['userid']), username=user['login'], email=HTMLParser().unescape(user['usermail']), date_joined=datetime.fromtimestamp(user['registerdate']), logo="logo/{}".format(user['userlogo']), quote=HTMLParser().unescape(str(user['usercitation'])), website=HTMLParser().unescape(str(user['usersite']))) u.set_password(password) u.save() # Write username & passwords into a file with open('new_users.json', 'a') as f: json.dump(new_users, f)
def import_threads(f): cat_map = {1: 1, 2: 2, 3: 3, 4: 4, 5: 6, 6: 7, 7: 5} # mapping categories threads = json.loads(fix_json(f)) # loading threads existing_threads = {} for thread in Thread.objects.iterator(): existing_threads[thread.pk] = thread.category_id # prepare tokens for threads tokens = set() print("Creating tokens...", end='\r') while len(tokens) != len(threads): tokens.add(keygen()) # Prepare bulk bulk = [] for i, thread in enumerate(threads): print("Preparing threads... {}/{}".format(i + 1, len(threads)), end='\r') if thread['idtopic'] in existing_threads: continue isSticky = True if thread['postit'] == 1 else False bulk.append( Thread( pk=int(thread['idtopic']), category=Category.objects.get(pk=cat_map[thread['idforum']]), title=HTMLParser().unescape(str(thread['sujet']))[:80], author=ForumUser.objects.get(pk=thread['idmembre']), icon=str(thread['icone']) + '.gif', viewCount=int(thread['nbvues']), isSticky=isSticky, cessionToken=tokens.pop())) print("Creating threads in the database...", end='\r') if bulk: Thread.objects.bulk_create(bulk)
def import_threads(f): cat_map = {1: 1, 2: 2, 3: 3, 4: 4, 5: 6, 6: 7, 7: 5} # mapping categories threads = json.loads(fix_json(f)) # loading threads existing_threads = {} for thread in Thread.objects.iterator(): existing_threads[thread.pk] = thread.category_id # prepare tokens for threads tokens = set() print("Creating tokens...", end='\r') while len(tokens) != len(threads): tokens.add(keygen()) # Prepare bulk bulk = [] for i, thread in enumerate(threads): print("Preparing threads... {}/{}".format(i + 1, len(threads)), end='\r') if thread['idtopic'] in existing_threads: continue isSticky = True if thread['postit'] == 1 else False bulk.append(Thread( pk=int(thread['idtopic']), category=Category.objects.get(pk=cat_map[thread['idforum']]), title=HTMLParser().unescape(str(thread['sujet']))[:80], author=ForumUser.objects.get(pk=thread['idmembre']), icon=str(thread['icone']) + '.gif', viewCount=int(thread['nbvues']), isSticky=isSticky, cessionToken=tokens.pop())) print("Creating threads in the database...", end='\r') if bulk: Thread.objects.bulk_create(bulk)
def save(self, *args, **kwargs): queryset = self.__class__.objects.all() self.token = keygen() while queryset.filter(token=self.token).exists(): self.token = keygen() super().save(*args, **kwargs)