Skip to content

Commit

Permalink
automatic ordering through api
Browse files Browse the repository at this point in the history
  • Loading branch information
Mahmoud committed Dec 17, 2023
1 parent e423fc1 commit 45c14f6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 43 deletions.
12 changes: 10 additions & 2 deletions cookbook/views/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -651,15 +651,23 @@ def destroy(self, *args, **kwargs):
content = {'error': True, 'msg': e.args[0]}
return Response(content, status=status.HTTP_403_FORBIDDEN)


import json
class RecipeBookViewSet(viewsets.ModelViewSet, StandardFilterMixin):
queryset = RecipeBook.objects
serializer_class = RecipeBookSerializer
permission_classes = [(CustomIsOwner | CustomIsShared) & CustomTokenHasReadWriteScope]

def get_queryset(self):
order_field = self.request.GET.get('order_field')
order_direction = self.request.GET.get('order_direction')

if not order_field:
order_field = 'id'

ordering = f"{'' if order_direction == 'asc' else '-'}{order_field}"

self.queryset = self.queryset.filter(Q(created_by=self.request.user) | Q(shared=self.request.user)).filter(
space=self.request.space).distinct()
space=self.request.space).distinct().order_by(ordering)
return super().get_queryset()


Expand Down
63 changes: 22 additions & 41 deletions vue/src/apps/CookbookView/CookbookView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
<b-button variant="primary" v-b-tooltip.hover :title="$t('Create')" @click="createNew">
<i class="fas fa-plus"></i>
</b-button>
<b-dropdown variant="primary" id="sortDropDown" :text= dropdown_text class="border-left">
<b-dropdown-item @click = "sortOldest" v-show="showOtN">oldest to newest</b-dropdown-item>
<b-dropdown-item @click = "sortNewest" v-show="showNtO">newest to oldest</b-dropdown-item>
<b-dropdown-item @click = "sortAlphabetical" v-show="showAlp">alphabetical order</b-dropdown-item>
<b-dropdown-item @click = " enableSortManually" v-show="showMan">manually</b-dropdown-item>
<b-dropdown variant="primary" id="sortDropDown" text="Order By" class="border-left">
<b-dropdown-item @click = "orderBy('id','asc')" :disabled= "isActiveSort('id','asc')">oldest to newest</b-dropdown-item>
<b-dropdown-item @click = "orderBy('id','desc')" :disabled= "isActiveSort('id','desc')">newest to oldest</b-dropdown-item>
<b-dropdown-item @click = "orderBy('name','asc')" :disabled= "isActiveSort('name','asc')">alphabetical order</b-dropdown-item>
<b-dropdown-item @click = " enableSortManually" :disabled= "isActiveSort('name','asc')" >manually</b-dropdown-item>
</b-dropdown>
</b-input-group-append>
<b-button class= "ml-2" variant="primary" v-show="!showMan" @click="submitManualChanging">
Expand Down Expand Up @@ -114,10 +114,8 @@ export default {
current_book: undefined,
loading: false,
search: "",
dropdown_text: "Sort by: oldest to newest",
showOtN: false,
showNtO: true,
showAlp: true,
activeSortField : 'id',
activeSortDirection: 'asc',
showMan: true,
md: 12,
inputValue: "",
Expand All @@ -139,7 +137,7 @@ export default {
methods: {
refreshData: function () {
let apiClient = new ApiApiFactory()
apiClient.listRecipeBooks().then((result) => {
this.cookbooks = result.data
})
Expand Down Expand Up @@ -197,41 +195,24 @@ export default {
}
})
},
sortAlphabetical: function(){
this.dropdown_text = "Sort by: alphabetical order"
this.cookbooks = this.cookbooks.sort((a, b) => a.name.localeCompare(b.name))
this.showAlp= false
this.showNtO = true
this.showOtN = true
this.showMan = true
this.submitManual = false
this.md = 12
},
sortNewest: function(){
this.dropdown_text = "Sort by: newest to oldest"
this.cookbooks = this.cookbooks.sort((a, b) => b.id - a.id);
this.showNtO = false
this.showAlp= true
this.showOtN = true
this.showMan = true
this.submitManual = false
this.md = 12
orderBy: function(order_field,order_direction){
let apiClient = new ApiApiFactory()
const options = {
order_field: order_field,
order_direction: order_direction
}
this.activeSortField = order_field
this.activeSortDirection = order_direction
apiClient.listRecipeBooks(options).then((result) => {
this.cookbooks = result.data
})
},
sortOldest: function(){
this.dropdown_text = "Sort by: oldest to newest"
this.cookbooks = this.cookbooks.sort((a, b) => a.id - b.id)
this.showOtN= false
this.showAlp= true
this.showNtO = true
this.showMan = true
this.submitManual = false
this.md = 12
isActiveSort: function(field, direction) {
// Check if the current item is the active sorting option
return this.activeSortField === field && this.activeSortDirection === direction;
},
enableSortManually: function(){
console.log(1)
this.synchroniseLocalToDatabase();
console.log(2)
if (localStorage.getItem('cookbooks') ){
this.cookbooks = JSON.parse(localStorage.getItem('cookbooks'))
}
Expand Down
7 changes: 7 additions & 0 deletions vue/src/utils/openapi/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8985,6 +8985,13 @@ export const ApiApiAxiosParamCreator = function (configuration?: Configuration)
const localVarQueryParameter = {} as any;


if (options.order_field !== undefined) {
localVarQueryParameter['order_field'] = options.order_field;
}

if (options.order_direction!== undefined) {
localVarQueryParameter['order_direction'] = options.order_direction;
}

setSearchParams(localVarUrlObj, localVarQueryParameter, options.query);
let headersFromBaseOptions = baseOptions && baseOptions.headers ? baseOptions.headers : {};
Expand Down

0 comments on commit 45c14f6

Please sign in to comment.