def update(request, space_name): context = {} try: space = Space.objects.get(code=hashing_into_int(space_name)) if request.user == space.owner: tab = request.GET.get('tab', 'information') tab = tab.lower() if tab == 'banner': banners = Banner.objects.filter(space_id=space.id) context['banners'] = banners token = get_api_token(request) context['token'] = token elif tab == 'logo': token = get_api_token(request) context['token'] = token else: tab = 'information' if request.method == 'POST': form = SpaceUpdateForm(request.POST, space=space) if form.is_valid(): space = form.save() return redirect('/'+space.name+'/') else: form = SpaceUpdateForm(space=space) context['form'] = form context['tab'] = tab context['space'] = space return render(request, 'space/manage/update.html', context) except ObjectDoesNotExist as e: pass return invalid_request(request, context)
def save(self, commit=True): self.space.name = self.cleaned_data['name'] self.space.code = hashing_into_int(self.cleaned_data['name']) self.space.display_name = self.cleaned_data['display_name'] self.space.description = self.cleaned_data['description'] if commit: self.space.save() return self.space
def save(self, commit=True): space = super(SpaceCreateForm, self).save(commit=False) space.code = hashing_into_int(space.name) space.owner = self.request.user space.logo = self.logo_path if commit: space.save() banner1 = Banner(space=space) banner2 = Banner(space=space) banner3 = Banner(space=space) banner1.save() banner2.save() banner3.save() return space
def __space_product(space_name, **kwargs): space = None try: space = Space.objects.get(code=hashing_into_int(space_name)) except ObjectDoesNotExist as e: return None limit = kwargs.get('limit', None) page = kwargs.get('page', None) if page is None: return None offset = page * PRODUCT_PAGINATION_SIZE result = Product.objects.filter(space_id=space.id)[offset:offset + PRODUCT_PAGINATION_SIZE] serializer = ProductSerializer(result, many=True) return serializer
def handle_favorite(user, name, req): if not user.is_authenticated: return False try: space = Space.objects.get(code=hashing_into_int(name)) if space.owner_id == user.id: return False req = req.strip() req = req.upper() if req == 'ADD' or req == 'REMOVE': try: row = Favorite.objects.get(user_id=user.id, space_id=space.id) except ObjectDoesNotExist as e: row = None status = Status.objects.get(space_id=space.id) if req == 'ADD': if row is None: row = Favorite(user_id=user.id, space_id=space.id) row.save() status.total_favorite += 1 status.rating = _addition(status.rating, ACTIVITY_POINT['FAVORITE']) status.save() return True else: if row is not None: row.delete() status.total_favorite -= 1 status.rating = _addition(status.rating, -ACTIVITY_POINT['FAVORITE']) status.save() return True except ObjectDoesNotExist as e: pass return False
def clean_name(self): name = self.cleaned_data['name'] __name = name.lower() if 'sakkhat' in __name: raise forms.ValidationError('Restricted Name') for i in _UNUSABLE_SYMBOLS: if i in name: if i == ' ': raise forms.ValidationError( 'blank space is not allowed in name') else: raise forms.ValidationError(i + " is invalid") for i in _UNUSABLE_NAMES: if i == __name: raise forms.ValidationError('Restricted Name') query = Space.objects.filter(code=hashing_into_int(name)).first() if query: raise forms.ValidationError('Space name already taken') return name
def index(request, space_name): try: space = Space.objects.get(code=hashing_into_int(space_name)) status = Status.objects.get(space_id=space.id) banners = Banner.objects.filter(space_id=space.id) token = get_api_token(request) context = {} context['space'] = space context['banners'] = banners context['status'] = status context['has_favorite'] = False context['token'] = token try: favorite = Favorite.objects.get(user_id=request.user.id, space_id=space.id) context['has_favorite'] = True except ObjectDoesNotExist as e: pass return render(request, 'space/manage/index.html', context) except ObjectDoesNotExist as e: return invalid_request(request)