Bhupesh-V / tutorialdb

A search 🔎 engine for programming/dev tutorials,
MIT License
124 stars 62 forks source link

Remove duplicate code from app & api #8

Open Bhupesh-V opened 5 years ago

Bhupesh-V commented 5 years ago

Currently the implementation of updating the db is almost the same in app & api

def post(self, request):
        linkCount = tutorial.objects.filter(link = request.POST['tlink']).count()
        if linkCount == 0:
            tags, title = generateTags(request.POST['tlink'])
            if 'other' in tags:
                return render(request, 'contribute.html', {'error': "Not a Tutorial Link, Try Again"})
            else:
                tutorialObject = tutorial.objects.create(
                    title = title, 
                    link = request.POST['tlink'], 
                    category = request.POST['tcategory']
                )
                for t in tags:
                    obj, created = tag.objects.get_or_create(name=t)

                tagObjList = tag.objects.filter(name__in = tags)
                tutorialObject.tags.set(tagObjList)

A possible solution is to shift this post method in serializers.py.

siarheipuhach commented 4 years ago

Please check my PR for that issue. https://github.com/Bhupesh-V/tutorialdb/pull/73