role_id and topic relations fixes

This commit is contained in:
2021-08-20 12:27:19 +03:00
parent ee3b186ba1
commit 3075dbb64b
19 changed files with 592 additions and 12 deletions

View File

@@ -0,0 +1 @@
__all__ = ["users"]

View File

@@ -0,0 +1,36 @@
from html2md import Converter
import datetime
markdown = Converter()
def migrate(entry):
```
# is comment
type Shout {
org: String!
slug: String!
author: Int!
body: String!
createdAt: DateTime!
updatedAt: DateTime!
deletedAt: DateTime
deletedBy: Int
rating: Int
published: DateTime # if there is no published field - it is not published
replyTo: String # another shout
tags: [String] # actual values
topics: [String] # topic-slugs
title: String
versionOf: String
visibleForRoles: [String] # role ids are strings
visibleForUsers: [Int]
}
```
# TODO: implement comments migration
return {
'org': 'discours.io',
'slug': entry['slug'],
'createdAt': entry['createdAt'],
'body': markdown(entry['body']),
'replyTo': entry['']
}

View File

@@ -0,0 +1,19 @@
def migrate(entry):
```
type Topic {
slug: String! # ID
createdBy: Int! # User
createdAt: DateTime!
value: String
parents: [String] # NOTE: topic can have parent topics
children: [String] # and children
}
```
return {
'slug': entry['slug'],
'createdBy': entry['createdBy'], # NOTE: uses an old user id
'createdAt': entry['createdAt'],
'value': entry['title'].lower(),
'parents': [],
'children': []
}

View File

@@ -0,0 +1,86 @@
from migration.html2md import Converter
from dateutil.parser import parse
from os.path import abspath
import json
from orm import Shout
users_dict = json.loads(open(abspath('migration/data/users.dict.json')).read())
users_dict['0'] = {'id': 99999 }
markdown = Converter()
type2layout = {
'Article': 'article',
'Literature': 'prose',
'Music': 'music',
'Video': 'video',
'Image': 'image'
}
def migrate(entry):
'''
type Shout {
org_id: Int!
slug: String!
author: Int!
body: String!
createdAt: DateTime!
updatedAt: DateTime!
deletedAt: DateTime
deletedBy: Int
rating: Int
ratigns: [Rating]
published: Bool!
publishedAt: DateTime # if there is no published field - it is not published
replyTo: String # another shout
tags: [String] # actual values
topics: [String] # topic-slugs, order has matter
title: String
versionOf: String
visibleForRoles: [String] # role ids are strings
visibleForUsers: [Int]
views: Int
}
'''
r = {
'org_id': 0,
'layout': type2layout[entry['type']],
'title': entry['title'],
'authors': [ users_dict[entry['createdBy']]['id'], ],
'topics': [],
'published': entry['published'],
'views': entry['views'],
'rating': entry['rating'],
'ratings': []
}
r['slug'] = entry.get('slug')
if not r['slug'] and entry.get('friendlySlugs') is not None:
r['slug'] = entry['friendlySlugs']['slug'][0]['slug']
if(r['slug'] is None):
r['slug'] = entry['friendlySlugs'][0]['slug']
if entry.get('image') is not None:
r['cover'] = entry['image']['url']
elif entry.get('thumborId') is not None:
r['cover'] = 'https://discours.io/' + entry['thumborId']
if entry.get('publishedAt') is not None:
r['publishedAt'] = entry['publishedAt']
if entry.get('createdAt') is not None:
r['createdAt'] = entry['createdAt']
if entry.get('updatedAt') is not None:
r['updatedAt'] = entry['updatedAt']
if entry.get('type') == 'Literature':
r['body'] = entry['media'][0]['literatureBody']
elif entry.get('type') == 'Video':
r['body'] = '<ShoutVideo src=\"' + entry['media'][0]['youtubeId'] + '\" />'
elif entry.get('type') == 'Music':
r['body'] = '<ShoutMusic media={\"' + json.dumps(entry['media']) +'\"} />'
else entry.get('type') == 'Image':
r['body'] = r['body']
else:
r['body'] = '## ' + r['title']
# TODO: compile md with graymatter
open('migration/content/' + r['slug'] + '.md', 'w').write(mdfile)
shout = Shout.create(**r.copy())
r['id'] = shout['id']
return r

View File

@@ -0,0 +1,2 @@
def migrate(entry):
return entry

20
migration/tables/tags.py Normal file
View File

@@ -0,0 +1,20 @@
def migrate(entry):
```
type Topic {
slug: String! # ID
createdBy: Int! # User
createdAt: DateTime!
value: String
parents: [String] # NOTE: topic can have parent topics
children: [String] # and children
}
```
creator = get_new_user_id(entry['cratedBy'])
return {
'slug': entry['slug'],
'createdBy': creator_id, # NOTE: uses an old user id
'createdAt': entry['createdAt'],
'value': entry['value'].lower(),
'parents': [],
'children': []
}

79
migration/tables/users.py Normal file
View File

@@ -0,0 +1,79 @@
from orm import User
from dateutil.parser import parse
counter = 0
def migrate(entry):
'''
type User {
username: String! # email
createdAt: DateTime!
email: String
password: String
oauth: String # provider:token
viewname: String # to display
userpic: String
links: [String]
emailConfirmed: Boolean # should contain all emails too
id: Int!
muted: Boolean
rating: Int
roles: [Role]
updatedAt: DateTime
wasOnlineAt: DateTime
ratings: [Rating]
slug: String
bio: String
notifications: [Int]
}
'''
res = {}
try:
res['old_id'] = entry['_id']
res['password'] = entry['services']['password'].get('bcrypt', '')
res['username'] = entry['emails'][0]['address']
res['email'] = res['username']
res['wasOnlineAt'] = parse(entry.get('loggedInAt', entry['createdAt']))
res['emailConfirmed'] = entry['emails'][0]['verified']
res['createdAt'] = parse(entry['createdAt'])
res['rating'] = entry['rating'] # number
res['roles'] = [] # entry['roles'] # roles without org is for discours.io
res['ratings'] = [] # entry['ratings']
res['notifications'] = []
res['links'] = []
res['muted'] = False
res['viewname'] = 'anonymous'
if entry['profile']:
res['slug'] = entry['profile'].get('path')
res['userpic'] = entry['profile'].get('image', {'url': ''}).get('url', '')
viewname = entry['profile'].get('firstName', '') + ' ' + entry['profile'].get('lastName', '')
viewname = entry['profile']['path'] if len(viewname) < 2 else viewname
res['viewname'] = viewname
fb = entry['profile'].get('facebook', False)
if fb:
res['links'].append(fb)
vk = entry['profile'].get('vkontakte', False)
if vk:
res['links'].append(vk)
tr = entry['profile'].get('twitter', False)
if tr:
res['links'].append(tr)
ws = entry['profile'].get('website', False)
if ws:
res['links'].append(ws)
if not res['slug']:
res['slug'] = res['links'][0].split('/')[-1]
if not res['slug']:
res['slug'] = res['email'].split('@')[0]
except Exception:
print(entry['profile'])
raise Exception
else:
old = res['old_id']
del res['old_id']
user = User.create(**res.copy())
res['id'] = user.id
res['old_id'] = old
return res