def sync(request, syncid): errors = [] try: sync = Sync.objects.get(pk=syncid) except Sync.DoesNotExist: return HttpResponseNotFound() sn_types = SNType.objects.filter(read_only=False) if request.POST: sync.title = request.POST['synctitle'] sync.save() for var in request.POST.keys(): test = re.findall("sync_target_id_(\d+)", var) if test and test[0] and request.POST[var]: pp = PostPlace.objects.get(pk=test[0]) pp.userid = request.POST[var] pp.save() for var in request.POST.keys(): test = re.findall("userid_(\d+)", var) if test and test[0] and request.POST[var]: pp = PostPlace.objects.get(pk=test[0]) pp.userid = request.POST[var] pp.save() if sync.source.sn_type.code == 'vk': vk = VK(vk_settings) groups = vk.VKGetGroups(sync.source.access_token) if 'error' in groups: raise Exception, groups['error']['error_msg'] if 'response' in groups: groups = groups['response'] sync.source.sources = [] for i in range(1, len(groups)): group = groups[i] if group['is_admin']: sync.source.sources.append({'id': group['gid'], 'name': group['name']}) if sync.source.sn_type.code == 'fb': fb_settings['redirect_uri'] = "%s%s?src=fb" % (HTTP_HOST, reverse('my.views.new')) fb = FB(fb_settings, code=sync.source.access_token) sync.source.sources = [] try: fb.login() groups = fb.getGroups() pages = fb.getPages() except Exception: pass if groups: for group in groups: if 'administrator' in group: sync.source.sources.append({'id': int(group['id']), 'name': 'Группа - ' + group['name']}) if pages: for page in pages: sync.source.sources.append({'id': int(page['id']), 'name': 'Страницы - ' + page['name']}) # for group in groups: # if 'administrator' in group: # sync.source.sources.append({'id': int(group['id']), 'name': group['name']}) destination = None destinations_vk = [] destinations_fb = [] fields = None if 'destination' in request.GET: destination = request.GET['destination'] if destination == 'vk': if request.POST and 'token' in request.POST: pp = PostPlace() pp.sn_type = SNType.objects.get(code='vk') pp.user = request.user pp.access_token = request.POST['token'] pp.save() sync.destination.add(pp) sync.save() return HttpResponseRedirect(reverse('my.views.sync', args=[sync.id])) fields = [ {'name': 'vkauth', 'type': 'button', 'size': 5, 'value': 'Авторизация через сайт Vkontakte', 'onclick': 'vkontakteauth(\'%s%s\');' % (HTTP_HOST, reverse('my.views.syncvk', args=[sync.id]))}, ] if destination == 'twitter': tw = Tweet(twitter_settings) register = tw.register( callbackurl="%s%s" % (HTTP_HOST, reverse('my.views.synctwitter', args=[sync.id]))) request.session['oauth_token_secret'] = register['data']['oauth_token_secret'] request.session['oauth_token'] = register['data']['oauth_token'] fields = [ {'name': 'twauth', 'type': 'button', 'size': 5, 'value': 'Авторизоваться через Twitter', 'onclick': 'twitterauth(\'%s\', \'%s\');' % ( reverse('my.views.synctwitter', args=[sync.id]), register['url'])}, ] if destination == 'fb': fb_settings['redirect_uri'] = "%s%s" % (HTTP_HOST, reverse('my.views.syncfacebook', args=[sync.id])) fb = FB(fb_settings) url = fb.register() fields = [ {'name': 'fbauth', 'type': 'button', 'size': 5, 'value': 'Авторизоваться через Facebook', 'onclick': 'facebookauth(\'%s\');' % url}, ] #livejournal if destination == 'lj': if request.POST: pp = PostPlace() pp.sn_type = SNType.objects.get(code='lj') pp.user = request.user pp.login = request.POST['user'] pp.password = md5.md5(request.POST['password']).hexdigest() pp.save() sync.destination.add(pp) sync.save() return HttpResponseRedirect(reverse('my.views.sync', args=[sync.id])) fields = [ {'name': 'message', 'type': 'message', 'message': lj_notice}, {'name': 'user', 'type': 'text', 'size': 50, 'label': 'Lj user'}, {'name': 'password', 'type': 'password', 'size': 50, 'label': 'Password'}, {'name': 'feedsave', 'type': 'submit', 'value': 'Продолжить'}, ] if sync.destination.all(): for dest in sync.destination.all(): new_sn_types = [] for sn_type in sn_types: if sn_type != dest.sn_type: new_sn_types.append(sn_type) sn_types = new_sn_types if dest.sn_type.code == 'vk': req = "https://api.vkontakte.ru/method/groups.get?access_token=%s&extended=1" % dest.access_token resp = urllib2.urlopen(req) groups = json.loads(resp.read()) if 'error' in groups: errors.append({'dest_id': dest.id, 'message': groups['error']['error_msg']}) sync.destination.remove(dest) if 'response' in groups: groups = groups['response'] for i in range(1, len(groups)): group = groups[i] if group['is_admin']: destinations_vk.append({'id': group['gid'], 'name': group['name']}) if dest.sn_type.code == 'fb': groups = None pages = None fb_settings['redirect_uri'] = "%s%s" % (HTTP_HOST, reverse('my.views.syncfacebook', args=[sync.id])) fb = FB(fb_settings, code=dest.access_token) try: fb.login() groups = fb.getGroups() pages = fb.getPages() except Exception: pass if groups: for group in groups: if 'administrator' in group: destinations_fb.append({'id': int(group['id']), 'name': 'Группа - ' + group['name']}) if pages: for page in pages: destinations_fb.append({'id': int(page['id']), 'name': 'Страница - ' + page['name']}) return {'sync': sync, 'sn_types': sn_types, 'destination': destination, 'fields': fields, 'destinations_vk': destinations_vk, 'destinations_fb': destinations_fb, 'errors': errors}