import base64 from odoo import http from odoo.http import request MAX_IMAGES = 3 def _is_module_installed(env, module_name): return bool(env['ir.module.module'].sudo().search_count( [('name', '=', module_name), ('state', '=', 'installed')] )) def _has_active_membership(partner): """Soft-check: only meaningful if community_membership is installed.""" if 'membership_state' not in partner._fields: return True return partner.membership_state in ('active', 'renewal_due') class ClassifiedsController(http.Controller): @http.route(['/classifieds'], type='http', auth='public', website=True, sitemap=True) def classifieds_list(self, category=None, **kwargs): domain = [('state', '=', 'published')] if category: domain.append(('category', '=', category)) listings = request.env['community.classified'].sudo().search(domain) return request.render('community_classifieds.classifieds_list_page', { 'listings': listings, 'category': category, }) @http.route(['/classifieds/'], type='http', auth='public', website=True, sitemap=False) def classifieds_detail(self, classified_id, **kwargs): listing = request.env['community.classified'].sudo().browse(classified_id) if not listing.exists() or listing.state != 'published': return request.not_found() listing._increment_view_count() return request.render('community_classifieds.classifieds_detail_page', {'listing': listing}) @http.route(['/classifieds/new'], type='http', auth='user', website=True) def classifieds_new(self, **kwargs): partner = request.env.user.partner_id if _is_module_installed(request.env, 'community_membership') and not _has_active_membership(partner): return request.render('community_classifieds.classifieds_membership_required', {}) if request.httprequest.method == 'POST': image_ids = [] for field_name in ('image1', 'image2', 'image3'): upload = kwargs.get(field_name) if upload and getattr(upload, 'filename', None): image_ids.append((0, 0, {'image': base64.b64encode(upload.read())})) listing = request.env['community.classified'].sudo().create({ 'title': kwargs.get('title', '').strip(), 'category': kwargs.get('category', 'other'), 'description': kwargs.get('description', ''), 'contact_method': kwargs.get('contact_method', 'email'), 'contact_email': kwargs.get('contact_email', '').strip(), 'contact_phone': kwargs.get('contact_phone', '').strip(), 'poster_partner_id': partner.id, 'image_ids': image_ids[:MAX_IMAGES], }) return request.redirect(f'/classifieds/my?posted={listing.id}') return request.render('community_classifieds.classifieds_new_page', {}) @http.route(['/classifieds/my'], type='http', auth='user', website=True) def classifieds_my(self, **kwargs): partner = request.env.user.partner_id listings = request.env['community.classified'].sudo().search([('poster_partner_id', '=', partner.id)]) return request.render('community_classifieds.classifieds_my_page', {'listings': listings}) @http.route(['/classifieds//renew'], type='http', auth='user', website=True) def classifieds_renew(self, classified_id, **kwargs): partner = request.env.user.partner_id listing = request.env['community.classified'].sudo().search([ ('id', '=', classified_id), ('poster_partner_id', '=', partner.id), ], limit=1) if listing: listing.action_renew() return request.redirect('/classifieds/my')