Fix user conflict on forum import

This commit is contained in:
rubenwardy 2020-12-04 03:08:54 +00:00
parent 8c5cdb630e
commit 14f643592c
1 changed files with 23 additions and 9 deletions

View File

@ -15,7 +15,7 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
import json, re import json, re, sys
from app.models import * from app.models import *
from app.tasks import celery from app.tasks import celery
from .phpbbparser import getProfile, getTopicsFromForum from .phpbbparser import getProfile, getTopicsFromForum
@ -61,6 +61,7 @@ def checkForumAccount(username, forceNoSave=False):
return needsSaving return needsSaving
@celery.task() @celery.task()
def checkAllForumAccounts(forceNoSave=False): def checkAllForumAccounts(forceNoSave=False):
needsSaving = False needsSaving = False
@ -128,21 +129,34 @@ def importTopicList():
for topic in ForumTopic.query.all(): for topic in ForumTopic.query.all():
topics_by_id[topic.topic_id] = topic topics_by_id[topic.topic_id] = topic
def get_or_create_user(username):
user = username_to_user.get(username)
if user:
return user
user = User.query.filter_by(forums_username=username).first()
if user is None:
user = User.query.filter_by(username=username).first()
if user:
return None
user = User(username)
user.forums_username = username
db.session.add(user)
username_to_user[username] = user
return user
# Create or update # Create or update
for info in info_by_id.values(): for info in info_by_id.values():
id = int(info["id"]) id = int(info["id"])
# Get author # Get author
username = info["author"] username = info["author"]
user = username_to_user.get(username) user = get_or_create_user(username)
if user is None: if user is None:
user = User.query.filter_by(forums_username=username).first() print("Error! Unable to create user {}".format(username), file=sys.stderr)
if user is None: continue
print(username + " not found!")
user = User(username)
user.forums_username = username
db.session.add(user)
username_to_user[username] = user
# Get / add row # Get / add row
topic = topics_by_id.get(id) topic = topics_by_id.get(id)