async def _post(ctx, pitch_name: lower, finder: lower): interest = Interest.objects.filter(scope='P') interest = interest.filter(user=ctx.author.id) existing_interest = interest.filter(finder_iexact=finder) if existing_interest.exists(): # the user has already created an interest raise MultipleObjectsReturned elif interest.exists( ): # then we already have the users permission to post. new_interest = Interest.objects.create(scope='P', service='R', finder=finder, pitch=pitch_name) await ctx.send('Created {} for {}'.format(new_interest.__str__(), ctx.author.nick)) else: new_interest = Interest.objects.create(scope='P', service='R', finder=finder, pitch=pitch_name) s1 = 'This operation requires a reddit account and user token to work properly. ' s2 = 'Please follow [this link]({0})'.format( AdapterRedditClient.first_time_user(ctx, new_interest)) s3 = ', sign in and authorize REDcord to post on your behalf for this request.' await ctx.send('Created {} for {}\n\n{}{}{}'.format( new_interest.__str__(), ctx.author.nick, s1, s2, s3))
async def _watch(ctx, *, msg: str): msg = check_finder(msg) if Interest.objects.filter(scope='W').filter(finder__iexact=msg).exists(): raise MultipleObjectsReturned() else: r = AdapterRedditClient() i = Interest.objects.create(scope='W', service='R', finder=msg) await ctx.send('Created ' + i)
def auth(request): # is there variables state and code? if request.method == 'GET': data = {'state': request.GET['state'], 'code': request.GET['code']} if State.objects.filter(state__exact=data['state']).exist(): state = State.objects.get(state__exact=data['state']) token = AdapterRedditClient.refresh_token(data['code']) Token.objects.create(interest=state.interest, user=state.user, token=token) state.delete() return render(request, 'rc/auth.html', data) else: raise Http404( 'Response not recognized. Please try your command again.')
def format_post(param: Post): r = AdapterRedditClient() return '{}{}{}'.format(param.author, param.title, param.link) + r.get_post( param.id)
def test_gen_state(self): state = AdapterRedditClient.gen_state() self.assertTrue(len(state) == 40)
def test_reddit_auth_unknown(self): r = AdapterRedditClient(user='******') self.assertTrue(isinstance(r, AdapterRedditClient)) self.assertEqual(r.a, 'ReadOnly')
def test_reddit_auth_known(self): self.authorized() r = AdapterRedditClient(user='******') self.assertTrue(isinstance(r, AdapterRedditClient)) self.assertEqual(r.a.token, self.refresh_token())
def test_reddit_readonly(self): r = AdapterRedditClient() self.assertTrue(isinstance(r, AdapterRedditClient)) self.assertEqual(r.a, 'ReadOnly')