示例#1
0
# Set up parameters
mailtoENParamConfig1 = new hubConfigurationParameter(user = request.user, configuration = mailtoENConfig, value = mailtoENparamGmail.value)
mailtoENParamConfig2 = new hubConfigurationParameter(user = request.user, configuration = mailtoENConfig, value = mailtoENparamEvernote.value)
mailtoENParamConfig3 = new hubConfigurationParameter(user = request.user, configuration = mailtoENConfig, value = mailtoENparamGCal.value)
# Connection config from template
mailtoENGmailConfig = new hubConfigurationGmail(user = request.user, 
                                                configuration = mailtoENConfig, 
                                                find_rule = configGmail.find_rule, 
                                                create_to_rule = configGmail.create_to_rule,
                                                create_from_rule =  configGmail.create_from_rule
                                                update_to_rule = configGmail.update_to_rule
                                                update_from_rule = configGmail.update_from_rule,
                                                remove_rule = configGmail.remove_rule
                                                )
# Connection config from template
mailtoENEvernoteConfig = new hubConfigurationEvernote(user = request.user, 
                                                      configuration = mailtoENConfig, 
                                                      find_rule = configGCal.find_rule, 
                                                      create_to_rule = configGCal.create_to_rule,
                                                      create_from_rule =  configGCal.create_from_rule
                                                      update_to_rule = configGCal.update_to_rule
                                                      update_from_rule = configGCal.update_from_rule,
                                                      remove_rule = configGCal.remove_rule
                                                      )
# Connection config from template
mailtoENGCalConfig = new hubConfigurationEvernote(user = request.user, 
                                                      configuration = mailtoENConfig, 
                                                      find_rule = configEvernote.find_rule, 
                                                      create_to_rule = configEvernote.create_to_rule,
                                                      create_from_rule =  configEvernote.create_from_rule
                                                      update_to_rule = configEvernote.update_to_rule
示例#2
0
def index(request):
    user = request.user
    
    gcal_storage = Storage(GcalCredentials, 'id', user, 'credential')
    gcal_credential = gcal_storage.get()
    
    en_credentials = get_onetoone_or_none(user, 'evernotecredentials')
    
    gmail_storage = Storage(GmailCredentials, 'id', request.user, 'credential')
    gmail_credential = gmail_storage.get()
    
    if gcal_credential is None or gcal_credential.invalid == True:
        return redirect(reverse('gcal:index'))
    elif en_credentials is None:
        return redirect(reverse('evernote:auth'))
    elif gmail_credential is None or gmail_credential.invalid == True:
        return redirect(reverse('gmail:index'))
    else:
        conf = hubConfiguration.objects.filter(user=user).first()
        
        if not conf:
            new_conf = hubConfiguration(user=user, name="Dummy", active=True, content_layout="")
            new_conf.save()
            conf = hubConfiguration.objects.filter(user=user).first()
            
            new_gmail = hubConfigurationGmail(user=user, configuration=new_conf,
                                               find_rule="label:!Projekt/VLAB/Test",
                                               create_from_rule=repr([{'name': "name", 'type': "standard", 'rule': """<TMPL_var name="subject">"""},
                                                                      {'name': "content", 'type': "standard", 'rule': """<TMPL_var name="body">"""},
                                                                      {'name': "tags", 'type': "iterate", 'iterate_on': "tags",
                                                                       'rules': [{'name': "tag", 'type': "standard", 'rule': """<TMPL_if name="tags_tag" value="!Projekt/VLAB/Test"><TMPL_ELSE><TMPL_var name="tags_tag"></TMPL_IF>"""}]
                                                                       }]),
                                               create_to_rule="",
                                               update_from_rule=repr([{'name': "tags", 'type': "iterate", 'iterate_on': "tags",
                                                                       'rules': [{'name': "tag", 'type': "standard", 'rule': """<TMPL_if name="tags_tag" value="!Projekt/VLAB/Test"><TMPL_ELSE><TMPL_var name="tags_tag"></TMPL_IF>"""}]
                                                                       }]),
                                               update_to_rule=repr([{'name': "subject", 'type': "standard", 'rule': """<TMPL_var name="name">"""},
                                                                    {'name': "body", 'type': "standard", 'rule': """<TMPL_var name="content">"""},
                                                                    {'name': "tags", 'type': "iterate", 'iterate_on': "tags",
                                                                     'rules': [{'name': "tag", 'type': "standard", 'rule': """<TMPL_var name="tags_tag">"""}]
                                                                     },
                                                                    {'name': "tags", 'type': "standard", 'rule': "!Projekt/VLAB/Test"}
                                                                    ]),
                                               remove_rule="label:!Projekt/VLAB/Test"
                                               )
            
            new_en = hubConfigurationEvernote(user=user, configuration=new_conf,
                                               find_rule="notebook:TestBook",
                                               create_from_rule=repr([{'name': "name", 'type': "standard", 'rule': """<TMPL_var name="title">"""},
                                                                      {'name': "content", 'type': "standard", 'rule': """<TMPL_var name="content">"""},
                                                                      {'name': "tags", 'type': "iterate", 'iterate_on': "tags",
                                                                       'rules': [{'name': "tag", 'type': "standard", 'rule': """<TMPL_var name="tags_tag">"""}]
                                                                       }]),
                                               create_to_rule=repr([{'name': "title", 'type': "standard", 'rule': """<TMPL_var name="name">"""},
                                                                    {'name': "content", 'type': "standard", 'rule': """<TMPL_var name="content">"""},
                                                                    {'name': "tags", 'type': "iterate", 'iterate_on': "tags",
                                                                     'rules': [{'name': "tag", 'type': "standard", 'rule': """<TMPL_var name="tags_tag">"""}]
                                                                     },
                                                                    {'name': "notebookName", 'type': "standard", 'rule': """TestBook"""},
                                                                    ]),
                                               update_from_rule=repr([{'name': "name", 'type': "standard", 'rule': """<TMPL_var name="title">"""},
                                                                      {'name': "content", 'type': "standard", 'rule': """<TMPL_var name="content">"""},
                                                                      {'name': "tags", 'type': "iterate", 'iterate_on': "tags",
                                                                       'rules': [{'name': "tag", 'type': "standard", 'rule': """<TMPL_var name="tags_tag">"""}]
                                                                       }]),
                                               update_to_rule=repr([{'name': "title", 'type': "standard", 'rule': """<TMPL_var name="name">"""},
                                                                    {'name': "content", 'type': "standard", 'rule': """<TMPL_var name="content">"""},
                                                                    {'name': "tags", 'type': "iterate", 'iterate_on': "tags",
                                                                     'rules': [{'name': "tag", 'type': "standard", 'rule': """<TMPL_var name="tags_tag">"""}]
                                                                     },
                                                                    {'name': "notebookName", 'type': "standard", 'rule': """TestBook"""},
                                                                    ]),
                                               remove_rule="notebook:TestBook"
                                               )
            
            new_gcal = hubConfigurationGoogleCalendar(user=user, configuration=new_conf,
                                                       find_rule = "#test",
                                                       create_from_rule=repr([{'name': "name", 'type': "standard", 'rule': """<TMPL_var name="summary">"""},
                                                                              {'name': "content", 'type': "standard", 'rule': """<TMPL_var name="description">"""},
                                                                              {'name': "tags", 'type': "iterate", 'iterate_on': "tags",
                                                                               'rules': [{'name': "tag", 'type': "standard", 'rule': """<TMPL_var name="tags_tag">"""}]
                                                                               }]),
                                                       create_to_rule="",
                                                       update_from_rule=repr([{'name': "name", 'type': "standard", 'rule': """<TMPL_var name="summary">"""},
                                                                              {'name': "content", 'type': "standard", 'rule': """<TMPL_var name="description">"""},
                                                                              {'name': "tags", 'type': "iterate", 'iterate_on': "tags",
                                                                               'rules': [{'name': "tag", 'type': "standard", 'rule': """<TMPL_var name="tags_tag">"""}]
                                                                               }]),
                                                       update_to_rule=repr([{'name': "summary", 'type': "standard", 'rule': """<TMPL_var name="name">"""},
                                                                            {'name': "description", 'type': "standard", 'rule': """<TMPL_var name="content">"""},
                                                                            {'name': "tags", 'type': "iterate", 'iterate_on': "tags",
                                                                             'rules': [{'name': "tag", 'type': "standard", 'rule': """<TMPL_var name="tags_tag">"""}]
                                                                             }]),
                                                       remove_rule="#test"
                                                       )
            
            new_gmail.save()
            new_en.save()
            new_gcal.save()
        
            
        conf_gmail = conf.hubconfigurationgmail
        conf_en = conf.hubconfigurationevernote
        conf_gcal = conf.hubconfigurationgooglecalendar
            
        
        
        
#         rules = {'gmail_find': "label:!Projekt/VLAB/Test",
#                  'gmail_to_hub': [
#                                   {'name': "name", 'type': "standard", 'rule': """<TMPL_var name="subject">"""},
#                                   {'name': "content", 'type': "standard", 'rule': """<TMPL_var name="body">"""},
#                                   {'name': "tags", 'type': "iterate", 'iterate_on': "tags",
#                                    'rules': [{'name': "tag", 'type': "standard", 'rule': """<TMPL_if name="tags_tag" value="!Projekt/VLAB/Test"><TMPL_ELSE><TMPL_var name="tags_tag"></TMPL_IF>"""}]
#                                   }
#                                   ],
#                  'hub_to_en':     [
#                                    {'name': "title", 'type': "standard", 'rule': """<TMPL_var name="name">"""},
#                                    {'name': "content", 'type': "standard", 'rule': """<TMPL_var name="content">"""},
#                                    {'name': "notebookName", 'type': "standard", 'rule': """VLABnotebook"""},
#                                    {'name': "tags", 'type': "iterate", 'iterate_on': "tags",
#                                    'rules': [{'name': "tag", 'type': "standard", 'rule': """<TMPL_var name="tags_tag">"""}]
#                                   }
#                                    ],
#                  'hub_to_gcal':   [
#                                    {'name': "summary", 'type': "standard", 'rule': """<TMPL_var name="name">"""},
#                                    {'name': "description", 'type': "standard", 'rule': """<TMPL_var name="content">"""},
#                                    {'name': "tags", 'type': "iterate", 'iterate_on': "tags",
#                                    'rules': [{'name': "tag", 'type': "standard", 'rule': """<TMPL_var name="tags_tag">"""}]
#                                   }
#                                    ],
#                 }
        
        gcal_conn = gcalConnection(user, gcal_credential)
        en_conn = enConnection(user, en_credentials)
        gmail_conn = gmailConnection(user, gmail_credential)
        
        
        latest_update_server = gmail_conn.latest_update_status()
        userinfo = user.gmailuserinfo
        
        existing_objects = conf.hubobject_set
        existing_ids = existing_objects.values_list('hubobjectgmail__api_id', flat=True)
        
        results = gmail_conn.find_updated_excepted(conf_gmail.find_rule, updated_since=userinfo.api_latest_update, excepted_ids=existing_ids)
        userinfo.api_latest_update = latest_update_server + 1
        userinfo.save()
        
        print results
        
        hubs = []
        
        for result in results:
            message, gmail_last_update = gmail_conn.read_updated(result)
            
            #Now done by refined tag rule, and added functionality in yoursync_object_utils
            #tags = [x for x in message.content['tags'] if not ("!Projekt/VLAB/Test" == x.get('tag'))]        #Remove the !Projekt/VLAB/Test tag
            #message.content['tags'] = tags
            
            hub = Hub(message, eval(conf_gmail.create_from_rule))
            
            ennote = Note()
            ennote.update(hub, eval(conf_en.create_to_rule))
            en_id = en_conn.create(ennote)
            
#             gcalevent = Event()
#             gcalevent.update(hub, rules['hub_to_gcal'])
#             gcalevent.content['start'] = datetime.datetime.now()
#             gcalevent.content['end'] = datetime.datetime.now() + datetime.timedelta(hours=1)
#             gcal_conn.create(gcalevent)

            dbhub = hubObject(user=user, configuration=conf, latest_update_date=datetime.datetime.now())
            dbhub.save()
            dbgmail = hubObjectGmail(user=user, parent_hub_object=dbhub, api_id=result, api_latest_update=gmail_last_update, latest_update_date=datetime.datetime.now())
            dbgmail.save()
            dben = hubObjectEvernote(user=user, parent_hub_object=dbhub, api_id=en_id, api_latest_update=None, latest_update_date=datetime.datetime.now())
            dben.save()
            
            hubs.append(hub)
            
        return render_to_response('hub_test/index.html', {'hubs': hubs})