"""Import Users and links from old massive version using mongoDB.

Usage:
    mongoToSqlite.py [(<links_path> <users_path>)]
"""

import json

from docopt import docopt
from massive import db, models
from massive.views import save_link
from pony.orm import db_session


if __name__ == '__main__':
    arguments = docopt(__doc__)

    list_users = {}

    with open(arguments['<users_path>']) as users_file:
        for file_line in users_file:

            line = json.loads(file_line)

            with db_session:
                user = models.Users.get(login=line["login"])

                if not user:
                    user = models.Users(
                        login=line["login"],
                        password=line["password"])

                list_users[line["_id"]["$oid"]] = user

    with open(arguments['<links_path>']) as links_file:
        for file_line in links_file:
            
            line = json.loads(file_line)

            with db_session:
                save_link(
                        line["title"],
                        line["url"],
                        line["tags"],
                        None,
                        list_users[line["user"]["$oid"]]
                        )



@@ -963,32 +963,123 @@
 ith db_session:%0A
+                link = models.Users.get(url=line%5B%22url%22%5D)%0A%0A                if not link:%0A    
                 
@@ -1117,23 +1117,40 @@
     
-line%5B%22title%22%5D,%0A
+    line.get(%22title%22,None),%0A    
     
@@ -1177,17 +1177,30 @@
 line
-%5B%22url%22%5D,%0A
+.get(%22url%22,None),%0A    
     
@@ -1227,18 +1227,31 @@
 line
-%5B
+.get(
 %22tags%22
-%5D,%0A
+,None),%0A    
     
@@ -1276,16 +1276,20 @@
   None,%0A
+    
         
@@ -1337,16 +1337,20 @@
 $oid%22%5D%5D%0A
+    
         

fix script import