class ProductionSelection(StrAndUnicode): def __init__(self, id=None, title=None, byline_lookup=None, types_to_set=[]): self.id = id self.types_to_set = types_to_set if self.id: self.production = Production.objects.get(id=self.id) self.title = self.production.title self.byline = self.production.byline() self.byline_lookup = None else: self.production = None self.title = title self.byline_lookup = byline_lookup def commit(self): if not self.production: self.production = Production( title=self.title, updated_at=datetime.datetime.now(), ) # Ugh. We really ought to come up with a nice way of setting supertype # in advance of setting types, rather than having to save a second time # once the types are in place... self.production.save() self.production.types = self.types_to_set self.production.save() if self.byline: self.byline.commit(self.production) return self.production def __unicode__(self): return u"ProductionSelection: %s - %s" % (self.id, self.title) def __eq__(self, other): if not isinstance(other, ProductionSelection): return False return self.title == other.title and str(self.id) == str(other.id) and self.byline_lookup == other.byline_lookup def __ne__(self, other): return not self.__eq__(other) @staticmethod def from_value(value, types_to_set=[]): # value can be: # a Production # None # an integer (will be used as an ID) # an existing ProductionSelection if not value: return ProductionSelection() elif isinstance(value, ProductionSelection): return ProductionSelection(id=value.id, title=value.title, byline_lookup=value.byline_lookup, types_to_set=value.types_to_set) elif isinstance(value, int): return ProductionSelection(id=value) elif isinstance(value, Production): return ProductionSelection(id=value.id) else: raise Exception("Don't know how to convert %s to a ProductionSelection!" % repr(value))
def handle_production(prod_data, competition): if prod_data.get('id'): production = Production.objects.get(id=prod_data['id']) else: production = Production( release_date=competition.shown_date, updated_at=datetime.datetime.now(), has_bonafide_edits=False) production.save() # assign an ID so that associations work # can only edit production details if production is non-stable (which is always true for # newly-created ones) if not production.is_stable_for_competitions(): if 'title' in prod_data: production.title = prod_data['title'] if 'platform_id' in prod_data: if prod_data['platform_id']: production.platforms = [Platform.objects.get(id=prod_data['platform_id'])] else: production.platforms = [] if 'production_type_id' in prod_data: if prod_data['production_type_id']: production.types = [ProductionType.objects.get(id=prod_data['production_type_id'])] else: production.types = [] if 'byline' in prod_data: try: production.author_nicks = [ NickSelection(author['id'], author['name']).commit() for author in prod_data['byline']['authors'] ] production.author_affiliation_nicks = [ NickSelection(affillation['id'], affillation['name']).commit() for affillation in prod_data['byline']['affiliations'] ] production.unparsed_byline = None except NickSelection.FailedToResolve: # failed to match up the passed nick IDs to valid nick records. # Keep the passed names, as an unparsed byline author_names = [author['name'] for author in prod_data['byline']['authors']] affiliation_names = [affiliation['name'] for affiliation in prod_data['byline']['affiliations']] byline_string = ' + '.join(author_names) if affiliation_names: byline_string += ' / ' + ' ^ '.join(affiliation_names) production.unparsed_byline = byline_string production.author_nicks = [] production.author_affiliation_nicks = [] production.updated_at = datetime.datetime.now() production.supertype = production.inferred_supertype production.save() return production
def import_text(request, competition_id): if not request.user.is_staff: return redirect('competition_edit', args=[competition_id]) competition = get_object_or_404(Competition, id=competition_id) if request.POST: current_highest_position = CompetitionPlacing.objects.filter(competition=competition).aggregate(Max('position'))['position__max'] next_position = (current_highest_position or 0) + 1 format = request.POST['format'] if format == 'tsv': rows = result_parser.tsv(request.POST['results']) elif format == 'pm1': rows = result_parser.partymeister_v1(request.POST['results']) elif format == 'pm2': rows = result_parser.partymeister_v2(request.POST['results']) elif format == 'wuhu': rows = result_parser.wuhu(request.POST['results']) else: return redirect('competition_edit', args=[competition_id]) for placing, title, byline, score in rows: if not title: continue production = Production( release_date=competition.shown_date, updated_at=datetime.datetime.now(), has_bonafide_edits=False, title=title) production.save() # assign an ID so that associations work if competition.platform: production.platforms = [competition.platform] if competition.production_type: production.types = [competition.production_type] if byline: production.byline_string = byline production.supertype = production.inferred_supertype production.save() placing = CompetitionPlacing( production=production, competition=competition, ranking=placing, position=next_position, score=score, ) next_position += 1 placing.save() Edit.objects.create(action_type='add_competition_placing', focus=competition, focus2=production, description=(u"Added competition placing for %s in %s competition" % (production.title, competition)), user=request.user) return redirect('competition_edit', args=[competition_id]) else: return render(request, 'competitions/import_text.html', { 'competition': competition, })
class ProductionSelection(StrAndUnicode): def __init__(self, id=None, title=None, byline_lookup=None, types_to_set=[]): self.id = id self.types_to_set = types_to_set if self.id: self.production = Production.objects.get(id=self.id) self.title = self.production.title self.byline = self.production.byline() self.byline_lookup = None else: self.production = None self.title = title self.byline_lookup = byline_lookup def commit(self): if not self.production: self.production = Production( title=self.title, updated_at=datetime.datetime.now(), ) # Ugh. We really ought to come up with a nice way of setting supertype # in advance of setting types, rather than having to save a second time # once the types are in place... self.production.save() self.production.types = self.types_to_set self.production.save() if self.byline: self.byline.commit(self.production) return self.production def __unicode__(self): return u"ProductionSelection: %s - %s" % (self.id, self.title) def __eq__(self, other): if not isinstance(other, ProductionSelection): return False return self.title == other.title and str(self.id) == str( other.id) and self.byline_lookup == other.byline_lookup def __ne__(self, other): return not self.__eq__(other) @staticmethod def from_value(value, types_to_set=[]): # value can be: # a Production # None # an integer (will be used as an ID) # an existing ProductionSelection if not value: return ProductionSelection() elif isinstance(value, ProductionSelection): return ProductionSelection(id=value.id, title=value.title, byline_lookup=value.byline_lookup, types_to_set=value.types_to_set) elif isinstance(value, int): return ProductionSelection(id=value) elif isinstance(value, Production): return ProductionSelection(id=value.id) else: raise Exception( "Don't know how to convert %s to a ProductionSelection!" % repr(value))
def import_text(request, competition_id): if not request.user.is_staff: return redirect('competition_edit', args=[competition_id]) competition = get_object_or_404(Competition, id=competition_id) if request.POST: current_highest_position = CompetitionPlacing.objects.filter( competition=competition).aggregate( Max('position'))['position__max'] next_position = (current_highest_position or 0) + 1 format = request.POST['format'] if format == 'tsv': rows = result_parser.tsv(request.POST['results']) elif format == 'pm1': rows = result_parser.partymeister_v1(request.POST['results']) elif format == 'pm2': rows = result_parser.partymeister_v2(request.POST['results']) elif format == 'wuhu': rows = result_parser.wuhu(request.POST['results']) else: return redirect('competition_edit', args=[competition_id]) for placing, title, byline, score in rows: if not title: continue production = Production(release_date=competition.shown_date, updated_at=datetime.datetime.now(), has_bonafide_edits=False, title=title) production.save() # assign an ID so that associations work if competition.platform: production.platforms = [competition.platform] if competition.production_type: production.types = [competition.production_type] if byline: production.byline_string = byline production.supertype = production.inferred_supertype production.save() placing = CompetitionPlacing( production=production, competition=competition, ranking=placing, position=next_position, score=score, ) next_position += 1 placing.save() Edit.objects.create( action_type='add_competition_placing', focus=competition, focus2=production, description=( u"Added competition placing for %s in %s competition" % (production.title, competition)), user=request.user) return redirect('competition_edit', args=[competition_id]) else: return render(request, 'competitions/import_text.html', { 'competition': competition, })